Guides
Errors
The error envelope, every status the API answers with, and the codes you can act on.
The error envelope#
Every error, from every operation, has the same shape. A successful answer is { "data": … }; a failure is { "error": … } with a matching HTTP status.
{
"error": {
"code": "validation_failed",
"message": "Some fields are invalid.",
"request_id": "4f1c2b8e9a3d47e0b6c5a1d2e3f40718",
"details": [
{ "field": "to", "message": "Contains an invalid email address." }
]
}
}| Field | Meaning |
|---|---|
code | A stable identifier for programs, such as validation_failed. Switch on this, never on the message. |
message | A sentence that is safe to show a person. Its wording can change. |
request_id | Identifies the request in our logs. Quote it when you ask for help. It is also in the X-Request-Id response header. |
details | A list of { field, message }, one per invalid field. Also one per refused recipient of a marketing or promotional email. |
resource_id | The existing resource a conflict is about. Left out otherwise. |
You can send your own X-Request-Id request header (8 to 64 letters, digits, dots, hyphens or underscores) to have it used as the request ID, which makes it easy to match a call in your logs to ours. Anything else is replaced with one we make up.
Statuses#
| Status | Meaning | Retry? |
|---|---|---|
400 | The body is not one JSON object, or has a field the endpoint does not accept (invalid_json). Unknown fields are rejected, not ignored. | No. Fix the request. |
401 | No key, or an unknown, revoked or expired key (unauthenticated, invalid_api_key). | No. See Authentication. |
403 | The key lacks the scope, the organisation is suspended or closed, or the send was refused by a sending rule. | No, until the cause is fixed. |
404 | No such resource, or no such route (not_found). An ID that is not a valid UUID is also a 404. | No. |
405 | The method is not allowed on that route (method_not_allowed). | No. |
409 | A conflict, such as domain_exists, domain_claimed or webhook_limit_reached. | No. |
413 | The body is larger than 1 MB (body_too_large). | No. |
422 | Well-formed but not acceptable: validation_failed, idempotency_key_reused and the rules of the marketing streams. | No. Fix the request. |
429 | Too many requests (rate_limited). Wait Retry-After seconds. | Yes, after waiting. |
5xx | Something went wrong on our side (internal_error). | Yes, with backoff. Safe for reads and for sends with an idempotency key. |
Codes you can act on#
Authentication and account#
| Code | Status | Meaning |
|---|---|---|
unauthenticated | 401 | No API key was sent. |
invalid_api_key | 401 | The key is unknown, revoked or expired. |
insufficient_scope | 403 | The key does not hold the scope this operation needs. |
account_suspended | 403 | The organisation is suspended; none of its keys work until that is lifted. |
account_closed | 403 | The organisation has been closed; none of its keys will work again. |
Sending#
| Code | Status | Meaning |
|---|---|---|
validation_failed | 422 | details names each invalid field. Idempotency-Key is the field for a bad key. |
idempotency_key_reused | 422 | The key was used before with a different body. |
stream_disabled | 403 | Sending on this stream has been turned off for the organisation. |
stream_not_allowed | 403 | The key may only send transactional email (it lacks campaign:write). |
domain_not_verified | 403 | The From domain is not registered or not verified. |
domain_disabled | 403 | The From domain has been disabled. |
sender_not_authorised | 403 | The From address may not be used. |
template_not_published | 422 | template_id names a template with no published version. |
template_category_mismatch | 422 | stream is not the template's category. |
single_recipient_required | 422 | A marketing or promotional email has more than one recipient. |
postal_address_not_set | 422 | Your organisation has not set its postal address. |
business_identification_missing | 422 | A body part that places its own footer does not show the postal address. |
A marketing or promotional recipient refused for its own reasons answers 403 with the reason as the code, such as recipient_unsubscribed. They are listed in Marketing and promotional email.
Domains and webhooks#
domain_exists(409): you have already registered this domain.domain_claimed(409): another organisation has already verified it.webhook_limit_reached(409): your organisation already has the maximum number of endpoints, 20.
Errors in the libraries#
The libraries turn each of these into an exception of its own kind, by status, with the code, the message, the request ID, the field details and, for a 429, the wait. A problem with no HTTP answer at all (the network, a timeout) is a connection error, a different kind. An answer that is not our envelope, such as an HTML page from a proxy, becomes the code unexpected_response with the status; the libraries never fail with a JSON parse error.
import { APIError, RateLimitError, ValidationError } from "@cirrus-mail/cirrus";
try {
await cirrus.emails.send(message);
} catch (error) {
if (error instanceof ValidationError) {
console.log(error.details); // [{ field, message }, ...]
} else if (error instanceof RateLimitError) {
console.log("try again in", error.retryAfter, "seconds");
} else if (error instanceof APIError) {
console.log(error.status, error.code, error.requestId);
} else {
throw error; // a connection problem, or a bug
}
}