DEV Community

Sospeter Mong'are
Sospeter Mong'are

Posted on

Safaricom Daraja API: Dynamic QR Code API Generation Guide

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.

Mpesa QR Code

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

  1. Access Token: Generate a token by using the Authorization API.
  2. 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

  1. Create a New POST Request:

    • Use POST as the method.
    • Enter the endpoint URL:
     https://sandbox.safaricom.co.ke/mpesa/qrcode/v1/generate
    
  2. Add Authorization Header:

    • Under Headers, add:
      • Key: Authorization
      • Value: Bearer YOUR_ACCESS_TOKEN
    • Replace YOUR_ACCESS_TOKEN with the token from the Authorization API.

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"
}
Enter fullscreen mode Exit fullscreen mode

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..."
}
Enter fullscreen mode Exit fullscreen mode

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..."

API Request and Response

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">
Enter fullscreen mode Exit fullscreen mode

Important Notes

  1. Token Expiry: The access token expires every 3600 seconds, so renew it as needed.
  2. 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.
  3. 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)