Idempotency
Networks fail. A request can time out after IDEALX has already placed your order. To retry without placing it twice,
send an Idempotency-Key header: repeating a request with the same key returns the first result and never moves
money or places an order twice.
Which endpoints
Section titled “Which endpoints”| Endpoint | Header |
|---|---|
POST /v1/accounts/{id}/orders |
Required |
POST /v1/accounts/{id}/transfers |
Required |
POST /v1/accounts/{id}/fx |
Required |
POST /v1/accounts/{id}/yield |
Required |
POST /v1/firms/{firmId}/accounts/{accountId}/proposals |
Optional: a retry returns the same proposal |
Other writes are naturally safe to repeat: submitting a proposal that is already submitted, cancelling an order that
is already cancelled or approving what you already approved returns the current state or a 409 you can read.
Choosing a key
Section titled “Choosing a key”- 16 to 128 characters of letters, digits,
-or_. A UUID (8d0f4b5e-…) is ideal. - One key per action, not per request: generate it when you decide to act, store it with the action in your own database, and reuse it for every retry of that action.
- A new action gets a new key, even if it has the same body.
A key that does not fit the pattern answers 400 invalid_idempotency_key. Leaving the header out where it is required
answers 422 validation_failed with details.issues[0].path set to idempotency-key.
import { randomUUID } from "node:crypto";
async function placeOrder(accountId: string, body: object, key = randomUUID()) { for (let attempt = 0; attempt < 5; attempt++) { try { const res = await fetch(`${base}/v1/accounts/${accountId}/orders`, { method: "POST", headers: { Authorization: `Bearer ${process.env.IDEALX_API_KEY}`, "Content-Type": "application/json", "Idempotency-Key": key, // the same key on every attempt }, body: JSON.stringify(body), }); if (res.status < 500 && res.status !== 429) return res; // done, or a 4xx to handle const wait = Number(res.headers.get("Retry-After") ?? 2 ** attempt); await new Promise((r) => setTimeout(r, wait * 1000)); } catch { await new Promise((r) => setTimeout(r, 2 ** attempt * 1000)); // network error: retry } } throw new Error("Order not confirmed; retry later with the same key");}import timeimport uuidimport requests
def place_order(account_id: str, body: dict, key: str | None = None) -> requests.Response: key = key or str(uuid.uuid4()) # store it with the action, reuse it on every retry for attempt in range(5): try: res = requests.post( f"{base}/v1/accounts/{account_id}/orders", json=body, headers={"Authorization": f"Bearer {api_key}", "Idempotency-Key": key}, timeout=30, ) if res.status_code < 500 and res.status_code != 429: return res time.sleep(float(res.headers.get("Retry-After", 2**attempt))) except requests.RequestException: time.sleep(2**attempt) raise RuntimeError("Order not confirmed; retry later with the same key")