Idempotent Requests
Safely retry API requests without risk of duplicate operations using idempotency keys.
KingsPay supports idempotency for safely retrying requests without accidentally performing the same operation twice. When creating or updating an object, you can supply an idempotency key so that if a connection error or timeout occurs, you can safely repeat the request without risk of creating a second object or performing the update twice.
To perform an idempotent request, include an Idempotency-Key header with your request.
curl -X POST "https://api.kingsplatform.net/api/api/v1/payments/charges" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: your-unique-key" \
-d '{ ... }'How it works
KingsPay saves the resulting status code and body of the first request made for any given idempotency key, whether it succeeds or fails. Subsequent requests with the same key return the same saved result, including validation errors such as 400 Bad Request. To retry after fixing the request, use a new key.
Two results are not saved, so you can retry them with the same key:
429 Too Many Requests- An unexpected server error that ends the request
The idempotency layer also compares the method, path and body to those of the original request. If they differ, it returns 409 Conflict with the code idempotency_key_reused.
If you send a request with an idempotency key while a previous request using the same key is still in progress, KingsPay returns a 409 Conflict immediately:
{
"status": "error",
"error": {
"type": "conflict_error",
"code": "request_in_progress",
"message": "A request with this idempotency key is already in progress.",
"field": null
}
}Wait for the original request to complete before retrying with the same key.
Keys are automatically pruned after they are at least 24 hours old. If a key is reused after it has been pruned, the request is treated as a new one.
Send Idempotency-Key on POST and DELETE requests. Do not send it on GET requests: they only read data, and a key would replay the same saved response for 24 hours instead of returning current data.
Generating idempotency keys
How you create unique keys is up to you. We recommend using V4 UUIDs or another random string with sufficient entropy to avoid collisions.
Tying the key to your own order or reference ID is a common pattern — it ensures retries from the same logical operation naturally carry the same key:
Idempotency-Key: order_7f3a1c2d-charge-attempt-1Best practices
- Always set a key for charge and payout requests. These are the operations where duplicates are most harmful.
- Use a stable, request-scoped identifier. A V4 UUID generated once per operation attempt works well.
- Do not reuse keys across different operations. Each distinct operation should have its own unique key.
- Store the key before sending the request. Generate and persist the key before making the call so that if your process crashes mid-flight, you can retry with the same key.