# Handle errors

> Every error the Onsite API answers with, what it means and how to fix it.

When a request can't be answered, the API sends an HTTP status code and a JSON body that says what went wrong in words you can act on. Check the status code first, then `error.code` in your code, and show or log `error.message`.

## The error body

```json
{
  "error": {
    "code": "invalid_input",
    "message": "range must be one of last_7d, last_14d, last_28d, last_3m, last_6m, last_12m, last_16m, this_month, last_month, last_quarter, year_to_date, or a window like 2026-08-01..2026-08-31.",
    "param": "range",
    "didYouMean": "last_28d",
    "requestId": "E-7K2Q-M4XD"
  }
}
```

<ResponseField name="error.code" type="string" required>
  What went wrong, as a stable word your code can check. The table below lists every one.
</ResponseField>

<ResponseField name="error.message" type="string" required>
  What went wrong, in a sentence a person can act on. The wording can improve over time, so check `code`, not `message`.
</ResponseField>

<ResponseField name="error.param" type="string">
  The query parameter, path part or body field at fault, when there is one.
</ResponseField>

<ResponseField name="error.allowed" type="string[]">
  The values it accepts, when there is a fixed set.
</ResponseField>

<ResponseField name="error.didYouMean" type="string">
  The closest allowed value, when the one you sent looks like a typo.
</ResponseField>

<ResponseField name="error.requestId" type="string" required>
  Quote it to support and they can find this request. It's also in the `X-Request-Id` header.
</ResponseField>

## Error codes

| Status | Code | What it means | What to do |
|---|---|---|---|
| 400 | `invalid_input` | A parameter or body field isn't valid, or isn't one this endpoint takes. | Fix the field `param` names. Use `allowed` or `didYouMean`. |
| 401 | `unauthenticated` | The key is missing, wrong, revoked or expired. | Send `Authorization: Bearer <key>` with a working key. |
| 402 | `payment_required` | The team doesn't have an active plan. | An owner renews the plan in **Settings › Billing**. |
| 402 | `credits_exhausted` | The team has used this month's credits. Only exports use credits. | Credits come back on the 1st of each month. |
| 403 | `forbidden` | The key can't do this: it can only read, it's limited to other sites, or its maker's role doesn't allow it. | Use a key with the right sites and **Can make changes**, made by someone whose role allows it. |
| 404 | `not_found` | The site, alert, note, export or endpoint doesn't exist, or the key can't read it. | Check the path. For a site, `didYouMean` suggests the closest one. |
| 405 | `invalid_input` | The endpoint exists but not for this method, such as `DELETE /sites`. | Use a method from the `Allow` header. |
| 409 | `conflict` | The change clashes with what's already there. | Read `message` for what to change. |
| 409 | `google_access_lost` | The Google account behind the site is no longer linked. | Someone on your team reconnects it in Onsite. |
| 422 | `search_data_unavailable` | Google hasn't sent complete data for some of these dates yet. | Ask for an earlier or shorter period. |
| 429 | `rate_limited` | Too many requests for this key. | Wait for the `Retry-After` seconds. See [Rate limits](/api/rate-limits). |
| 429 | `quota_limited` | Google Analytics is busy for this property. | Wait for the `Retry-After` seconds. |
| 500 | `internal` | Something went wrong on our side. | Try again. If it keeps happening, contact support with the `requestId`. |
| 503 | `search_data_changed` | New data arrived while your request was being answered. | Try again after the `Retry-After` seconds. |
| 503 | `maintenance` | Onsite is down for maintenance. | Try again after the `Retry-After` seconds. |

## Retry safely

Retry a request when the status is `429`, `500` or `503`, after the `Retry-After` header's seconds (or a few seconds when there isn't one). Wait a little longer after each try.

Don't retry other `4xx` errors unchanged: the same request gets the same answer. Change what `message` asks for first.

```javascript
async function onsite(path, tries = 4) {
  for (let attempt = 1; ; attempt++) {
    const response = await fetch(`https://api.onsite.so/api/v1${path}`, {
      headers: { Authorization: `Bearer ${process.env.ONSITE_API_KEY}` },
    });

    if (response.ok) return response.json();

    const retryable = [429, 500, 503].includes(response.status);
    if (!retryable || attempt === tries) {
      const { error } = await response.json();
      throw new Error(`${error.code}: ${error.message} (${error.requestId})`);
    }

    const wait = Number(response.headers.get("retry-after")) || 2 ** attempt;
    await new Promise((resolve) => setTimeout(resolve, wait * 1000));
  }
}
```

## Related

<CardGroup cols={2}>
  <Card title="Authentication" icon="key-round" href="/api/authentication">
    Fix key and permission errors.
  </Card>
  <Card title="Rate limits" icon="gauge" href="/api/rate-limits">
    Stay under the limits and handle `429`.
  </Card>
</CardGroup>
