Core concepts
Idempotency and duplicates
Networks time out. Two protections make sure a retry never charges your wallet twice.
The Idempotency-Key header
Every purchase and bulk request must include an Idempotency-Key header. Requests without one are rejected with IDEMPOTENCY_KEY_REQUIRED before anything is charged.
- Generate one key per order, for example a UUID. Keys can be up to 100 characters; a longer key is refused with
400 VALIDATION_ERROR. - If a request times out or you get no response, send exactly the same request again with the same key. You get the original result instead of a second purchase.
- Treat keys as single-use. They are remembered per account for 24 hours, across every endpoint and across test and live, so never reuse a key for anything else. After 24 hours the stored response expires, but a key that placed an order still answers
409 ORDER_ALREADY_PLACED. Only a key whose request placed no order (for example one refused with a4xx) runs again.
POST /api/v1/vtu/airtime/purchase HTTP/1.1
Authorization: Bearer pays_test_••••••••
Content-Type: application/json
Idempotency-Key: 6f1c2a7e-3b9d-4c55-9a51-0e7d2c4b8f10What happens on a retry
| Situation | Result |
|---|---|
| Same key, same body, first request finished | The stored response is returned (same status and body, with a fresh meta) with the header x-cache: HIT-IDEMPOTENCY. Nothing is charged again. It is a snapshot: to see an order's current status, get the order. |
| Same key, first request still running | Pay Sef waits up to 8 seconds for it to finish, then returns HTTP 409 IDEMPOTENCY_IN_FLIGHT. Retry later with the same key. A request that has been running for more than 2 minutes is treated as lost, and the retry runs instead. |
| Same key, first request failed with a 5xx or never answered, but the order was placed | HTTP 409 ORDER_ALREADY_PLACED with the order's order_id and order_ref in the body. Nothing is charged again; read that order. |
| Same key, different body | HTTP 409 IDEMPOTENCY_PAYLOAD_MISMATCH. The body is compared byte for byte, so even different spacing counts as a different request. |
| No key | HTTP 400 IDEMPOTENCY_KEY_REQUIRED. Nothing is charged. |
| Key over 100 characters | HTTP 400 VALIDATION_ERROR. Nothing is stored or charged. |
4xx errors are remembered too
The first response for a key is stored even when it is a 4xx error (for example a validation error or DUPLICATE_PURCHASE). After you fix the request, send it with a new key. A 5xx response is not stored: the key is released, so the same request with the same key runs again, and if the first attempt did place the order you get ORDER_ALREADY_PLACED rather than a second charge. After a timeout, check with verify or retry with the same key; never retry with a new key until you know no order exists.
Duplicate guard
Separately from idempotency keys, Pay Sef refuses a purchase of the same product for the same recipient (in the same mode, test or live) while an earlier one is recent and has not failed. This catches double clicks and loops that generate a new key each time. The amount is not compared, so ₦100 and then ₦500 of MTN airtime to the same number inside the window counts as a duplicate.
A refused duplicate returns HTTP 409 DUPLICATE_PURCHASE with retry_after_seconds in the body. In a bulk purchase the guard is checked line by line and refused lines are listed in 400 BULK_LINES_INVALID, with nothing charged. The window is 60 seconds by default. You can set it between 0 (off) and 300 seconds in Developer settings in the app. Keep it on unless you deliberately send the same top-up to the same recipient several times in a row.
Use both
The idempotency key protects you from your own retries; the duplicate guard protects you from your own bugs. A pending order is not a failure: check it with verify rather than buying again.