STK push
Start an M-Pesa STK push for a payment.
STK push
Prepaid accounts need a service-token balance before the push (see 402 / INSUFFICIENT_PREPAID). Postpaid accounts are not blocked by that check. The fee is debited on a successful payment.
POST
https://bluepay.co.ke/api/stk_push.php
Content-Type: application/json*
Body
| Field | Required | Notes |
|---|---|---|
api_secret | Yes | API secret from API Keys. |
account_id | Yes | Account ID from API Keys (BP-XXXX-XXXX). |
channel_id | Yes | Alias payment_channel_id. Channel ID from API Keys (e.g. BP23356). |
phone | Yes | Alias phone_number. Kenya MSISDN. |
amount | Yes | KES 1–250000. |
account_reference | No | Payment reference (invoice id). Max 64; prefix rules apply. Omitted → BluePay generates ref. Also external_reference. |
csrf | Session only | With dashboard cookie, no API key in body. |
provider | No | m-pesa or omit. |
callback_url | No | Webhook for this STK. Otherwise the Account callback URL is used. Safaricom posts to BluePay only. |
Request & success examples
{
"api_secret": "YOUR_API_SECRET",
"account_id": "BP-XXXX-XXXX",
"channel_id": "BP23356",
"phone": "254712345678",
"amount": 100,
"account_reference": "BPINV-2048"
}
200
{ "ok": true, "stk_request_id": 1, "checkout_request_id": "ws_CO_...", "account_reference": "BP-LINK-A1B2C3D4" }
Error responses (400–502)
400 / 405
{ "ok": false, "success": false, "error": "Invalid JSON body", "message": "Invalid JSON body" }
{ "ok": false, "success": false, "error": "Method not allowed", "message": "Method not allowed" }
401 / 403
{ "ok": false, "success": false, "error": "Unauthorized", "message": "Send api_secret, account_id, and channel_id.", "hint": "See /public/docs.php#authentication" }
{ "ok": false, "success": false, "error": "Invalid CSRF token", "message": "Invalid CSRF token" }
422
{ "ok": false, "success": false, "error": "…", "message": "…" }
402
Branch on error_code: INSUFFICIENT_PREPAID + required_kes / balance_kes.
{
"ok": false,
"success": false,
"error": "402 Insufficient service tokens",
"message": "402 Insufficient service tokens",
"error_code": "INSUFFICIENT_PREPAID",
"required_kes": 5.5,
"balance_kes": 0,
"fee_kes": 5.5,
"tier_fee_kes": 3,
"sms_fee_kes": 2.5,
"sms_included": true,
"unpaid_kes": 0
}
Prepaid accounts only. sms_included is true when the payment-received SMS (KES 2.50) is part of fee_kes. Postpaid accounts skip this check.
error_code (pay-default / validation)
| Code | When |
|---|---|
INSUFFICIENT_PREPAID | 402 — top up service tokens. |
REFERENCE_PREFIX_MISMATCH | Pay-default — ref prefix vs channel required_prefix. |
INVALID_PHONE | MSISDN not normalizable to 254… |
CHANNEL_NOT_AVAILABLE | Channel inactive or merchant inactive. |
ACCOUNT_REFERENCE_INVALID | Ref failed M-Pesa rules. |
STK_VALIDATION_FAILED | Pay-default — missing fields. |
502
{ "ok": false, "success": false, "error": "…", "message": "…", "detail": null, "stk_request_id": 1 }
Non-production: detail may include provider message.
Code samples (cURL, PHP, Node)
curl -sS -X POST 'https://bluepay.co.ke/api/stk_push.php' \
-H "Content-Type: application/json" \
-d '{"api_secret":"YOUR_API_SECRET","account_id":"BP-XXXX-XXXX","channel_id":"BP23356","phone":"2547…","amount":100,"account_reference":"BPINV-2048"}'
<?php
$body = [
'api_secret' => 'YOUR_API_SECRET',
'account_id' => 'BP-XXXX-XXXX',
'channel_id' => 'BP23356',
'phone' => '2547…',
'amount' => 100,
'account_reference' => 'BPINV-2048',
];
$ch = curl_init('https://bluepay.co.ke/api/stk_push.php');
curl_setopt_array($ch, [
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
],
CURLOPT_POSTFIELDS => json_encode($body),
CURLOPT_RETURNTRANSFER => true,
]);
$response = curl_exec($ch);
curl_close($ch);
const res = await fetch("https://bluepay.co.ke/api/stk_push.php", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
api_secret: "YOUR_API_SECRET",
account_id: "BP-XXXX-XXXX",
channel_id: "BP23356",
phone: "2547…",
amount: 100,
account_reference: "BPINV-2048",
}),
});
console.log(res.status, await res.json());