Idempotency
Retry a request safely without creating a second payment or refund.
A network error can leave you not knowing whether a request reached us. The Idempotency-Key header makes a retry safe: the same key with the same body always gives the first answer, and never creates a second payment.
Where it is needed
The header is required on every call that creates something:
| Call | Creates |
|---|---|
POST /v1/payments | a payment |
POST /v1/payments/{id}/refunds | a refund |
POST /v1/billing/plans | a plan |
POST /v1/billing/subscriptions | a subscription |
Without it you get 400 idempotency_key_required. The key is a string of up to 255 characters. Other POST calls (check, new QR, simulate, pause, resume, cancel, test event, resend) do not take it.
The rules
| You send | You get |
|---|---|
| A new key | The request runs and its answer is saved under the key. |
| The same key and the same body, after the first answer | The first answer again, with the same status, and the header Idempotent-Replayed: true. Nothing new is created. |
| The same key and the same body, while the first is still running | 409 idempotency_in_progress. Retry after a moment. |
| The same key with a different body, or on a different call | 422 idempotency_mismatch. |
The same key after a 5xx answer | The request runs again: server errors are not saved. |
- Keys are per business and per mode: test and live never share them. The same key sent with another app's API key counts as a different request (
422 idempotency_mismatch). - A refusal of the request itself is saved like a success. If the first request was refused (for example
400 invalid_amount), fix it and send it with a new key. Refusals of the key or the rate (401,403,429) come before the key is looked at and are not saved: retry them with the same key. - The body of these calls is at most 64 KB (
413 body_too_large).
Choosing keys
Use one key per thing you mean to create, and keep it with your own record, so a retry after a crash reuses it:
- a random UUID made when you create the order's payment and saved with the order, or
- a key made from your own ids, such as
order-1042-pay-1orrefund-1042-1.
When you want a new payment for the same order (the first one expired or failed), use a new key.
curl https://pay.piti.id/v1/payments \
-H "Authorization: Bearer lbp_test_xxxxxxxx" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: 6f1c2a9e-3b7d-4c55-9a1e-0d2f8b7c4e31" \
-d '{"amount": 150000, "reference": "INV-1042"}'502 payment_unavailable
This means the QR or virtual account could not be made. Nothing was charged and no customer saw it. Create a new payment with a new Idempotency-Key, or try another method.
See Errors for what each code means.