Skip to main content

Dynamic QR Code

Generate and manage dynamic M-Pesa QR codes for payments and transfers. Dynamic QR lets you create a QR payload for a single transaction that includes amount, reference and recipient details.

User Stories

  • As a fintech product owner, I want to programmatically generate dynamic QR codes so that customers can easily make payments.
  • As an integrations developer, I want a simple client and clear webhook callbacks so I can implement reliable end-to-end flows with minimal boilerplate.
  • As a billing operations engineer, I want result and timeout notifications with acknowledgements so I can reconcile transactions and trigger retries or alerts when needed.
  • As a reseller partner, I want a tested SDK and examples so I can onboard quickly and reduce integration defects.

Parameters

ParameterTypeDescription
MerchantNamerequired
str
StringMerchant or business name shown on the QR experience (e.g., store name).
RefNorequired
str
StringTransaction reference or account identifier (e.g., invoice or order id).
Amountrequired
int
IntegerAmount in KES. Must be greater than zero.
TrxCoderequired
str
StringTransaction type code. Supported values: BG, WA, PB, SM, SB.
CPIrequired
str
StringCredit Party Identifier: mobile number, paybill, till or business identifier depending on TrxCode.
Sizerequired
str
StringQR image size in pixels (square). Example: '300'.

Integration Overview

Two integration patterns: use the library-level client (recommended) for quick setup or the lower-level service for custom workflows.

Quick Start (Python)

Python
# Example: using MpesaClient facade to create a dynamic QR
from mpesakit import MpesaClient
from mpesakit.dynamic_qr_code import DynamicQRTransactionType
client = MpesaClient(
consumer_key="YOUR_KEY",
consumer_secret="YOUR_SECRET",
environment="sandbox",
)
response = client.dynamic_qr.generate(
merchant_name="Corner Store",
ref_no="ORDER-1001",
amount=250,
trx_code=DynamicQRTransactionType.BUY_GOODS,
cpi="373132",
size="300",
)
if response.is_successful():
print("QR generated", response.QRCode)
else:
print("Failed:", response.ResponseDescription)

Response

  • Response objects are typed models with attributes matching the API response fields.
  • Use is_successful() to check if the request succeeded.
  • If you need the raw response data, use model_dump(mode="json") to get a dictionary representation.
Sample Response
{
"ResponseCode": "00",
"ResponseDescription": "QR Code generated successfully",
"QRCode": "iVBORw0KGgoAAAANSUhEUgAA..."
}
Python
# Check success and extract QR payload
if response.is_successful():
payload = response.QRCode
# store or render payload as needed
else:
print("Failed to generate QR:", response.ResponseDescription)

Next Steps