daraja-openapi

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
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.

OperationEndpointChecked against the sandbox
Generate an access tokenGET /oauth/v1/generate1 December 2026
Send an STK Push payment promptPOST /mpesa/stkpush/v1/processrequest1 December 2026
Check the result of an STK PushPOST /mpesa/stkpushquery/v1/query1 December 2026
Register C2B confirmation and validation URLsPOST /mpesa/c2b/v2/registerurl1 December 2026
Simulate a customer payment (sandbox only)POST /mpesa/c2b/v2/simulate1 December 2026
Send money from your shortcode to a customerPOST /mpesa/b2c/v3/paymentrequest1 December 2026
Pay another business's paybill or tillPOST /mpesa/b2b/v1/paymentrequest1 December 2026
Reverse a customer paymentPOST /mpesa/reversal/v1/request1 December 2026
Look up an M-PESA transactionPOST /mpesa/transactionstatus/v1/query1 December 2026
Read your shortcode's balancesPOST /mpesa/accountbalance/v1/query1 December 2026
Generate an M-PESA payment QR codePOST /mpesa/qrcode/v1/generate1 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.

  1. Start the mock: it listens on port 4010. All the options.
  2. Point any of the clients at http://127.0.0.1:4010.
  3. Use a test number below to get the result you want to handle.
npx @bytegigs/daraja-mock --port 4010

In 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 numberWhat happens
254700000001The customer cancels (1032)
254700000002Balance too low (1); B2C fails for insufficient funds
254700000003The phone can't be reached (1037)
254700000004Wrong PIN (2001)
254700000005Paid, but the callback never arrives; B2C times out in the queue
Any other valid numberPaid

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.

LanguagePackageGood for
TypeScript@bytegigs/darajaNode, Next.js, serverless
Pythonbytegigs-darajaDjango, Flask, FastAPI (sync and async)
PHPbytegigs/darajaLaravel, WordPress, shared hosting; no dependencies
Kotlincom.bytegigs:darajaKtor, Spring; server-side only
SwiftDaraja (SwiftPM)Vapor, Hummingbird