Errors

Sendfern uses HTTP status codes to say whether a request worked, and returns the same error object for every failure. This page lists the types and codes and how to handle each.

The error object

A 2xx status means the request worked. Anything in the 4xx range means the request was refused and nothing changed. A failed response has this body:

Response · 400 Bad Request
{
  "error": {
    "type": "invalid_request_error",
    "code": "invalid_parameter",
    "message": "'not-an-address' is not a valid email address.",
    "param": "to"
  }
}
typestring
The broad class of error. It follows the HTTP status, so you can branch on either.
codestring
A specific, stable reason, such as invalid_parameter or quota_exceeded. Branch on this in code.
messagestring
A sentence for a developer saying what went wrong and often how to fix it. It may change, so don't match on it.
paramstring
The request field the error is about. Present only when the error is about one field.

What param points to

param names the field in your request body or query string that caused the error, for example to, scheduled_at or tag. Nested fields use dots and list positions use brackets, counting from 0:

Response · 400 Bad Request
{
  "error": {
    "type": "invalid_request_error",
    "code": "invalid_parameter",
    "message": "This field is required.",
    "param": "attachments[0].filename"
  }
}

Only the first problem is reported. Fix it and send again to see the next one. In a batch, nothing is queued if any item is invalid, and the message starts with the position of the first bad item, for example Item 2:, followed by its errors.

Status codes, types and codes

StatusTypeCodes
400invalid_request_errorinvalid_parameter, invalid_request, not_cancelable, checks_failed, already_sent, not_editable, not_resendable, not_retryable, endpoint_disabled, cannot_revoke_self, audience_in_use, provider_error
401authentication_errornot_authenticated, authentication_failed
403permission_errorpermission_denied, domain_not_verified, account_not_active, quota_exceeded
404not_found_errornot_found
409idempotency_erroridempotency_conflict, idempotency_in_progress
415invalid_request_errorunsupported_media_type
429rate_limit_errorthrottled

Any other status, for example 405 for a method an endpoint doesn't support, has the type api_error.

What the codes mean

CodeMeaning
invalid_parameterA field is missing, has the wrong type or breaks a limit. See param.
invalid_requestThe request as a whole is wrong, for example a batch that isn't an array of 1 to 100 emails.
not_cancelableOnly a scheduled email can be cancelled.
checks_failedThe broadcast failed its pre-send checks. The message lists them.
already_sentOnly a draft broadcast can be sent. Cancel a scheduled one first.
not_editableOnly a draft broadcast can be edited.
not_resendableThis email can't be resent, for example its body has been cleared or it came from a broadcast.
not_retryableOnly a failed webhook delivery can be retried.
endpoint_disabledThe webhook endpoint is disabled. Enable it first.
cannot_revoke_selfA key tried to revoke itself. Use another key.
audience_in_useThe audience has broadcasts, so it can't be deleted.
provider_errorThe email provider refused or couldn't be reached for a domain change. Try again later.
not_authenticatedNo API key was sent.
authentication_failedThe key is wrong, malformed or revoked.
permission_deniedThe key may not do this: a send-only key outside sending, or a test key changing a shared object.
domain_not_verifiedNo verified domain covers the From address. See Domains and DNS.
account_not_activeThe account is awaiting approval or paused. See Going live.
quota_exceededThe send would go over the monthly quota. See Rate limits and quotas.
not_foundNo such object in your account and mode.
idempotency_conflictThe Idempotency-Key was used before with a different body.
idempotency_in_progressA request with the same Idempotency-Key is still running.
unsupported_media_typeThe body isn't JSON. Send Content-Type: application/json.
throttledToo many requests. Wait for the number of seconds in Retry-After.

The account errors say what to do in their message, so log it or show it to whoever runs the account:

Response · 403 Forbidden
{
  "error": {
    "type": "permission_error",
    "code": "quota_exceeded",
    "message": "This account has used 5000 of its 5000 live emails this month and this needs 1 more. Ask us to raise the quota, or wait for next month."
  }
}

New codes may be added. Treat a code you don't know by its type and status.

Handle errors

The SDKs throw a SendfernError for every failed request, with status, type, code, param and message. If Sendfern can't be reached at all, status is 0 and code is network_error. A response without an error object, such as some 5xx responses, gets the code unexpected_response.

curl -i https://api.sendfern.com/v1/emails \
  -H "Authorization: Bearer $SENDFERN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "from": "Acme <onboarding@acme.com>",
    "to": "not-an-address",
    "subject": "Hello",
    "text": "Hello"
  }'

# HTTP/1.1 400 Bad Request
# {"error": {"type": "invalid_request_error", "code": "invalid_parameter", ...}}

Decide whether to retry

  • 429. Retry after the number of seconds in the Retry-After header. Nothing was done, so a retry is always safe. See Rate limits and quotas.
  • 5xx. Something failed on our side, and the body may not be an error object. Retry with exponential backoff. For a send, use an idempotency key so a retry can't send twice.
  • 409. idempotency_in_progress: wait a moment and retry with the same key. idempotency_conflict: a bug, since one key must always carry the same body.
  • Other 4xx. Don't retry the same request; it will fail again. Fix the request, or for domain_not_verified, account_not_active and quota_exceeded, fix the account.

The SDKs already retry 429s, 5xx responses and network errors, twice by default, and wait for Retry-After when it is sent. They retry a POST that fails with a 5xx or a network error only when it has an idempotency key. Change the number of retries with maxRetries in Node.js or max_retries in Python.

# Branch on the HTTP status first, then on error.code:
#   429 or 5xx  -> wait and retry (honour Retry-After on a 429)
#   409         -> same Idempotency-Key, different body: fix your code
#   other 4xx   -> do not retry; fix the request or the account

Errors after the request

A send that is accepted can still fail later, for example when the recipient's server rejects it. That isn't an API error: the email's status becomes bounced or failed, and you hear about it through its timeline and webhooks. See Email lifecycle.