M-Pesa Daraja API integration: how it works and what it takes
M-Pesa Daraja integration connects your website, app or system to Safaricom's M-Pesa APIs so payments are requested, confirmed and recorded automatically. You need a Paybill or Till number, a Safaricom developer account and a public HTTPS callback URL. Mjengo Corporate typically takes a Daraja integration from sandbox to live in 1–3 weeks.
Updated
| API | What it does | Typical use | Who starts the payment |
|---|---|---|---|
| M-Pesa Express (STK Push) | Sends a PIN prompt to the customer's phone | Online checkout, app payments, bill payment | Your system |
| C2B (Customer to Business) | Notifies your system when someone pays your Paybill/Till | Customers paying from the M-Pesa menu | The customer |
| B2C (Business to Customer) | Sends money from your shortcode to a phone number | Refunds, payouts, salaries, winnings | Your system |
| Transaction Status | Checks the result of a specific transaction | Reconciling missing or delayed callbacks | Your system |
| Account Balance | Queries your shortcode's balance | Finance dashboards, payout checks | Your system |
How does M-Pesa Daraja integration work?
Daraja is Safaricom's developer platform for M-Pesa. Your server authenticates with a consumer key and secret, then calls the API you need. For an STK Push, the flow looks like this:
- The customer enters their phone number at checkout.
- Your server sends an STK Push request with the amount and an account reference.
- The customer sees a PIN prompt on their phone and approves it.
- Safaricom sends the result to your callback URL.
- Your system marks the order as paid and, for example, releases the goods or switches a service on.
For C2B, customers pay from the M-Pesa menu and Safaricom sends a confirmation to the URL you registered for your shortcode.
What do you need before integrating M-Pesa?
- A Paybill or Till (Buy Goods) number registered to your business.
- Access to the M-Pesa organisation portal for that shortcode (the business administrator).
- A Safaricom developer account on the Daraja portal, where you create an app and test in the sandbox.
- A public HTTPS server to receive callbacks. Localhost and plain HTTP will not work in production.
- For go-live: the shortcode owner approves the production app, and you receive production credentials (and an STK passkey for M-Pesa Express).
Common M-Pesa integration pitfalls
These are the problems we see most often when taking over broken integrations:
- Trusting the browser instead of the callback. A payment is only confirmed when Safaricom's callback arrives (or a Transaction Status query confirms it), never because the customer clicked "I've paid".
- No idempotency. Callbacks can arrive more than once. Store the M-Pesa receipt number and ignore duplicates, or you will credit customers twice.
- Lost callbacks. If your server is down when the callback arrives, the payment is "missing". A reconciliation job using Transaction Status fixes this.
- Wrong account references. Customers type account numbers wrongly on C2B. Match by phone number and amount as a fallback, and flag unmatched payments for a human.
- Hard-coded sandbox credentials shipped to production.
How Mjengo Corporate builds M-Pesa integrations
M-Pesa is at the core of our own product. Netily, our ISP billing system, matches M-Pesa payments to subscribers and switches their internet back on automatically, with no router login and no manual unblocking. It works for ISPs with or without their own Daraja keys, and settles to 22 Kenyan banks and SACCOs.
For client projects we build the same way: callbacks with idempotency, a reconciliation job, a payments dashboard showing matched and unmatched transactions, and alerts when something fails. We handle the sandbox build, go-live paperwork with you, and production testing with real small payments.
Frequently asked questions
How long does M-Pesa Daraja integration take?
A standard STK Push or C2B integration takes 1–3 weeks from sandbox to live at Mjengo Corporate. Most of the variation comes from how quickly the shortcode owner completes the go-live approval.
Can I integrate M-Pesa without a Paybill?
To receive payments automatically into your own account you need a Paybill or Till number. Some platforms, including Netily for ISPs, can collect payments on your behalf and settle to your bank, but for your own system a shortcode is the standard route.
What is the difference between STK Push and C2B?
With STK Push your system sends a PIN prompt to the customer's phone. With C2B the customer pays from the M-Pesa menu and your system is notified. Many systems use both.
Does Safaricom charge for using the Daraja API?
Access to the Daraja API itself is provided to shortcode holders; normal M-Pesa transaction tariffs for your Paybill or Till still apply. Check current tariffs with Safaricom for your account type.
