What Daraja is
By the end of this track you can make an authenticated call and explain the async model.
0/4 finished
Step 1
The idea
Daraja is Safaricom's web API for M-Pesa. Your server sends HTTPS requests, M-Pesa moves the money, and the result comes back later to a URL you host. A business is identified by its shortcode: its Paybill or Till number.
Step 2
How it works
The three parties
Every Daraja payment involves three parties: your server, which asks for the payment; Daraja, Safaricom's API, which passes the request into M-Pesa; and the customer, whose phone shows a prompt or who pays from the M-Pesa menu.
Your browser or mobile app never talks to Daraja directly. It talks to your server, and your server holds the keys.
Why the answer comes later
Money moves only when a person acts: they type their PIN, or they don't. That can take seconds or never happen. So Daraja can't keep your request open until it knows. It answers straight away with "I got it", then later sends the real result as an HTTPS POST to a URL you gave it: your callback URL.
This split, a quick acknowledgement now and the result later, is the idea behind every lesson that follows.
Two environments, two hosts
The sandbox uses fake money and test numbers. Production moves real money. They have different hosts and different keys, and keys from one never work on the other.
Sandbox https://sandbox.safaricom.co.keProduction https://api.safaricom.co.keShortcodes
A business is identified by its shortcode. A Paybill takes an account number with each payment, so it suits invoices and bills. A Till (Buy Goods) takes just an amount, so it suits shops. Most Daraja requests name the shortcode they act for.
The APIs you will meet
- Collecting: STK Push, STK Query, C2B (Paybill and Till), Dynamic QR.
- Paying out: B2C (to phones), B2B (to other businesses).
- Operations: Transaction Status, Reversal, Account Balance.
Step 3
Try it
Nothing to run yet. Think about what you want to build: taking payments, paying people, or both.
Step 4
Common mistakes
- Calling Daraja from the browser or a mobile app. The keys would ship to every user.
- Treating the first response as the payment result.
- Building against production first. Learn on the sandbox, where mistakes cost nothing.
Go deeper
Where does the final result of a payment arrive?
Answer in your own words first.
Show the answerHide the answer
At your callback URL, later. Not in the first response.