The Dynamic QR Code API on Safaricom’s Daraja platform provides a way for businesses to generate QR codes for M-PESA transactions. This allows customers to scan the QR code in the My Safaricom App or M-PESA app to automatically capture the till number and transaction amount, making payments seamless at select Lipa Na M-Pesa outlets. This guide provides instructions for integrating the Dynamic QR API, along with request and response examples.
Endpoint Overview
-
Method:
POST
-
URL:
https://sandbox.safaricom.co.ke/mpesa/qrcode/v1/generate
- Authentication: Requires a Bearer token generated via the Authorization API.
Prerequisites
- Access Token: Generate a token by using the Authorization API.
- Registered Application: Set up your application on the Daraja platform to access your Consumer Key and Consumer Secret.
Guide to Generating a Dynamic QR Code
Step 1: Set Up the Request in Postman
-
Create a New POST Request:
- Use POST as the method.
- Enter the endpoint URL:
https://sandbox.safaricom.co.ke/mpesa/qrcode/v1/generate
-
Add Authorization Header:
- Under Headers, add:
-
Key:
Authorization
-
Value:
Bearer YOUR_ACCESS_TOKEN
-
Key:
- Replace
YOUR_ACCESS_TOKEN
with the token from the Authorization API.
- Under Headers, add:
Step 2: Define the Request Body
In the Body section, choose raw and set the format to JSON. Enter the following JSON payload:
{
"MerchantName": "TEST Business",
"RefNo": "Invoice Test",
"Amount": 1,
"TrxCode": "BG",
"CPI": "0708920430",
"Size": "300"
}
Request Parameters
Parameter | Description | Type | Example Value |
---|---|---|---|
MerchantName | Name of the merchant or company. | String | "TEST SUPERMARKET" |
RefNo | Reference number, such as an invoice ID. | String | "Invoice Test" |
Amount | Total transaction amount. | Numeric | 1 |
TrxCode | Transaction type, with options: - BG : Buy Goods- WA : Withdraw Cash- PB : Paybill- SM : Send Money- SB : Send to Business. |
String |
"BG" (Buy Goods) |
CPI | Credit Party Identifier, such as a till number or phone number, Business Number, Agent Till, Paybill or Business number, or Merchant Buy Goods. | String | "0708920430" |
Size | QR code image size in pixels (e.g., 200, 300). | String | "300" |
Step 3: Send the Request
Click Send to submit the request in Postman. A successful request will return a QR code as a base64-encoded string.
Example Response
Upon success, you’ll receive a response similar to the following:
{
"ResponseCode": "00",
"ResponseDescription": "The service request is processed successfully.",
"QRCode": "iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAYAA..."
}
Response Parameters
Parameter | Description | Type | Example Value |
---|---|---|---|
ResponseCode | Code indicating the transaction result. | String | "00" |
ResponseDescription | Description of the transaction status. | String | "The service request is processed successfully." |
QRCode | Base64-encoded string of the QR code image. | String | "iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAYAA..." |
Viewing the QR Code
To display the QR code, convert the base64 string from the QRCode
parameter into an image. Here’s an HTML example:
<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAASwAAAEsCAYAA..." alt="M-PESA QR Code">
Important Notes
- Token Expiry: The access token expires every 3600 seconds, so renew it as needed.
- Environment: This endpoint is available in both Sandbox and Production environments. Use the Sandbox URL for testing and switch to Production when ready to go live.
-
QR Code Size: The
Size
parameter defines the QR code dimensions, which will always be a square image.
By following these steps, you can integrate the Dynamic QR API and create a fast and secure payment process for your customers.
I welcome any suggestions, comments, or issues you encounter during implementation.
Happy Coding with M-Pesa Daraja API!
Top comments (0)