M-PESA API
Accept M-PESA payments via STK Push. Customers authorize payments directly on their phone.
Note
Payment Flow
- 1Your server calls the init-payment endpoint
- 2Customer receives STK Push prompt on their phone
- 3Customer enters M-PESA PIN to authorize
- 4Safaricom processes the payment
- 5Webhook notification sent to your callback URL
- 6Optionally query status using confirmPayment endpoint
/api/v1/init-paymentInitiate STK Push
Send an STK Push prompt to the customer's phone to authorize a payment.
| Parameter | Type | Description |
|---|---|---|
phoneNumberrequired | string | Customer phone number in format 254XXXXXXXXX Example: |
amountrequired | number | Amount to charge in KES Example: |
orderIdrequired | string | Your unique order/transaction reference Example: |
accountReferencerequired | string | Account reference shown on customer phone Example: |
transactionDesc | string | Transaction description Example: |
callbackUrl | string | URL to receive payment notification Example: |
Code Examples
const response = await fetch('https://api.serixpay.com/api/v1/init-payment', {
method: 'POST',
headers: {
'Authorization': 'Bearer pk_test_your_api_key',
'Content-Type': 'application/json'
},
body: JSON.stringify({
phoneNumber: '254712345678',
amount: 100,
orderId: 'ORDER-001',
accountReference: 'Payment',
transactionDesc: 'Order payment',
callbackUrl: 'https://your-site.com/webhook'
})
});
const data = await response.json();
console.log(data.checkoutRequestId);/api/v1/confirmPayment/:CheckoutRequestIDQuery Payment Status
Check the status of an STK Push payment using the checkoutRequestId.
| Parameter | Type | Description |
|---|---|---|
CheckoutRequestIDrequired | string | The checkoutRequestId from the init-payment response Example: |
Code Examples
const checkoutRequestId = 'ws_CO_01012024123456789';
const response = await fetch(
`https://api.serixpay.com/api/v1/confirmPayment/${checkoutRequestId}`,
{
method: 'POST',
headers: {
'Authorization': 'Bearer pk_test_your_api_key',
'Content-Type': 'application/json'
}
}
);
const data = await response.json();
console.log(data.status); // 'completed', 'pending', or 'failed'/api/v1/transactionsList Transactions
Retrieve a paginated list of M-PESA transactions for your organization.
Query Parameters
| Parameter | Type | Description |
|---|---|---|
page | number | Page number for pagination |
limit | number | Items per page (max 100) |
status | string | Filter by status: completed, pending, failed |
startDate | string | Filter from date (ISO 8601) |
endDate | string | Filter to date (ISO 8601) |
Transaction Statuses
Webhook Payload
When a payment completes, we send a webhook to your callback URL:
{
"event": "payment.completed",
"timestamp": "2024-01-15T10:30:00Z",
"data": {
"orderId": "ORDER-001",
"checkoutRequestId": "ws_CO_01012024123456789",
"amount": 100,
"currency": "KES",
"status": "completed",
"method": "mpesa",
"mpesaReceiptNumber": "ABC123XYZ",
"phoneNumber": "254712345678",
"transactionDate": "2024-01-15T10:30:00Z"
}
}