Skip to content

Configuration

  • Explicit constructor config wins.
  • Missing values fall back to environment variables.
  • Missing auth credentials fail fast with MpesaValidationError.
  • Invalid environment values fail fast instead of silently defaulting.
VariablePurpose
MPESA_CONSUMER_KEYDaraja app consumer key
MPESA_CONSUMER_SECRETDaraja app consumer secret
MPESA_ENVIRONMENTsandbox or production
MPESA_SHORT_CODEPaybill or till number
MPESA_PASS_KEYLipa Na M-Pesa online passkey
MPESA_INITIATOR_NAMEInitiator username for B2C, balance, status, and reversal
MPESA_SECURITY_CREDENTIALEncrypted initiator password
const mpesa = new Mpesa({
consumerKey: process.env.MPESA_CONSUMER_KEY,
consumerSecret: process.env.MPESA_CONSUMER_SECRET,
environment: "sandbox",
shortCode: process.env.MPESA_SHORT_CODE,
passKey: process.env.MPESA_PASS_KEY,
initiatorName: process.env.MPESA_INITIATOR_NAME,
securityCredential: process.env.MPESA_SECURITY_CREDENTIAL,
});
  • STK Push and STK Query need shortCode and passKey.
  • C2B register URLs and sandbox simulation need shortCode.
  • B2C, account balance, transaction status, and reversal need shortCode, initiatorName, and securityCredential.
  • Do not commit real Daraja credentials.
  • Generate securityCredential from the initiator password using Safaricom’s approved RSA flow for your profile.
  • Keep sandbox and production values separate. Mixing credentials across environments is a common source of MpesaAuthError and MpesaRequestError failures.