# Error Handling

> HTTP status codes, error bodies, and which Sendable API errors to retry.

Source: https://wav1.recrav.com/docs/errors

## HTTP Status Codes [#http-status-codes]

| Code  | Meaning                                                                                                                                                          | Retry?                   |
| ----- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------ |
| `400` | Invalid request: body validation failed, bad cursor, bad phone number or jid                                                                                     | No, fix the request      |
| `401` | Missing, invalid, disabled, or expired API key                                                                                                                   | No                       |
| `403` | The key may not call this route: missing permission (`insufficient_scope`), or a session key on an account route. Also when WhatsApp refuses (not a group admin) | No                       |
| `404` | Session, chat, message, group, or webhook not found                                                                                                              | No                       |
| `409` | Conflict, for example an `Idempotency-Key` whose first request is still running                                                                                  | Yes, after `Retry-After` |
| `410` | Gone, for example an expired invite                                                                                                                              | No                       |
| `422` | `Idempotency-Key` reused with a different request body                                                                                                           | No, use a new key        |
| `429` | Rate limited (per API key or by the session send policy)                                                                                                         | Yes, after `Retry-After` |
| `500` | Unexpected server error                                                                                                                                          | Yes, with backoff        |
| `502` | WhatsApp returned an error                                                                                                                                       | Yes, with backoff        |
| `503` | WhatsApp session not connected, or the send queue is unavailable                                                                                                 | Yes, with backoff        |

## Error Bodies [#error-bodies]

JSON errors have an `error` field with a stable code and, usually, a `message`:

```json
{ "error": "rate_limited", "message": "api key rate limit exceeded", "retryAfter": 1260 }
```

| `error`                       | Status | Meaning                                                                                                                 |
| ----------------------------- | ------ | ----------------------------------------------------------------------------------------------------------------------- |
| `Validation failed`           | `400`  | The body or query failed validation. `issues` lists each field: `[{ "path": "/chatId", "message": "Expected string" }]` |
| `insufficient_scope`          | `403`  | The API key's [permissions](/docs/learn/api-keys#permissions) do not include `required` (for example `message:write`)   |
| `rate_limited`                | `429`  | The API key used up its request limit. `retryAfter` is in seconds                                                       |
| `idempotency_key_in_progress` | `409`  | The first request with this `Idempotency-Key` is still running                                                          |
| `idempotency_key_mismatch`    | `422`  | The `Idempotency-Key` was used before with a different body                                                             |
| `internal_error`              | `500`  | Unexpected server error                                                                                                 |

Other errors are returned as a plain-text message (`Content-Type: text/plain`), for example `401 invalid api key`, `404 chat not found`, or `503 whatsapp socket not connected`. Read the body as text when it is not JSON.

The [OpenAPI document](/docs/api-reference) describes these as the shared `ErrorResponse` schema.

## Retry Strategy [#retry-strategy]

* Honour `Retry-After` (seconds) on `409` and `429`.
* Retry `500`, `502`, and `503` with exponential backoff and jitter.
* Send an `Idempotency-Key` with `POST /v1/message/send` so a retry never sends the same message twice.
* The [SDK](/docs/quickstart/nodejs/sdk) retries `429`, `503`, and network errors for you when you set `maxRetries`.

```typescript
async function sendWithRetry(body: unknown, idempotencyKey: string, maxRetries = 3) {
  for (let attempt = 0; ; attempt++) {
    const res = await fetch('https://api.sendable.dev/v1/message/send', {
      method: 'POST',
      headers: {
        'x-api-key': process.env.SENDABLE_API_KEY!,
        'content-type': 'application/json',
        'Idempotency-Key': idempotencyKey,
      },
      body: JSON.stringify(body),
    })
    const retryable = [409, 429, 500, 502, 503].includes(res.status)
    if (!retryable || attempt >= maxRetries) return res
    const retryAfter = Number(res.headers.get('retry-after'))
    const delayMs = retryAfter > 0 ? retryAfter * 1000 : 2 ** attempt * 1000
    await new Promise((r) => setTimeout(r, delayMs + Math.random() * 250))
  }
}
```
