M-Pesa API: how to integrate it
The M-Pesa API, called Daraja, lets your app take payments from Safaricom M-Pesa users in Kenya. The best-known flow is STK Push: your app sends a payment prompt to the customer's phone, they enter their PIN, and the money arrives. Here's what you need to get started and go live.
Key facts
- Developer portaldeveloper.safaricom.co.ke (Daraja)
- Main flowLipa Na M-Pesa Online (STK Push). The customer enters their PIN on their phone.
- Other APIsC2B (customer pays your Paybill or Till), B2C (send money to customers), transaction status
- Going live needs a Paybill or Till number from Safaricom
- Your callback URL must be public HTTPS
How the integration works
- Create an account on the Daraja portal and create an app to get your consumer key and secret.
- Test in the sandbox with the test shortcode and passkey.
- Get an access token, then call the STK Push endpoint with the amount, the customer's phone number (2547XXXXXXXX) and your callback URL.
- The customer enters their M-Pesa PIN on the prompt.
- Safaricom calls your callback URL with the result. Confirm it from your server before giving value.
- Apply to go live with your Paybill or Till number, then swap in your production credentials.
Want it done for you?
Kollekt integrates M-Pesa into your app or store. Built your app but stuck on payments? Kollekt integrates mobile money for you.
Questions people ask
Do I need a Paybill or Till number to use the M-Pesa API?
For live payments, yes. Daraja production access is tied to a Paybill or Till number registered with Safaricom. If you don't have one, a payment gateway that supports M-Pesa lets you accept it through their account setup instead.
Does the M-Pesa API work outside Kenya?
Daraja is for Safaricom M-Pesa in Kenya. M-Pesa in other countries, like Tanzania (Vodacom) or DR Congo, is run by different operators with their own APIs and onboarding.