STK Push

Send the M-PESA PIN prompt (Lipa Na M-PESA Online) to a customer's phone, for an amount you set, paid into your paybill or till.

POST/api/v1/stk

Scope stk:initiate.

# Paste your API key once (it starts with mpk_):
export MYPAYMENTS_KEY="mpk_your_key_here"

curl -X POST https://api.mypayments.co.ke/api/v1/stk \
  -H "Authorization: Bearer $MYPAYMENTS_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "account": "5e2989df-905b-40e9-a056-8251c2e34a37",
    "phone": "254712345678",
    "amount": 100,
    "reference": "INV-001",
    "description": "WiFi 1hr",
    "tracking_id": "order-8842",
    "callback_url": "https://billing.example.co.ke/mypayments/hotspot"
  }'

Body

FieldTypeRequiredDescription
accountstringYesThe account id to collect into.
phonestringYesThe payer's Safaricom number as 2547XXXXXXXX or 2541XXXXXXXX: twelve digits, no +. 07…, 01… and +254… are converted. Phone format.
amountnumber | stringYesWhole shillings, 1 to 250,000 (the M-PESA per-transaction limit). M-PESA takes no cents: 100.50 is refused.
referencestringYesYour id for this payment: 1–12 characters, letters, digits, . _ -. Unique in your business. Shown to the customer as the account reference.
descriptionstringNoUp to 13 characters shown on the prompt. Defaults to "Payment".
tracking_idstringNoUp to 36 characters of your own: an order id, a session id. Comes back on the webhook and the lookup. Not unique; filter by it with GET /transactions?tracking_id=.
callback_urlstringNoSend this payment's webhook here instead of to your primary endpoint's URL. HTTPS, public. How it is signed.

Response

json
{
  "success": true,
  "data": {
    "transaction_uuid": "1d6aea1c-6911-455b-8300-2d347cc2a6e4",
    "reference": "INV-001",
    "status": "pending",
    "customer_message": "Success. Request accepted for processing"
  }
}

pending means Safaricom accepted the request and the prompt is on its way. It is not a payment. The customer may pay, cancel, or ignore it.

What happens next

  1. The customer enters their PIN, or cancels, or lets it time out (about a minute).
  2. Safaricom calls us back. We don't trust that callback: we query Safaricom for the result and compare the amount.
  3. The payment becomes settled (paid, confirmed) or failed, with Safaricom's result_code and result_desc.
  4. We send a payment.settled or payment.failed webhook.

Common failure codes: 1032 cancelled by the customer, 1037 no answer from the phone, 1 insufficient balance, 2001 wrong PIN.

Retrying safely

If your request times out, send it again with the same reference. We return the existing payment and never prompt the customer twice:

json
{
  "success": true,
  "data": {
    "transaction_uuid": "1d6aea1c-6911-455b-8300-2d347cc2a6e4",
    "reference": "INV-001",
    "status": "pending",
    "customer_message": "This reference was already initiated.",
    "already_exists": true
  }
}

To charge the same customer again, use a new reference (INV-001-2).

Per-request callback URL

Without callback_url, the payment goes to every webhook endpoint its filters match. With it, the URL you give replaces your primary endpoint's URL for this one payment, and the delivery is signed with the primary endpoint's secret, so the same verification code works. Other endpoints whose filters match still get their copy.

It needs a primary endpoint

The signing secret comes from your primary endpoint. With no endpoint configured, a callback_url delivery cannot be signed, so it is not sent; the delivery log on the payment says why. Add one endpoint in Dashboard → Webhooks first.

Errors

  • 400: a field is invalid (reference must be 1–12 characters), or the body has an unknown field.
  • 403: the key lacks stk:initiate, the account is disabled, or STK is not enabled for you on a shared shortcode.
  • 404: the account id is not one of yours.
  • 503: Safaricom refused or did not answer (often wrong Daraja keys or passkey on the account). The payment is recorded as failed; fix the cause and retry with a new reference.