Skip to content

Collections

Charge a customer by M-Pesa prompt: rules, statuses, what you receive, and sandbox numbers for every outcome.

A collection charges a customer by M-Pesa. Their phone rings with an M-Pesa prompt for the amount; when they enter their PIN, the money arrives in your workspace balance as USDC. Use it for checkout in a web shop or app, invoices, subscriptions you bill yourself, or anything where your customer pays you. The endpoint details are in the Collections reference.

Collections need a key with collections:write (to charge) and collections:read (to look them up). The collections key preset in the Console has both.

Charging a customer

curl -X POST "https://api.autopayke.com/api/v1/collections" \
  -H "Authorization: Bearer $AUTOPAYKE_API_KEY" \
  -H "Idempotency-Key: order-1042-charge" \
  -H "Content-Type: application/json" \
  -d '{
    "amount": 150000,
    "currency": "KES",
    "payer": { "phone": "+254700000001", "name": "Jane Wanjiku" },
    "reference": "order_1042",
    "description": "Order 1042"
  }'
  1. We check the Idempotency-Key, the key's scope, whether collections are paused, the number and the amount.
  2. We check your daily and monthly limits, and how often this phone was prompted recently.
  3. We ring the customer's phone and answer at once with the collection, status pending.
  4. The customer enters their M-Pesa PIN (or cancels, or ignores it). The result arrives by webhook, usually within a minute.

Rules

  • amount is in KES cents, whole shillings only: 150000 is KES 1,500. One prompt collects KES 10 to KES 150,000.
  • payer.phone must be a Safaricom M-Pesa number in any common format (+254712345678, 0712345678). Airtel numbers get PAYER_INVALID.
  • The customer has 10 minutes to answer before the collection expires. Create a new collection (with a new Idempotency-Key) to ask again.
  • To protect people from spam, one phone can get at most 3 prompts in 10 minutes and 10 a day, across every business on AutoPayKe. Past that you get PAYER_PROMPT_LIMIT.
  • Collections count towards your workspace's daily and monthly limits (LIMIT_DAILY_EXCEEDED, LIMIT_MONTHLY_EXCEEDED).

Statuses

pending ──► succeeded
   │
   ├──► failed    (cancelled, wrong PIN, not enough money)
   └──► expired   (not answered within 10 minutes)
StatusFinal?MeaningWebhook
pendingNoThe prompt was sent; waiting for the customer.collection.pending
succeededYesPaid. credited_usdc reached your balance; mpesa_receipt is the customer's M-Pesa code.collection.succeeded
failedYesNot paid. failure_code says why: PAYER_CANCELLED or INSUFFICIENT_FUNDS.collection.failed
expiredYesNot answered in time (PROMPT_EXPIRED).collection.expired
Only ship goods or unlock a service on collection.succeeded (or a GET that says succeeded). Never on the create response, and never on what your own page or app shows the customer.

What you receive

The customer pays the KES amount. It is converted to USDC at the rate shown on the collection, the provider's fee (fee_usdc) is taken off, and the rest (credited_usdc) lands in your balance. From there you can hold it, pay it out with a payout, or move it on-chain.

Testing in the sandbox

With an apk_test_ key no phone rings. The number decides the outcome about 5 seconds later, and a success adds the credited USDC to your sandbox balance, so you can test collect-then-pay-out end to end.

payer.phoneOutcome
+254700000001succeeded
+254700000002failed, PAYER_CANCELLED
+254700000003failed, INSUFFICIENT_FUNDS
+254700000004expired, PROMPT_EXPIRED
Any other Safaricom numbersucceeded

To force an outcome right away, call POST /api/v1/sandbox/collections/{id}/simulate with { "outcome": "succeeded" | "cancelled" | "insufficient_funds" | "expired" }.