B2C Pay-Request API
Send money to customer phone numbers using Safaricom M-Pesa B2C. This comprehensive guide will walk you through every step of implementing B2C payments.
B2C Transfer Flow
Initiate B2C Transfer
Send money transfer request to customer's phone
Customer Receives SMS
Customer receives M-Pesa notification
Money Transfer
M-Pesa processes the money transfer
Callback Notification
Receive real-time transfer status updates
Step-by-Step Implementation Guide
Get Your API Credentials
Before you can use the B2C Pay-Request API, you need to obtain your API credentials:
- API Key: Your unique authentication token
- Wallet Balances: Ensure you have sufficient funds in both payment and service wallets
- M-Pesa Credentials: Your Safaricom M-Pesa B2C credentials (configured in the system)
Validate Phone Number Format
Ensure the phone number is in the correct format for M-Pesa B2C:
Calculate Required Balances
Before initiating a B2C transfer, calculate the required wallet balances:
Make the API Request
Send a POST request to the B2C endpoint with the required parameters:
Handle the Response
Process the API response and handle different scenarios:
Handle Callbacks
Set up your callback endpoint to receive real-time status updates:
Endpoint
payment_wallet (amount) and service_wallet (withdrawal fee).Request Parameters
| Parameter | Type | Required | Description | Example |
|---|---|---|---|---|
amount |
integer | Yes | The amount to send in KES (minimum 1, maximum 100,000) | 1000 |
phone_number |
string | Yes | Recipient's phone number (with or without country code) | "254712345678" |
command_id |
string | Optional | M-Pesa command type (default: "BusinessPayment") | "BusinessPayment" |
remarks |
string | Optional | Additional remarks for the transaction | "Payment for services" |
occasion |
string | Optional | Occasion or purpose of the payment | "Service Payment" |
external_reference |
string | Optional | Your unique reference for tracking this transaction | "PAY-001" |
callback_url |
string | Optional | URL to receive status callbacks (must be HTTPS) | "https://mysite.com/b2c-callback" |
- Amount must be between 1 and 100,000 KES
- Phone number will be automatically formatted to include country code
- External reference must be unique per transaction
- Callback URL must be a valid HTTPS endpoint
cURL Example
Error Handling
Success Response
Callbacks
Your callback endpoint will receive real-time updates about the transaction status:
initiated- Payment request sent to M-Pesacompleted- Payment successfully processedfailed- Payment failed (insufficient funds, invalid number, etc.)cancelled- Payment was cancelled by user or system