Get started
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
| Value | What it is | Where to find it |
|---|---|---|
| Consumer key and secret | Identify your Daraja app; exchanged for an access token | Your app on the Daraja portal |
| Shortcode | Your paybill or till number | Sandbox: 174379 for STK Push. Production: the number Safaricom gave your business |
| Passkey | Used to build the STK Push password | Sandbox: the portal's test credentials page. Production: sent by Safaricom at go-live |
| Initiator name and password | The API user for B2C, B2B, reversal, status and balance | Sandbox test credentials, or the M-PESA org portal in production |
| Safaricom's certificate | Encrypts the initiator password | Daraja's docs; the sandbox and production certificates differ |
Base URLs
| Environment | Base URL |
|---|---|
| Sandbox | https://sandbox.safaricom.co.ke |
| Production | https://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
- Keep every value above in environment variables or a secret manager, never in source code.
- Never put them in a mobile or browser app. Call your own server, and let it call Daraja.
- The SDKs never print secrets: logging the client shows only its base URL and shortcode.