Retry requests safely with idempotency keys
If a request times out or your connection drops, you may not know whether your trade or payment order was created. Idempotency keys let you retry the request without creating a duplicate.
An idempotency key is a unique value you send in the optional x-idempotency-key header. When the API sees a key it has already processed, it returns the original response instead of carrying out the operation again.
This feature is optional and backward compatible. If you don't send the header, requests and responses work exactly as they do today.
Supported operations
| Operation | Endpoint |
|---|---|
| Book Trade | POST /api/v1/trade |
| Submit Payment Order | POST /api/v1/paymentorder |
Send an idempotency key
- Generate a new key for each new operation. We recommend a UUID (v4).
- Send it in the
x-idempotency-keyheader. - If you need to retry, send the same key and the same request body.
POST /api/v1/trade
Content-Type: application/json
Authorization: Bearer <token>
x-idempotency-key: 3f8b6c2e-9a41-4d7e-b5a0-1c2d3e4f5a6b
{ ...trade request body... }Key requirements
- Length: 1 to 255 characters. A key that is too long returns
400withIDM00003. - Scope: keys are unique per client. A key you've used on one endpoint can't be used for a different operation on either endpoint.
What happens when you retry
| Result of the original request | What the API returns on retry | What to do |
|---|---|---|
| Succeeded | The original status code and response body, including the same fxId. Nothing new is created. | Use the returned response. |
| Still processing | 409 Conflict | Wait, then retry with the same key. |
Failed with a server or rate-limit error (429 or 5xx) | The request is processed again, including validation and execution. | Retry with the same key and body. |
Failed validation (400) | The same request returns the same error. A corrected request sent with the same key returns 409 with IDM00001. | Fix the request, then send it with a new key. |
A replayed response looks exactly like the original. The API doesn't add a header or flag to show that a response is a replay.
Idempotency errors
The API returns these errors when a key is invalid, or when you reuse a key with request details that don't match the original. Don't retry these errors automatically. Sending the same request again returns the same error.
| Status | Error code | Applies to | When it happens | What to do |
|---|---|---|---|---|
400 | IDM00003 | Book Trade | The x-idempotency-key header is empty or longer than 255 characters. | Send a key of 1 to 255 characters. A UUID (36 characters) always fits. |
409 | IDM00001 | Book Trade | The key was already used for a request with different booking details or session. This usually happens when the original request failed validation (for example, an invalid quote ID), and you fixed the request but kept the same key. | Retry with the original details. If you meant to send a corrected or new request, first check whether the original request created a trade, then use a new key. |
409 | IDM00005 | Book Trade | The key was already used for a trade with a different quote ID. | To book a new trade, use a new key. To retry the original trade, send its original quote ID. |
Example response:
{
"type": "https://docs.payments.stonex.io/reference/error-codes",
"title": "Conflict",
"status": 409,
"detail": "This idempotency key was already used for another trade with a different quote. To book a new trade, use a new idempotency key. To retry the original trade, send its original quote id.",
"traceId": "00-7768b49ae068ef1a7175a6a48a264b0e-6c89f76da98d4e9a-99",
"errorCode": "IDM00005"
}Check
errorCode, not just the status code. A request that is still processing also returns409 Conflict. If your client retries every409automatically, it will keep retryingIDM00001andIDM00005and never succeed. Retry a409only when it's the still-processing case.
Rules for reusing keys
- Keys expire 24 hours after the first request. Retries don't extend this window. After it ends, a request with that key is treated as a new operation and could create a duplicate.
- Never change the request when you retry. The API rejects some changes with the errors above, but it doesn't check every field. If a change isn't caught, the API returns the original response and ignores your new request without an error.
- One key, one operation. Never reuse a key for a different trade or payment order.
- Keep the key and body until you know the outcome.
If you're not sure a request succeeded
If the request times out, the connection drops, or you receive 429, 500, 502, 503 or 504:
- Keep the original key and request body.
- Retry with the same key. Use exponential backoff, for example waiting 1, 2, 4 and then 8 seconds. The API doesn't send a
Retry-Afterheader, client sets its own wait time. - If you receive
409 Conflictbecause the request is still processing, wait and retry again with the same key. If theerrorCodeisIDM00001orIDM00005, stop retrying and follow Idempotency errors. - Once you have an
fxId, use Get Status to track the trade or payment order.
Don't generate a new key just because you didn't get a response. The API treats a new key as a new operation, so it could create a second trade or payment.
Updated 2 days ago

