Error Handling
All API errors follow a consistent structure for programmatic handling. Parse type and code to determine what happened and how to recover.
Error Response Structure
Error Types
Each type maps to an HTTP status code:
Common Error Codes
Authentication & Authorization
Cart Creation (POST /carts)
Checkout (POST /orders)
General
Retry Strategy
Safe to Retry
- 5xx errors — server-side issues are usually transient. Retry with exponential backoff (1s, 2s, 4s, max 3 attempts).
- 429 Rate Limit — wait for the
Retry-Afterheader value before retrying. - Network timeouts on
POST /orders— always safe when using anIdempotency-Key. The server deduplicates by key, so retrying the same request with the same key will return the existing order if one was created.
Not Safe to Retry (Without Changes)
- 400 errors — fix the request before retrying (wrong field value, missing field, invalid quantity).
- 401/403 errors — fix authentication or permissions.
- 409 Conflict — state has changed; re-fetch the resource and adapt.
- 410 Gone — resource is permanently unavailable; start over (e.g., create a new cart).
Idempotency Keys
POST /orders requires an Idempotency-Key header. This makes retries safe:
- Generate a unique key per purchase attempt (format:
idem_prefix + random string) - Reuse the same key when retrying the same checkout
- Same key + same
cartIdwithin 24 hours → returns the existing order - Same key + different
cartId→ returns409 Conflict - Keys expire after 24 hours
Forward Compatibility
The API may introduce new error codes and types at any time. To avoid breaking your integration:
- Switch on
typefor broad error handling (authentication vs validation vs server error) - Switch on
codefor specific recovery logic (e.g.,CART_EXPIRED→ create new cart) - Use a default/fallback case for unknown
typeandcodevalues - Display
messageto users but never parse it programmatically — it may change