Use the Idempotency-Key header to safely retry POST requests without creating duplicate trades, actions, or accounts when networks fail or time out.
Idempotency keys let you retry POST requests safely. If a request fails due to a network error or timeout, you can resend it with the same key and get the original response back — without creating a duplicate.
Use UUIDv4 for unique keys, or derive them deterministically from your own identifiers:
import { randomUUID } from "crypto";// Random keyconst key = randomUUID();// Deterministic key (same input always produces the same key)const key = `trade-${accountId}-${symbol}-${side}-${Date.now()}`;
import uuid# Random keykey = str(uuid.uuid4())# Deterministic keykey = f"trade-{account_id}-{symbol}-{side}-{int(time.time())}"
Deterministic keys are useful when you want the same logical operation to always map to the same key — for example, “buy AAPL in portfolio X today” should only happen once regardless of how many times you retry.
Idempotency keys are cached for 24 hours. After that, the same key creates a new resource. Design your keys so that reuse after 24 hours either doesn’t happen or is intentional.