error.code as the stable, machine-readable field and error.message as human-facing guidance that may evolve.
Error envelope
HTTP status codes
4xx responses indicate the caller needs to change something before retrying; 5xx responses indicate Foil’s side needs to recover. The
error.retryable field encodes this directly and is safe to key retry logic off.
Error code taxonomy
Error codes are namespaced with a category prefix. The prefix tells you which layer rejected the request; the suffix tells you specifically what went wrong.
Branch your error handling on the category prefix first, the specific code second. A generic “auth failure” path that handles any
auth.* code is usually correct, with specific codes adding nuance only where your UX demands it (e.g. handling auth.origin_not_allowed by reminding the developer to add their domain to the key’s allowed origins, or auth.insufficient_scope by pointing at the missing scope).
The browser SDK surfaces its own client-side codes - config.* and runtime.*, plus transport.upgrade_required - documented alongside the SDK. See Browser SDK → Error codes for the full table, including retry semantics and the recommended fallback policy.
Details field
Some error codes include a structureddetails object with extra context.
Field errors (request.validation_failed)
fields[] entry carries:
name- dot-path to the offending field in the request body (or query)issue- a stable short code describing why the value was rejectedexpected- an optional human-readable description of what’s validreceived- the value the server saw (redacted for sensitive fields)
Next action hints
Some errors include anext_action in details to make retry logic straightforward:
For everything else, key your retry logic off
error.retryable and the HTTP status - see Retry strategy.
Enumerated fields
When a field only accepts a fixed set of values, the rejected field’sexpected lists them.
Rate limiting
Rate-limited responses return429 Too Many Requests with:
Retry-Afterheader - seconds until the next acceptable retryX-RateLimit-Limit- the ceiling for this organization and key typeX-RateLimit-Remaining-0at the point of rejection
Retry-After. Don’t retry inside the window - further requests count against the limit and typically extend the cooldown. For bulk workflows, the server SDKs include built-in retry-with-backoff that honors this header.
See Authentication → Rate limits for organization-level defaults.
Using request_id
Every response (success and failure) carries a request_id in the meta block for successful responses, and in error.request_id for failures. Include it verbatim when you:
- Email
security@usefoil.comabout a suspected security issue - Open a support ticket about an unexpected error
- Correlate a client-side report with server-side logs
req_0123456789abcdef0123456789abcdef (32 hex characters after the prefix). It’s unique per request and never reused.
Handling errors in the server SDKs
Each server SDK throws structured error types that mirror the envelope above. The Node SDK’s types illustrate the pattern every SDK follows.Node.js
All expose equivalent fields -
status, code, request_id, field_errors, docs_url, and the raw response body.
Retry strategy
A minimal retry loop that handles the common cases:- Read
error.retryable. Iffalse, don’t retry - the request will fail the same way every time. - If the status is
429, wait forRetry-Afterseconds before retrying. - If the status is
5xx, retry with exponential backoff (e.g. 0.5s, 1s, 2s, 4s, capped at 30s) with jitter. - Cap total retries at 3–5 attempts before surfacing the error to the caller.
- Always log
error.request_idon the last attempt so support can correlate.
What’s next
Authentication
Key types, scopes, and lifecycle.
Pagination
Iterating through list endpoints.
API introduction
Surface-level summary of the API.
Troubleshooting
Operational guidance when things go wrong.