status, a code, a detail and a request_id:
Every API version sends
code and request_id. The fields are optional in the OpenAPI spec, so read them defensively: an approval refusal (below) and a 502 from the gateway can arrive without them, and a 401 for a missing or invalid API key has code and message in place of detail.
On a quote or subscription call that an approval stops, a 4xx body also has an approval object with approval.reason; a call whose own approval request waits answers 202 Accepted with the same body. See When a call needs approval.
Error codes
code names a specific error when the API has a code for it, and otherwise the category of the error.
Handle codes you do not know. New codes are added without a new API version, so a code you have not seen before is not a breaking change. Fall back to the HTTP status for any code you do not recognise.
Specific errors:
Categories:
Status codes
400 and 422 both indicate the request will not succeed as sent. Broadly, 400 means the request itself was malformed, and 422 means it parsed correctly but violates a rule — for example a value that is well-formed but not valid for the current state of the resource.Request IDs
Every response carries anX-Request-Id header — a UUID generated per request. An error body repeats it as request_id. Log it.
When you contact support about a failed request, quote the request ID. It is the fastest way for us to find the request — far quicker than a timestamp and an endpoint. The TypeScript SDK surfaces it as requestId on every error from a response.
Typed errors in the TypeScript SDK
The TypeScript SDK maps failed responses onto typed error classes, so you can branch withinstanceof rather than inspecting status codes by hand.
All of them extend
AlgunaError and carry:
message— the body’sdetail, elsetitle, elsemessage, else the HTTP status textstatusCode— the HTTP status (undefinedwhen no response arrived)requestId— theX-Request-Idof the failed responsecode— the body’scode, such asnot_foundorinvoice_already_issued, when the body carries onebody— the parsed response bodyheaders— the response headers, lower-cased (for exampleretry-afteron a429)
UnprocessableEntityError extends ValidationError, so one instanceof ValidationError check handles both 400 and 422.credits.check and credits.track throw RateLimitError: the credit gate stayed busy, or the ledger stayed at its limit of open holds, through every retry. Insufficient credit is not an error. Those calls return it as allowed: false.
Before it throws, the SDK retries a failed attempt twice: on 408, 429, 502, 503 and 504, on a 409 with Retry-After, and when no response arrived (a refused or reset connection, or a timed-out attempt). It waits with exponential backoff, or for Retry-After when the response has one, at most 60 s. Every request that is not a GET carries an Idempotency-Key, yours or a generated one, that its retries reuse, so the SDK never applies a write twice. events.ingest, credits.track and credits.check are the exceptions. The events in the request body identify the work, so the SDK sends no generated key for these calls and retries them without one. It still sends a key that you pass. See Writes identified by their events. Set maxRetries: 0 on the client to turn retries off.
Retrying
Retry these:429— back off exponentially before retrying.502,503and504— retry with exponential backoff and jitter, with the same idempotency key.409withRetry-After— the earlier request with that idempotency key is still in flight. WaitRetry-Afterseconds, then retry with the same key. This is safe. Do not generate a new key, or you risk creating a duplicate.- Network timeouts — you do not know whether the request was applied. Retry with the same idempotency key.
400, 401, 403, 404 or 422 without changing the request. They are deterministic and will fail identically.
A 500 is an unexpected error inside the API, and the request may have changed data before it failed. The API stores no result for a 5xx under the idempotency key, so a retry runs the request again: it does not replay a result. For this reason the SDKs do not retry a 500. If you retry one yourself, retry only a request that is safe to run twice, and send the same idempotency key.
On any 5xx, retry a write only with an Idempotency-Key. A write sent without one may already have been applied, and a retry can apply it a second time.
Related
Idempotency
Idempotency keys, replay behaviour, and the
409 conflict case.TypeScript SDK
Installation, authentication, and error handling.