BluePayAPI
Ask AI
BluePay
API Keys Support Dashboard

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

FieldRequiredNotes
api_secretYesAPI secret from API Keys.
account_idYesAccount ID from API Keys (BP-XXXX-XXXX).
channel_idYesAlias payment_channel_id. Channel ID from API Keys (e.g. BP23356).
phoneYesAlias phone_number. Kenya MSISDN.
amountYesKES 1–250000.
account_referenceNoPayment reference (invoice id). Max 64; prefix rules apply. Omitted → BluePay generates ref. Also external_reference.
csrfSession onlyWith dashboard cookie, no API key in body.
providerNom-pesa or omit.
callback_urlNoWebhook 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)

CodeWhen
INSUFFICIENT_PREPAID402 — top up service tokens.
REFERENCE_PREFIX_MISMATCHPay-default — ref prefix vs channel required_prefix.
INVALID_PHONEMSISDN not normalizable to 254…
CHANNEL_NOT_AVAILABLEChannel inactive or merchant inactive.
ACCOUNT_REFERENCE_INVALIDRef failed M-Pesa rules.
STK_VALIDATION_FAILEDPay-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());