daraja-openapi

API reference / Dynamic QR

Generate an M-PESA payment QR code

POST/mpesa/qrcode/v1/generate

Checked 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

SDKMethod
TypeScriptdynamicQr()
Pythondynamic_qr()
PHPdynamicQr()
KotlindynamicQr()
SwiftdynamicQR()

Request

EnvironmentURL
Sandboxhttps://sandbox.safaricom.co.ke/mpesa/qrcode/v1/generate
Productionhttps://api.safaricom.co.ke/mpesa/qrcode/v1/generate

Send the access token as Authorization: Bearer <token>, and the body as JSON.

FieldTypeDescription
MerchantNamestringRequired
RefNostringRequiredThe account number or reference shown to the customer.
AmountintegerRequiredAt least 1.
TrxCodestringRequiredBG 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.
CPIstringRequiredThe till, agent, paybill or phone number to pay.
SizestringRequiredWidth 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.

FieldTypeDescription
ResponseCodestringRequired
RequestIDstring
ResponseDescriptionstringRequired
QRCodestringRequiredA PNG image, base64-encoded.

Errors

StatusMeaning
400Daraja 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.
401Daraja 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.
500Daraja 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"
}

View the source of this page

Built by ByteGigs Labs. Apache 2.0. M-PESA, Daraja and Safaricom are trademarks of Safaricom PLC; this project is independent and not endorsed by Safaricom.