Most Daraja API errors and STK Push problems come from one of three places: the request you send, the phone you send it to, or the callback URL you gave Safaricom. If a payment prompt never arrives, or it arrives but your system never hears back, start by checking those three things before you assume the API is broken.
How STK Push actually works
STK Push, also called M-Pesa Express or Lipa Na M-Pesa Online, is the prompt a customer sees on their phone asking them to enter their M-Pesa PIN. Your website or app calls Safaricom’s Daraja API with the customer’s phone number and the amount. Safaricom pushes the prompt to the phone. The customer approves or cancels it. Safaricom then sends the result to a callback URL that you provide, as a ResultCode and ResultDesc.
That callback step is where most integrations fail silently. If your server never receives it, your system has no way of knowing whether the payment went through.
The most common reasons a prompt never arrives
- Wrong shortcode or passkey. Sandbox credentials will not work against the production API, and the other way round. A mismatched passkey usually fails before the prompt is even sent.
- Phone number format. Daraja expects the number in the 2547XXXXXXXX format, not 07XXXXXXXX or +254. A malformed number is one of the most common causes of a request that fails immediately.
- Expired access token. The OAuth token from the Authorization API has a short lifetime. If your code caches it too long, requests start failing with an authorisation error.
- Phone is off or out of network coverage. The prompt cannot reach a phone that is switched off, has no signal, or is mid-call on some networks.
The most common reasons the prompt arrives but the payment fails
- The customer cancels it. This is completely normal. People change their mind, get interrupted, or tap the wrong button. Design your checkout to let them try again without starting over.
- No response in time. The prompt times out if the customer does not act on it quickly. Treat a timeout the same way you treat a cancellation: offer a retry.
- Insufficient funds. The customer’s M-Pesa balance is too low. Your error message to the customer should say this plainly rather than showing a generic “payment failed”.
- Wrong PIN entered. M-Pesa itself handles PIN retries. Your system only sees the final result.
Why the callback never reaches your server
This is the fault we see most often in integrations that “sometimes” work. Safaricom will only call back a URL that is publicly reachable over HTTPS with a valid SSL certificate. Common causes of a missing callback:
- The callback URL points to
localhostor a staging server that Safaricom’s servers cannot reach. - The SSL certificate is expired, self-signed, or misconfigured, so the HTTPS handshake fails silently.
- A firewall or hosting security rule is blocking Safaricom’s IP range.
- The endpoint takes too long to respond, or returns anything other than a quick success response, and Safaricom’s retry logic gives up.
If you are not sure whether a callback actually arrived, log every request your callback endpoint receives, including ones with an unexpected body. That log is the fastest way to tell “Safaricom never called us” apart from “our code crashed when it did”.
A practical checklist before you ask for help
- Confirm you are using the correct shortcode, passkey and consumer key/secret for the environment you are testing against.
- Confirm the phone number is in 2547XXXXXXXX format.
- Confirm your callback URL loads over HTTPS from outside your network, not just from your own machine.
- Check your server logs for the raw callback payload, not just your application’s interpretation of it.
- If a request fails immediately with no prompt at all, check the access token age and the exact error message Daraja returns rather than guessing.
For the definitive list of request parameters, response fields and current API behaviour, Safaricom’s own Daraja developer portal is the source to check, since third-party guides (including this one) can go out of date.
Sandbox to production: the migration step people skip
A lot of “it worked for months then broke” reports trace back to the move from sandbox to production. The shortcode changes. The passkey changes. In some setups the callback URL changes too, from a staging domain to the live one. If your SSL certificate on the live domain was never tested end to end, the callback can fail even though everything looked fine in sandbox testing.
Before you go live, send a real STK Push to a real phone on the production shortcode, approve it, and confirm the callback actually lands in your logs with the correct amount and reference. Do this once with a small real amount rather than trusting that sandbox success means production will behave the same way.
What good error handling looks like to the customer
A failed payment is not just a technical event, it is a moment where a customer can lose trust in your checkout. Good practice is to:
- Show a clear, specific message (cancelled, insufficient funds, timed out) instead of a generic “something went wrong”.
- Let the customer retry immediately without re-entering their order details.
- Keep the order in a pending state until you have a confirmed callback, rather than marking it paid early.
- Send a confirmation (on screen, by email, or by SMS) once the callback confirms success, so the customer is not left wondering.
This matters as much for a one-off online store as it does for recurring collection, like a school collecting fees through M-Pesa, where a parent needs to trust that a payment actually registered.
When it is worth getting help
If you have checked the basics above and Daraja API errors are still happening inconsistently, the cause is usually one of: a race condition in how you handle the callback, a server that cannot handle concurrent callbacks, or a sandbox-to-production migration that was never fully tested. We fix these for Kenyan businesses as part of our M-Pesa payment integration service, which we also use when adding M-Pesa checkout to an online store, and we build the same kind of STK Push handling into our own products, including the fee collection in Boochy Shuleni. If you want the fuller picture of how the pieces fit together, we covered the general flow in how M-Pesa integration works for a Kenyan website.
FAQ
Why does STK Push work in the sandbox but not in production?
Sandbox and production use different shortcodes, passkeys and API hosts. A setup that only has sandbox credentials configured will fail in production even if the code is identical.
Can I retry a failed STK Push automatically?
You can prompt the customer to try again, but do not auto-resend without the customer’s action. A customer who cancelled on purpose does not want a second prompt arriving unannounced.
How long should I wait for a callback before treating a payment as failed?
Give it a reasonable window, then query the transaction status if you have not received a callback, rather than leaving the customer’s order in limbo indefinitely.
Does my website need its own server to use STK Push?
You need a backend that can call Daraja and receive its callback. A plain HTML page cannot do this alone; it has to be paired with server-side code.
Is STK Push the same as a payment gateway plugin?
A payment gateway plugin for WooCommerce or another platform is usually built on top of the same Daraja API. The plugin handles the integration for you, but it still depends on the same shortcode, passkey and callback URL requirements.
If STK Push is failing on your site and you have worked through the checklist above without a fix, talk to us on WhatsApp or request a quote and we will look at what is actually happening in your logs.
If you run a WooCommerce store, our Boochy M-Pesa plugin adds this same STK Push, PayBill and Buy Goods payment acceptance directly to WooCommerce checkout.