# Reading a Send Failure

A failed send is easy to make worse. The wrong retry can send two emails, or hammer
a request that can never succeed. So every failure in mailbot, in the API, the MCP
tools and the dashboard, is explained the same way.

| Question | Where you find it |
|---|---|
| What happened | One plain sentence. |
| Where did it stop | The **stage**, listed below. |
| Is a retry safe | One of five **retry answers**, listed below. |
| Which request was it | The request ID. It is the `x-request-id` response header, and `error.request_id` when the body carries it. Quote it to support. |
| What do I do now | One next action. |

Branch your code on `error.code`, never on the message text. The code is stable.

## What the API returns today

The error body is `{"error": {"code", "message"}}`. Some errors add fields such as
`message_id`, `reason` or `balance`. The request ID is in the `x-request-id` header.

`stage`, `retry_safe`, `request_id` and `next_action` inside `error` are requested
from the backend and are not returned yet. The MCP tools and the dashboard already
read them when they appear. Until then they work out the stage and the retry answer
from the code, using the tables on this page. If a response ever says
`retry_safe: false`, treat it as stricter than the tables. A `true` never overrides
a table row that says wait, fix or check.

## Stages

| Stage | Means | Was anything created |
|---|---|---|
| `auth` | The key or sign in was not accepted. | No |
| `request` | The request itself is wrong or incomplete. | No |
| `policy` | A send rule said no: recipient, limit, pause, attachment. | No, and any credit is returned |
| `approval` | A person must approve the message first. | A held message exists. It is not sent. |
| `domain` | The sender domain is not ready. | No |
| `queue` | The scheduler could not take the job. | A saved message exists. It will not go out on time. |
| `provider` | The handoff to the email provider did not confirm. | A saved message exists, marked failed. |
| `unknown` | We could not tell what happened. | Maybe. Read it back. |

## Retry answers

| Answer | Meaning | What to do |
|---|---|---|
| `safe` | Nothing was created. | Send the same request again. |
| `wait` | Nothing was created. A limit or pause is in the way. | Send again after the wait. |
| `fix_first` | The same request fails again. | Change something, then send. |
| `check_first` | The outcome is not known. | Read the message back. Send again only if it is not there, with a **new** `Idempotency-Key`. |
| `never` | The first request already did its job. | Do not send another. |

Never retry on the status alone. A `502` and a `503` can mean the message already
left. A `403` and a `429` mean it did not.

## The ten kinds of failure

### 1. Sign in and keys

`UNAUTHORIZED`, `INSUFFICIENT_PERMISSION`, `SCOPED_API_KEY_FORBIDDEN`,
`OWNERSHIP_VERIFICATION_REQUIRED`, `ownership_unverified`.

Stage `auth` or `policy`. Retry: `fix_first`. The key is wrong, too narrow, or the
account has not proved it owns its email address. Next: use a key with write
permission, or sign in at getmail.bot with the email code, Google or GitHub.

### 2. Invalid request

`VALIDATION_ERROR`, `INVALID_SEND_AT`, `IDEMPOTENCY_KEY_REQUIRED`,
`recipient_invalid`, `recipient_cap_exceeded`, `unsafe_header`.

Stage `request`. Retry: `fix_first`. Nothing was created. Next: fix the field the
message names.

### 3. Policy denial

