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

OperationEndpoint
Book TradePOST /api/v1/trade
Submit Payment OrderPOST /api/v1/paymentorder

Send an idempotency key

  1. Generate a new key for each new operation. We recommend a UUID (v4).
  2. Send it in the x-idempotency-key header.
  3. 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 400 with IDM00003.
  • 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 requestWhat the API returns on retryWhat to do
SucceededThe original status code and response body, including the same fxId. Nothing new is created.Use the returned response.
Still processing409 ConflictWait, 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.

StatusError codeApplies toWhen it happensWhat to do
400IDM00003Book TradeThe 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.
409IDM00001Book TradeThe 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.
409IDM00005Book TradeThe 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 returns 409 Conflict. If your client retries every 409 automatically, it will keep retrying IDM00001 and IDM00005 and never succeed. Retry a 409 only 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:

  1. Keep the original key and request body.
  2. 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-After header, client sets its own wait time.
  3. If you receive 409 Conflict because the request is still processing, wait and retry again with the same key. If the errorCode is IDM00001 or IDM00005, stop retrying and follow Idempotency errors.
  4. 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.


Did this page help you?