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: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"
}'- We check the Idempotency-Key, the key's scope, whether collections are paused, the number and the amount.
- We check your daily and monthly limits, and how often this phone was prompted recently.
- We ring the customer's phone and answer at once with the collection, status
pending. - The customer enters their M-Pesa PIN (or cancels, or ignores it). The result arrives by webhook, usually within a minute.
Rules
amountis in KES cents, whole shillings only:150000is KES 1,500. One prompt collects KES 10 to KES 150,000.payer.phonemust be a Safaricom M-Pesa number in any common format (+254712345678, 0712345678). Airtel numbers getPAYER_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)| Status | Final? | Meaning | Webhook |
|---|---|---|---|
pending | No | The prompt was sent; waiting for the customer. | collection.pending |
succeeded | Yes | Paid. credited_usdc reached your balance; mpesa_receipt is the customer's M-Pesa code. | collection.succeeded |
failed | Yes | Not paid. failure_code says why: PAYER_CANCELLED or INSUFFICIENT_FUNDS. | collection.failed |
expired | Yes | Not answered in time (PROMPT_EXPIRED). | collection.expired |
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.phone | Outcome |
|---|---|
+254700000001 | succeeded |
+254700000002 | failed, PAYER_CANCELLED |
+254700000003 | failed, INSUFFICIENT_FUNDS |
+254700000004 | expired, PROMPT_EXPIRED |
| Any other Safaricom number | succeeded |
To force an outcome right away, call POST /api/v1/sandbox/collections/{id}/simulate with { "outcome": "succeeded" | "cancelled" | "insufficient_funds" | "expired" }.