Guides
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 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 |
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 }}
| Input | Default | What it does |
|---|---|---|
port | 4010 | Port the mock listens on |
callback-delay-ms | 1500 | How long the simulated customer takes to answer |
allow-http | localhost | Which http:// callback URLs to allow: never, localhost or always |
Options
| Flag | Default | What it does |
|---|---|---|
--port | 4010 | Port to listen on |
--delay | 1500 | Milliseconds before callbacks and results are sent |
--allow-http | localhost | Which http:// callback URLs to accept: never, localhost or always |
--query-min-interval | 0 | Answer HTTP 429 to STK queries sent closer together than this, like the real sandbox |
--c2b-validation | off | Send C2B validation requests before confirmations |
--quiet | off | Don't log each request |
The mock listens only on 127.0.0.1, so nothing outside your machine or CI job can reach it.