API reference / Dynamic QR
Generate an M-PESA payment QR code
POST
/mpesa/qrcode/v1/generateChecked against the Safaricom sandbox on 1 December 2026
Returns a QR code that customers scan in the M-PESA app to pay a fixed amount
to your paybill, till, agent or phone number. QRCode is a base64 PNG.
In the SDKs
| SDK | Method |
|---|---|
| TypeScript | dynamicQr() |
| Python | dynamic_qr() |
| PHP | dynamicQr() |
| Kotlin | dynamicQr() |
| Swift | dynamicQR() |
Request
| Environment | URL |
|---|---|
| Sandbox | https://sandbox.safaricom.co.ke/mpesa/qrcode/v1/generate |
| Production | https://api.safaricom.co.ke/mpesa/qrcode/v1/generate |
Send the access token as Authorization: Bearer <token>, and the body as JSON.
| Field | Type | Description |
|---|---|---|
MerchantName | stringRequired | |
RefNo | stringRequired | The account number or reference shown to the customer. |
Amount | integerRequired | At least 1. |
TrxCode | stringRequired | BG buy goods (till), WA agent withdrawal, PB paybill, SM send money to a phone number, SB send to a business. One of BG, WA, PB, SM, SB. |
CPI | stringRequired | The till, agent, paybill or phone number to pay. |
Size | stringRequired | Width and height of the image in pixels, as a string. Matches ^[0-9]+$. |
Example request
curl -X POST "https://sandbox.safaricom.co.ke/mpesa/qrcode/v1/generate" \
-H "Authorization: Bearer $TOKEN" \
-H "Content-Type: application/json" \
-d '{
"MerchantName": "Mama Mboga Online",
"RefNo": "INV-1001",
"Amount": 250,
"TrxCode": "PB",
"CPI": "174379",
"Size": "300"
}'Response
The QR code.
| Field | Type | Description |
|---|---|---|
ResponseCode | stringRequired | |
RequestID | string | |
ResponseDescription | stringRequired | |
QRCode | stringRequired | A PNG image, base64-encoded. |
Errors
| Status | Meaning |
|---|---|
400 | Daraja rejected the request. An HTTP 401 with "Invalid API call as no apiproduct match found" means your Daraja app does not have this API product enabled: add it to the app on the Daraja portal, then request a new access token. |
401 | Daraja rejected the request. An HTTP 401 with "Invalid API call as no apiproduct match found" means your Daraja app does not have this API product enabled: add it to the app on the Daraja portal, then request a new access token. |
500 | Daraja rejected the request. An HTTP 401 with "Invalid API call as no apiproduct match found" means your Daraja app does not have this API product enabled: add it to the app on the Daraja portal, then request a new access token. |
See errors and rate limits for what to do with each.
Example response
{
"ResponseCode": "AG_20191219_000043fdf61864fe9ff5",
"RequestID": "16738-27456357-1",
"ResponseDescription": "QR Code Successfully Generated.",
"QRCode": "iVBORw0KGgoAAAANSUhEUgAAASwAAAEsAQAAAABRBrPYAAAD"
}