daraja-openapi

Credentials and environments

Daraja has two environments with separate credentials. Nothing from the sandbox works in production, and the other way round.

The values you need

ValueWhat it isWhere to find it
Consumer key and secretIdentify your Daraja app; exchanged for an access tokenYour app on the Daraja portal
ShortcodeYour paybill or till numberSandbox: 174379 for STK Push. Production: the number Safaricom gave your business
PasskeyUsed to build the STK Push passwordSandbox: the portal's test credentials page. Production: sent by Safaricom at go-live
Initiator name and passwordThe API user for B2C, B2B, reversal, status and balanceSandbox test credentials, or the M-PESA org portal in production
Safaricom's certificateEncrypts the initiator passwordDaraja's docs; the sandbox and production certificates differ

Base URLs

EnvironmentBase URL
Sandboxhttps://sandbox.safaricom.co.ke
Productionhttps://api.safaricom.co.ke

Set environment to sandbox or production and the SDKs pick the base URL. To use the mock, set the base URL to http://127.0.0.1:4010 instead. Plain http:// is accepted only for your own machine, because your consumer secret travels to that address.

API products

Each Daraja app only reaches the API products added to it on the portal. Calling one that isn't added fails with HTTP 401 and the message "Invalid API call as no apiproduct match found". In the sandbox, add M-Pesa Sandbox for C2B, B2C and B2B, then request a new access token.

Access tokens

The SDKs fetch the token for you, cache it, and refresh it a minute before it expires. Concurrent requests share one refresh, so a traffic spike doesn't send a burst of token requests. In PHP, where each web request usually starts fresh, pass a TokenStore backed by your cache; see the PHP SDK.

Keeping secrets safe

View the source of this page

Built by ByteGigs Labs. Apache 2.0. M-PESA, Daraja and Safaricom are trademarks of Safaricom PLC; this project is independent and not endorsed by Safaricom.