99.9% Live Uptime Free .co.ke Domain on Business Plans →

Safaricom Daraja API and M-Pesa STK Push Explained for Business Owners

If you sell online in Kenya, you will hear two terms constantly: Daraja and STK Push. Together they are the technology behind most "Pay with M-Pesa" buttons on websites and apps. You do not need to be a developer to benefit from them, but understanding how they work helps you ask the right questions, choose the right setup and diagnose problems.

What Daraja is

Daraja is Safaricom's official developer platform for M-Pesa. It is a set of APIs that let your website or software talk to M-Pesa programmatically, instead of staff checking a statement and matching payments by hand. Before Daraja, businesses had no secure, official way to connect their systems directly to M-Pesa, and payments had to be reconciled manually against paybill statements.

The main Daraja capabilities include:

APIWhat it doesTypical use
M-Pesa Express (STK Push)Sends a payment prompt to a customer's phoneOnline checkout, invoice payment
C2B (customer to business)Notifies you when a customer pays your paybill or till directlyManual paybill payments recorded automatically
B2C (business to customer)Sends money from your business to a customerRefunds, payouts, salaries, disbursements
Transaction statusLooks up the result of a transactionResolving uncertain payments
Account balanceChecks your shortcode's balanceOperations and finance
ReversalReverses a transactionCorrections within Safaricom's rules

What STK Push is

STK Push (named after the SIM Toolkit menu on a phone) lets a website trigger an M-Pesa payment prompt directly on the customer's handset. Instead of memorising a paybill number, an account number and an amount, the customer types their phone number at checkout and gets a pop-up asking them to enter their M-Pesa PIN. That removes most of the friction of mobile payments.

How an STK Push payment works, step by step

  1. The customer chooses M-Pesa at checkout and enters their phone number, in the 254XXXXXXXXX format.
  2. Your server authenticates with Daraja using your consumer key and secret to obtain a short-lived access token.
  3. Your server sends the STK Push request with the shortcode, amount, phone number, an account reference and a callback URL.
  4. Safaricom sends the prompt to the customer's phone.
  5. The customer enters their PIN on their own device. The PIN never passes through your website.
  6. M-Pesa processes the payment and sends the result to your callback URL as a JSON message.
  7. Your system updates the order or invoice, stores the M-Pesa receipt number and tells the customer.

What you need to go live

  • A paybill or till number registered to your business. This is where the money lands.
  • A Daraja developer account and an app, which gives you consumer key and secret for the sandbox, then production credentials once approved.
  • A passkey and shortcode for the STK Push (Lipa Na M-Pesa Online) product.
  • A public HTTPS callback URL that Safaricom can reach. If it is unreachable, you will not receive payment confirmations.
  • A tested flow that handles success, cancellation, timeout and failure.

Why the callback matters

The STK Push response you receive immediately only says that the request was accepted. The callback is the message that tells you whether the customer actually paid. A correct integration:

  • Verifies the callback relates to a payment you started.
  • Marks the order paid only when the result code indicates success.
  • Stores the M-Pesa receipt number against the order.
  • Handles duplicate callbacks safely.
  • Falls back to a transaction status query if the callback never arrives.

Common result outcomes

OutcomeWhat it meansWhat your system should do
Success (result code 0)Customer paidMark paid, store receipt
Cancelled by userCustomer dismissed the promptKeep the order open and let them retry
Insufficient fundsBalance too lowTell the customer; offer another method
Wrong PINCustomer entered the incorrect PINLet them retry
Timeout / unreachablePhone offline or no responseOffer retry or manual payment instructions

Is it secure?

Yes, and by design. The customer's PIN is entered into Safaricom's own prompt on their phone, never in your website. Nothing is charged without the customer's PIN approval. Your business receives only confirmation details such as the amount, phone number and receipt. See is it safe to pay online with M-Pesa? for the customer's view.

Direct integration or aggregator?

You can integrate Daraja yourself, or use an aggregator that has already done so and adds cards and other methods. Each has trade-offs in control, cost and effort. We compare them in PesaPal vs direct Daraja. If you run WooCommerce, the practical steps are in how to add M-Pesa checkout to your website.

What this means for your business

You do not have to write any of this yourself. What you need is hosting or a developer who has built and secured the integration properly. Our own billing system accepts M-Pesa through a custom Daraja integration, so customers can pay an invoice with a prompt on their phone, and our eCommerce hosting is set up for M-Pesa-ready checkout.

Frequently asked questions

Do I need a paybill or can I use a till number?

Both can be used, with different transaction types. A till (Buy Goods) is common for retail; a paybill suits businesses that need account references such as invoice numbers.

Is the sandbox the same as the live system?

No. It is a test environment with test credentials and no real money. Your live credentials and endpoints are different, and a common mistake is forgetting to switch them.

How long does it take to go live?

That depends on your shortcode registration and Safaricom's approval steps. Start the paybill or till process early.

Can I use STK Push for recurring billing?

You can send a prompt on each renewal date, but each payment still needs the customer's PIN. See invoices, reconciliation and recurring billing with M-Pesa.