`recipient_unproven`, `recipient_suppressed`, `api_key_recipient_denied`,
`api_key_hourly_limit_exhausted`, `outbound_paused`, `RATE_LIMIT_EXCEEDED`,
`MONTHLY_EMAIL_LIMIT_REACHED`, and the rest of the
[delivery policy codes](/docs/concepts/send-authorization#delivery-policy-codes).

Stage `policy`. Retry: `fix_first` for a rule you can change, `wait` for a limit or
an operator pause. A denial that is proven to have happened before the provider
removes the provisional message and returns any credit. Next: change the recipient
or key setting, or wait for the reset.

### 4. Approval hold

`202` with `approval_status: "awaiting_approval"`. This is not an error.

Stage `approval`. Retry: `never`. The message is held and not sent, for up to 24
hours. Sending it again creates a second held message. Next: approve or reject it in
the dashboard. After 24 hours it is dropped, and trying to decide it returns
`APPROVAL_EXPIRED`. Write a new message if you still need it.

### 5. Domain and DKIM

`DOMAIN_NOT_VERIFIED`, `domain_unverified`, `COMPLIANCE_FAILED`.

Stage `domain`. Retry: `fix_first`. Nothing was created.

What it means, without the jargon. Your domain has a published signature, called
DKIM, that proves mail from it really came from you. Receiving mail servers look
for that signature. If the DNS record that holds it is missing, mistyped or has not
spread yet, they cannot trust your mail, so mailbot stops before sending. SPF is a
second record that lists who may send for your domain.

Next: open the domain's DNS check, fix the one record it names, wait for DNS to
update (it can take minutes, sometimes longer), then run verify. Retrying the send
before that fails the same way.

### 6. Queue failure

`SCHEDULE_ENQUEUE_FAILED`.

Stage `queue`. Retry: `check_first`. The message was saved, but the scheduler could
not queue it, so it will not go out at the send time. The error carries
`message_id`. Next: reschedule that saved message. Do not create a second one. The
failed request also locks its `Idempotency-Key`.

### 7. Provider rejection

`TRANSMISSION_FAILED` (`502`).

Stage `provider`. Retry: `check_first`. The email provider did not confirm it
accepted the message. The message is saved with `delivery_status: "failed"` and any
credit is returned. Today the same code is returned for a clean rejection and for a
handoff that timed out, so **do not assume nothing was delivered**. Next: read the
message by its `message_id`. If it is failed and no copy has arrived, send a new
email with a new `Idempotency-Key`.

### 8. Unknown status

`delivery_unavailable` (`503`), `INTERNAL_ERROR` (`500`),
`IDEMPOTENCY_OUTCOME_UNKNOWN` (`409`), and a request that got no answer at all.

Stage `unknown`. Retry: `check_first`. We could not decide, so we do not guess.
Next: read your recent messages. Send again only if the message is not there.

### 9. Attachment rejection

`OUTBOUND_POLICY_DENIED` with `reason: "attachment_review_required"`.

Stage `policy`. Retry: `fix_first`. Outbound attachments are off. No file type is
accepted, and no message, debit or reservation is made. Next: remove `attachments`
or send `[]`, and link to a file you host. This page makes no promise about when
attachments return. See [Messages API](/docs/messages-api#attachments) for the
current rule.

### 10. Duplicates and repeated requests

`IDEMPOTENCY_CONFLICT`, `IDEMPOTENCY_IN_PROGRESS`, `delivery_already_reserved`.

- Same key, same request, finished earlier: you get the **same response again**, and
  no second email. Today that repeat looks identical to the first response. Do not
  count it as a new send. Compare the `message_id`.
- Same key, different request: `IDEMPOTENCY_CONFLICT`. Use a new key for a new email.
- Same key, first request still running: `IDEMPOTENCY_IN_PROGRESS`. Wait, then send the
  identical request with the identical key.
- First request failed with a server error: the key is locked and returns
  `IDEMPOTENCY_OUTCOME_UNKNOWN`. Read the message back before using a new key.
- `delivery_already_reserved`: the delivery already exists. Retry: `never`.

## In your code

Branch on the code and the retry answer. Do not loop on the status.

```ts
const res = await fetch(url, { method: 'POST', headers, body });
if (res.ok) return res.json();

const { error } = await res.json().catch(() => ({ error: {} }));
const requestId = res.headers.get('x-request-id');

switch (error.code) {
  case 'IDEMPOTENCY_IN_PROGRESS':
  case 'RATE_LIMIT_EXCEEDED':
    // Waiting is safe. Nothing was created.
    break;
  case 'TRANSMISSION_FAILED':
  case 'delivery_unavailable':
  case 'INTERNAL_ERROR':
    // Unknown outcome. Read the message back before any new send.
    break;
  default:
    // Everything else needs a change before the same request can work.
}
console.error(`send failed: ${error.code} request=${requestId}`);
```

See [Errors](/docs/errors) for every code and status, and
[Send Authorization](/docs/concepts/send-authorization) for the order the checks run.
