Configuration
Every option DarajaConfig accepts, and what happens if you get one wrong.
interface DarajaConfig {
consumerKey: string;
consumerSecret: string;
environment?: "sandbox" | "production"; // default: 'sandbox'
timeout?: number; // default: 10000 (ms)
}Prop
Type
consumerKey / consumerSecret
Required. Both go through a validation pipeline the moment you call Daraja(config) before any network request is made:
- Presence not
undefined,null, or"" - Type must be a
string - Sanitization surrounding quotes, tabs, and newlines picked up from
.envcopy-paste are stripped automatically - Placeholder detection values like
"YOUR_CONSUMER_KEY","changeme", or"xxxx..."are rejected outright - No internal whitespace
- Alphanumeric only
- Exact length 48 characters for the Consumer Key, 64 for the Consumer Secret
- Cross-check if
consumerKey === consumerSecret, or if one field's length matches what the other field is supposed to be, the error message tells you that you likely swapped them
// Throws: "Consumer Key must be exactly 48 characters (received 64, 16
// characters too long). This matches the expected length of the other
// credential. Did you swap Consumer Key and Consumer Secret?"
Daraja({
consumerKey: process.env.DARAJA_CONSUMER_SECRET!, // oops
consumerSecret: process.env.DARAJA_CONSUMER_KEY!,
});This validation exists because a swapped key/secret pair produces a generic
400 Bad Request from Daraja's OAuth endpoint with no indication of what's
actually wrong. The SDK catches it before the request ever leaves your
machine.
environment
'sandbox'(default) points athttps://sandbox.safaricom.co.ke'production'points athttps://api.safaricom.co.ke, and additionally requires HTTPS on every callback URL (callBackURL,confirmationUrl,validationUrl,queueTimeOutURL,resultURL). Passing anhttp://callback URL in production throwsINVALID_CALLBACK_URLimmediately instead of letting Daraja reject it later.
Any value outside 'sandbox' / 'production' throws INVALID_ENVIRONMENT at construction time.
timeout
Milliseconds before an in-flight request is aborted via AbortController. Applies to every HTTP call the SDK makes, including the OAuth token fetch. Must be a positive, finite number 0, negative numbers, NaN, and Infinity all throw INVALID_TIMEOUT.
Aborted requests surface as a DarajaError with errorCode: 'REQUEST_TIMEOUT', not a raw AbortError so your error handling only needs to know about one error shape. See Error Handling.
Full example
import { Daraja } from "@lumierelabs/daraja";
export const daraja = Daraja({
consumerKey: process.env.DARAJA_CONSUMER_KEY!,
consumerSecret: process.env.DARAJA_CONSUMER_SECRET!,
environment: process.env.NODE_ENV === "production" ? "production" : "sandbox",
timeout: 15000,
});