daraja-openapi

Testing with the mock

The mock is a local Daraja. It answers like the real API, posts real callbacks to your app a moment later, and lets you choose each payment's outcome with the phone number.

The sandbox is slow, rate-limited and sometimes down. The mock answers in milliseconds, needs no credentials, and makes every failure reproducible.

Run it

npx @bytegigs/daraja-mock --port 4010

Point your client at http://127.0.0.1:4010. The mock accepts the consumer key mock-consumer-key, the secret mock-consumer-secret, and Daraja's public sandbox shortcode 174379 and passkey.

Choose the outcome

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

The fifth row is the case that hurts in production: the customer paid, and your app never heard. Test that your app recovers.

Every operation

The mock serves all eleven operations. Results of B2C, B2B, reversal, transaction status and account balance are posted to your ResultURL. Paid STK, C2B and B2C transactions get receipts that transaction status and reversal can find. Dynamic QR returns a real PNG.

In GitHub Actions

Use Daraja Mock Server from the Marketplace:

- id: daraja
  uses: ByteGigs-Labs/daraja-mock-action@v1
- run: npm test
  env:
    DARAJA_BASE_URL: ${{ steps.daraja.outputs.url }}
InputDefaultWhat it does
port4010Port the mock listens on
callback-delay-ms1500How long the simulated customer takes to answer
allow-httplocalhostWhich http:// callback URLs to allow: never, localhost or always

Options

FlagDefaultWhat it does
--port4010Port to listen on
--delay1500Milliseconds before callbacks and results are sent
--allow-httplocalhostWhich http:// callback URLs to accept: never, localhost or always
--query-min-interval0Answer HTTP 429 to STK queries sent closer together than this, like the real sandbox
--c2b-validationoffSend C2B validation requests before confirmations
--quietoffDon't log each request

The mock listens only on 127.0.0.1, so nothing outside your machine or CI job can reach it.

View the source of this page

Built by ByteGigs Labs. Apache 2.0. M-PESA, Daraja and Safaricom are trademarks of Safaricom PLC; this project is independent and not endorsed by Safaricom.