M-Pesa STK Push Integration: A Production Checklist for Developers
How the Daraja STK push flow really works, and the details that separate a sandbox demo from a payment system you can trust with real money.
By DevOpsCommand Engineering · · 3 min read

Getting an STK push prompt to appear on a phone in the Daraja sandbox takes an afternoon. Building an M-Pesa integration that never loses a payment, never double-credits an order and keeps finance happy at month end takes more care. This checklist covers the parts that tutorials usually skip.
How the STK push flow works
- Your server requests an OAuth access token using your app's consumer key and secret. Tokens expire after about an hour, so cache them and refresh before expiry rather than requesting one per payment.
- Your server calls the STK push endpoint with the shortcode, amount, the customer's phone number in 2547XXXXXXXX format, a callback URL and a password built from
base64(Shortcode + Passkey + Timestamp), where the timestamp uses the formatYYYYMMDDHHmmss. - Safaricom responds immediately with a
CheckoutRequestID. This only means the request was accepted, not that the customer has paid. - The customer enters their PIN, cancels or ignores the prompt. Safaricom then sends the result to your callback URL. A
ResultCodeof 0 means success; anything else is a failure or cancellation.
1. Store the request before you send it
Create a payment record with status PENDING, the order reference, amount and phone number before calling Daraja, then save the returned CheckoutRequestID against it. When the callback arrives, you look up the payment by that ID. If you only create records when callbacks arrive, any callback you fail to process is a payment you cannot trace.
2. Treat the callback as untrusted input
Your callback URL is public. Anyone who finds it can post a fake success message. Protect it:
- Use a long, unguessable path and HTTPS only.
- Restrict the endpoint to Safaricom's published IP ranges at your firewall or reverse proxy where practical.
- Only accept callbacks whose
CheckoutRequestIDmatches a pending payment you created. - Check that the amount in the callback metadata matches the amount you requested.
3. Make processing idempotent
Callbacks can arrive more than once, and your own retries can overlap. Update the payment inside a database transaction with a condition such as WHERE status = 'PENDING', so a second callback finds nothing to update and the order is fulfilled exactly once. Always respond to Safaricom quickly with a success acknowledgement, and do slow work such as sending emails in a background job.
4. Do not rely on the callback alone
Callbacks occasionally fail to arrive because of network problems or downtime on your side. Run a scheduled job that finds payments still pending after a few minutes and calls the STK push query endpoint with the CheckoutRequestID to fetch the final status. This single job eliminates most "customer paid but order not confirmed" support tickets.
5. Record the M-Pesa receipt number
The successful callback includes the M-Pesa receipt number, such as SKL4ABC123. Store it, show it to the customer and include it in exports for finance. It is the reference your accounts team will use to reconcile against the M-Pesa statement.
6. Handle the common failure codes clearly
Customers cancel prompts, enter the wrong PIN, have insufficient balance or let the request time out. Show a clear message for each and let them retry, instead of a generic "payment failed".
7. Keep credentials out of code
Your consumer key, secret and passkey belong in a secrets manager or server-side environment variables, never in a front-end bundle or a Git repository. Rotate them if they are ever exposed.
8. Go live deliberately
- Complete Safaricom's go-live process for your production shortcode and update the base URL from sandbox to production.
- Test with small real amounts on the production shortcode before announcing.
- Set up monitoring for callback errors, pending payments older than ten minutes and token failures.
We build and review M-Pesa integrations as part of our Payment Integrations service, alongside Stripe, Flutterwave, Paystack and Pesapal. If your current integration loses payments or needs a security review, book a free technical review.