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. Both are available as sync and async variants.

Quick Start (Sync)

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)

Quick Start (Async)

Python
import asyncio
from mpesakit import AsyncMpesaClient
from mpesakit.dynamic_qr_code import DynamicQRTransactionType
async def main():
async with AsyncMpesaClient(
consumer_key="YOUR_KEY",
consumer_secret="YOUR_SECRET",
environment="sandbox",
) as client:
response = await 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)
asyncio.run(main())

Response

  • Response objects are typed models with attributes matching the API response fields, whether returned directly (sync) or via await (async).
  • 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.

Response Schema

DynamicQRGenerateResponse — returned by client.dynamic_qr.generate(...). This is a synchronous, single-call API with no result/timeout callbacks.

ParameterTypeDescription
ResponseCoderequired
str | int
String/Integer'00' indicates the QR code was generated successfully.
ResponseDescriptionrequired
str
StringHuman readable status description, e.g. 'QR Code Successfully Generated.'
QRCoderequired
str
StringBase64-encoded QR code image data.
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