Get started
Quick start
Send an M-PESA prompt to a phone and confirm the payment, in about five minutes.
Before you start
You need a Daraja sandbox app. Create one on the Daraja portal, then note its consumer key and consumer secret. The sandbox uses the public test shortcode 174379 and its passkey, both shown on the portal's test credentials page. See credentials and environments for what each value is.
Keep these in environment variables, never in your code.
Install
npm install @bytegigs/daraja
pip install bytegigs-daraja
composer require bytegigs/daraja
implementation("com.bytegigs:daraja:0.2.0")
.package(url: "https://github.com/ByteGigs-Labs/daraja-openapi", from: "0.2.0")
Send the prompt
import { Daraja } from "@bytegigs/daraja";
const daraja = new Daraja({
consumerKey: process.env.DARAJA_CONSUMER_KEY,
consumerSecret: process.env.DARAJA_CONSUMER_SECRET,
environment: "sandbox",
shortCode: "174379",
passkey: process.env.DARAJA_PASSKEY,
});
const push = await daraja.stkPush({
amount: 150,
phone: "0712 345 678",
callbackUrl: "https://your-app.example/daraja/callback",
accountReference: "INV-1001",
description: "Invoice 1001",
});
import os
from daraja import Daraja
daraja = Daraja(
os.environ["DARAJA_CONSUMER_KEY"],
os.environ["DARAJA_CONSUMER_SECRET"],
environment="sandbox",
short_code="174379",
passkey=os.environ["DARAJA_PASSKEY"],
)
push = daraja.stk_push(
amount=150,
phone="0712 345 678",
callback_url="https://your-app.example/daraja/callback",
account_reference="INV-1001",
description="Invoice 1001",
)
use ByteGigs\Daraja\Daraja;
$daraja = new Daraja(
getenv('DARAJA_CONSUMER_KEY'),
getenv('DARAJA_CONSUMER_SECRET'),
environment: 'sandbox',
shortCode: '174379',
passkey: getenv('DARAJA_PASSKEY'),
);
$push = $daraja->stkPush(
amount: 150,
phone: '0712 345 678',
callbackUrl: 'https://your-app.example/daraja/callback',
accountReference: 'INV-1001',
description: 'Invoice 1001',
);
val daraja = Daraja(
System.getenv("DARAJA_CONSUMER_KEY"),
System.getenv("DARAJA_CONSUMER_SECRET"),
shortCode = "174379",
passkey = System.getenv("DARAJA_PASSKEY"),
)
val push = daraja.stkPush(
amount = 150,
phone = "0712 345 678",
callbackUrl = "https://your-app.example/daraja/callback",
accountReference = "INV-1001",
description = "Invoice 1001",
)
import Daraja
let daraja = try DarajaClient(
consumerKey: env("DARAJA_CONSUMER_KEY"),
consumerSecret: env("DARAJA_CONSUMER_SECRET"),
environment: .sandbox,
shortCode: "174379",
passkey: env("DARAJA_PASSKEY")
)
let push = try await daraja.stkPush(
amount: 150,
phone: "0712 345 678",
callbackURL: "https://your-app.example/daraja/callback",
accountReference: "INV-1001",
description: "Invoice 1001"
)
Phone numbers can be written any common Kenyan way: 0712 345 678, +254 712 345 678 and 712345678 all become 254712345678. The client checks every field before sending, so a description over 13 characters fails on your server, not at Daraja.
Confirm the payment
The customer answers on their phone, and Daraja POSTs the result to your callback URL. Callbacks aren't signed, so confirm before you deliver anything:
const result = await daraja.waitForStkResult({ checkoutRequestId: push.CheckoutRequestID });
if (result.state === "paid") await markInvoicePaid("INV-1001");
else if (result.state === "failed") console.log(result.meaning); // "The customer cancelled the payment"
status = daraja.wait_for_stk_result(push["CheckoutRequestID"])
if status.state == "paid":
mark_invoice_paid("INV-1001")
elif status.state == "failed":
print(status.meaning)
$status = $daraja->waitForStkResult($push['CheckoutRequestID']);
if ($status->isPaid()) {
markInvoicePaid('INV-1001');
}
val status = daraja.waitForStkResult(push["CheckoutRequestID"]!!.jsonPrimitive.content)
if (status.state == StkState.PAID) markInvoicePaid("INV-1001")
let status = try await daraja.waitForStkResult(checkoutRequestID: push.CheckoutRequestID)
if case .paid = status { try await markInvoicePaid("INV-1001") }
waitForStkResult waits a few seconds before its first query, treats "still processing" as waiting, and backs off when Daraja rate-limits. In a web request, prefer the callback and confirm there.
Next
Try every failure case without the sandbox using the mock, then read handling callbacks before you go live.