
The Safaricom Daraja API is a platform that allows developers to integrate M-Pesa payments into applications such as:
π It enables automatic payment requests, confirmations, and transaction tracking
Before writing any code, ensure you have:
Daraja uses OAuth.
GET /oauth/v1/generate?grant_type=client_credentials
const axios = require("axios");
const auth = Buffer.from("CONSUMER_KEY:CONSUMER_SECRET").toString("base64");
axios.get("https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials", {
headers: {
Authorization: `Basic ${auth}`
}
})
.then(res => console.log(res.data.access_token));π This token is used in all API requests
Password = Base64 encode of:
Shortcode + Passkey + Timestamp
Example:
const timestamp = "20260421123000";
const password = Buffer.from(shortcode + passkey + timestamp).toString("base64");POST /mpesa/stkpush/v1/processrequest
axios.post("https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest", {
BusinessShortCode: shortcode,
Password: password,
Timestamp: timestamp,
TransactionType: "CustomerPayBillOnline",
Amount: "100",
PartyA: "2547XXXXXXXX",
PartyB: shortcode,
PhoneNumber: "2547XXXXXXXX",
CallBackURL: "https://yourdomain.com/callback",
AccountReference: "POS001",
TransactionDesc: "Payment"
}, {
headers: {
Authorization: `Bearer ${access_token}`
}
});π This triggers payment prompt on customer phone
When payment is completed, Safaricom sends a response to your server.
{
"Body": {
"stkCallback": {
"ResultCode": 0,
"ResultDesc": "Success",
"CallbackMetadata": {
"Item": [
{"Name": "Amount", "Value": 100},
{"Name": "MpesaReceiptNumber", "Value": "ABC123XYZ"},
{"Name": "PhoneNumber", "Value": 2547XXXXXXXX}
]
}
}
}
}ResultCode == 0 β
In your POS interface:
β οΈ Critical for real systems:
π Always test fully before going live
Your POS should:
π This creates a fully automated digital biashara system
You can:
Integrating M-Pesa via Daraja API turns a simple POS into a powerful automated payment system.