# Rate Limiting

The API allows **60 requests per minute per API key**. The limit applies to every key in every account, and all endpoints share it.

The window is fixed: it starts with your first request and resets 60 seconds later. It is not a rolling window.

## Response Headers

Every response to an authenticated request includes these headers:

| Header | Description |
|--------|-------------|
| `X-RateLimit-Limit` | Requests allowed per window (`60`) |
| `X-RateLimit-Remaining` | Requests left in the current window |
| `X-RateLimit-Reset` | When the current window resets, as a Unix timestamp in seconds |

A few endpoints don't count toward the limit, and their responses leave these headers out.

```bash
curl -i https://shipwave.app/api/v1/orders \
  -H "Authorization: Bearer sw_live_abc123..."

# Response headers:
# X-RateLimit-Limit: 60
# X-RateLimit-Remaining: 58
# X-RateLimit-Reset: 1708372800
```

Requests that fail authentication (`401`) or scope checks (`403`) don't count toward the limit and don't get these headers.

## Exceeding the Limit

Once you go over the limit, the API returns `429 Too Many Requests` until the window resets:

```json
{
  "error": {
    "code": "RATE_LIMITED",
    "message": "Rate limit exceeded. Try again later."
  }
}
```

The `429` response includes the `X-RateLimit-*` headers and a `Retry-After` header giving the number of seconds to wait.

## Best Practices

1. **Check the headers.** Watch `X-RateLimit-Remaining` and slow down before you reach zero.

2. **Wait as long as the server tells you.** On a `429`, wait the number of seconds in `Retry-After`, then retry:

```typescript
async function fetchWithRetry(url: string, options: RequestInit, maxRetries = 3) {
  for (let i = 0; i <= maxRetries; i++) {
    const res = await fetch(url, options);
    if (res.status !== 429) return res;

    const retryAfter = Number(res.headers.get("Retry-After")) || 1;
    await new Promise((r) => setTimeout(r, retryAfter * 1000));
  }
  throw new Error("Rate limit exceeded after retries");
}
```

3. **Make fewer requests.** Use list filters and `limit` (up to 100 per page) rather than fetching records one at a time, and use [webhooks](webhooks.md) instead of polling for changes.
