Take M-PESA payments without reverse-engineering Daraja
A precise OpenAPI description of Safaricom's Daraja API, checked against the real sandbox every night, with clients for five languages and a mock you can run offline.
- STK Push, C2B, B2C, B2B, reversal, transaction status, account balance and Dynamic QR.
- Every client handles tokens, the STK password, Kenyan time and phone formats for you.
- Pending prompts, rate limits and Daraja's misspelled fields are documented, not discovered.
What arrives at your callback
- result
- Paid, confirmed with Daraja
- receipt
- TKU3XXXXXX
- amount
- Ksh 150
- phone
- 254712345678
- when
- 2026-12-01 10:15:12 EAT
What the spec covers
Each operation shows the date its contract test last passed against Safaricom's sandbox. If Daraja changes, the nightly check opens an issue the same day.
| Operation | Endpoint | Checked against the sandbox |
|---|---|---|
| Generate an access token | GET /oauth/v1/generate | 1 December 2026 |
| Send an STK Push payment prompt | POST /mpesa/stkpush/v1/processrequest | 1 December 2026 |
| Check the result of an STK Push | POST /mpesa/stkpushquery/v1/query | 1 December 2026 |
| Register C2B confirmation and validation URLs | POST /mpesa/c2b/v2/registerurl | 1 December 2026 |
| Simulate a customer payment (sandbox only) | POST /mpesa/c2b/v2/simulate | 1 December 2026 |
| Send money from your shortcode to a customer | POST /mpesa/b2c/v3/paymentrequest | 1 December 2026 |
| Pay another business's paybill or till | POST /mpesa/b2b/v1/paymentrequest | 1 December 2026 |
| Reverse a customer payment | POST /mpesa/reversal/v1/request | 1 December 2026 |
| Look up an M-PESA transaction | POST /mpesa/transactionstatus/v1/query | 1 December 2026 |
| Read your shortcode's balances | POST /mpesa/accountbalance/v1/query | 1 December 2026 |
| Generate an M-PESA payment QR code | POST /mpesa/qrcode/v1/generate | 1 December 2026 |
Test without the sandbox
The sandbox is slow, rate-limited and down at awkward times. The mock answers like Daraja, sends real callbacks, and lets you pick the outcome with the phone number.
- Start the mock: it listens on port 4010. All the options.
- Point any of the clients at
http://127.0.0.1:4010. - Use a test number below to get the result you want to handle.
npx @bytegigs/daraja-mock --port 4010In GitHub Actions, add Daraja Mock Server from the Marketplace:
- id: daraja
uses: ByteGigs-Labs/daraja-mock-action@v1
# then use ${{ steps.daraja.outputs.url }} as your Daraja base URL| Phone number | What happens |
|---|---|
254700000001 | The customer cancels (1032) |
254700000002 | Balance too low (1); B2C fails for insufficient funds |
254700000003 | The phone can't be reached (1037) |
254700000004 | Wrong PIN (2001) |
254700000005 | Paid, but the callback never arrives; B2C times out in the queue |
| Any other valid number | Paid |
Clients
All five follow the same rules: secrets never appear in logs, input is checked before anything is sent, and STK Push and payouts are never retried for you, because a retry could charge or pay someone twice.
| Language | Package | Good for |
|---|---|---|
| TypeScript | @bytegigs/daraja | Node, Next.js, serverless |
| Python | bytegigs-daraja | Django, Flask, FastAPI (sync and async) |
| PHP | bytegigs/daraja | Laravel, WordPress, shared hosting; no dependencies |
| Kotlin | com.bytegigs:daraja | Ktor, Spring; server-side only |
| Swift | Daraja (SwiftPM) | Vapor, Hummingbird |