# Rate Limits

> Request limits per API key, per-session send pacing, and how to handle 429 responses.

Source: https://wav1.recrav.com/docs/rate-limits

Sendable has two separate limits:

1. A **request limit per API key** for every `/v1` route.
2. A **send policy per WhatsApp session** that paces outgoing messages to protect the number from WhatsApp bans.

Both answer `429 Too Many Requests` with a `Retry-After` header (seconds) when you need to slow down.

## Request limit per API key [#request-limit-per-api-key]

Each API key may make **500 requests per hour**. The window starts with the key's first request and resets one hour later, whatever the traffic in between. Limits do not depend on the plan. Self-hosted servers can change them with `API_RATE_LIMIT_MAX` and `API_RATE_LIMIT_WINDOW_SECONDS`.

Every response to an authenticated request carries the current state:

```http
RateLimit-Limit: 500
RateLimit-Remaining: 499
RateLimit-Reset: 3600
RateLimit-Policy: 500;w=3600
```

| Header                | Description                                |
| --------------------- | ------------------------------------------ |
| `RateLimit-Limit`     | Requests allowed per window                |
| `RateLimit-Remaining` | Requests left in the current window        |
| `RateLimit-Reset`     | Seconds until the window resets            |
| `RateLimit-Policy`    | The policy as `<limit>;w=<window seconds>` |

The names follow the IETF `RateLimit` header draft. There are no `X-RateLimit-*` headers. Browsers can read all of these headers (they are listed in `Access-Control-Expose-Headers`).

When the key is exhausted, the response is `429` with `Retry-After` and a JSON body:

```http
HTTP/1.1 429 Too Many Requests
Retry-After: 1260
RateLimit-Limit: 500
RateLimit-Remaining: 0
RateLimit-Reset: 1260
Content-Type: application/json

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

Requests with a missing or invalid key get `401` and do not count against any limit.

## Send policy per WhatsApp session [#send-policy-per-whatsapp-session]

Sending through `POST /v1/message/send` is paced per WhatsApp session. You configure the policy in the dashboard under the session's settings.

| Rule                                                    | Default                                  | Sync send (`async` unset or `false`)            | Async send (`"async": true`) |
| ------------------------------------------------------- | ---------------------------------------- | ----------------------------------------------- | ---------------------------- |
| Gap between sends                                       | 5 s, plus up to 2 s random jitter        | waits for the next slot, up to 60 s, then `429` | queued with a delay          |
| Warm-up after linking a number                          | 24 h: 30 s gap, at most 50 sends per day | same as above                                   | same as above                |
| Daily send cap                                          | none                                     | `429` until the next day (session timezone)     | queued until the next day    |
| Quiet hours                                             | none                                     | `429` until quiet hours end                     | queued until quiet hours end |
| Sending paused (manually or after a connection failure) | off                                      | `429`, `Retry-After: 60`                        | `429`, `Retry-After: 60`     |

These `429` responses carry `Retry-After` and a plain-text body such as `quiet hours, retry in 1800 seconds`. They do not carry `RateLimit-*` headers for the send policy (the `RateLimit-*` headers on them describe the request limit).

## Handling 429 [#handling-429]

* Wait for `Retry-After` seconds before retrying. Do not retry in a tight loop.
* Watch `RateLimit-Remaining` and slow down before it reaches 0.
* Send an `Idempotency-Key` with `POST /v1/message/send` so a retry can never send the same message twice. See [API Reference](/docs/api-reference#conventions).
* For bulk sends, use `"async": true`. The queue applies the send policy for you instead of rejecting requests.

```typescript
async function withRetry<T>(fn: () => Promise<Response>, retries = 3) {
  for (let attempt = 0; ; attempt++) {
    const res = await fn()
    if (res.status !== 429 || attempt >= retries) return res
    const retryAfter = Number(res.headers.get('retry-after') ?? '1')
    await new Promise((r) => setTimeout(r, retryAfter * 1000))
  }
}

const res = await withRetry(() =>
  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': 'order-123-notify',
    },
    body: JSON.stringify({
      chatId: '6281234567890@s.whatsapp.net',
      text: { content: 'Your order has shipped' },
    }),
  }),
)
```
