Idempotency-Key makes the retry safe, so you never place the same order twice.
The API may change until launch. Every change is listed in the Changelog.
Send a key with every order
An API key must send anIdempotency-Key header with every POST /api/v1/orders. Without one, the order is refused with 400 IDEMPOTENCY_KEY_REQUIRED.
- Generate a new random value for each order, such as 32 hexadecimal characters.
- Send exactly one value, 1 to 255 bytes, with no spaces around it. Anything else is refused with
400 IDEMPOTENCY_KEY_INVALID. - Store the key with your record of the order before you send the request, so a retry after a crash can reuse it.
Replay: same key, same body
Send the same body with the same key again and you get the first answer back. No second order is placed. The body is compared by its content, so whitespace and key order do not matter.Output
QUOTE_EXPIRED or QUOTE_ALREADY_ORDERED.
Conflict: same key, different body
Reusing a key with a different body is refused with409 IDEMPOTENCY_CONFLICT, and nothing is placed. Here the same quote with a minReceive added counts as a different body:
Output
When to retry with the same key
If you have lost the key itself, recover by quote.
Recover an order by its quote
GET /api/v1/orders/by-quote/{quoteId} tells you what a quote produced. It never places anything, and it answers whether or not the quote has expired. The key needs Read orders.
An unknown quote id, or one from another workspace, answers
404 QUOTE_NOT_FOUND. If the read itself fails, it answers 503 QUOTE_ORDER_UNAVAILABLE: do not place another order until a read succeeds.
What never to do
- Never retry an order whose outcome is unknown with a new key. A new key is a new order. Retry with the same key, or read by quote first.
- Never reuse a key for a different order. It is refused, and it hides which order the key belongs to.
- Never treat
unknownor a failed read as “not ordered”. OnlynotOrderedmeans nothing was placed. - Never take
QUOTE_ALREADY_ORDEREDas a failure. It means another order, sent with a different key, already used the quote. Read by quote to find it.