> ## Documentation Index
> Fetch the complete documentation index at: https://docs.crispstats.com/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> CrispStats is a hosted, privacy-first web analytics product. It has no self-hosted edition, no session replay, no heatmaps, no A/B testing, no mobile SDKs and no data import except a site's history from a Plausible export (Settings, Import and export).
> The script global is crispstats(...), site keys start with cs_pk_ and secret API keys start with cs_sk_. The script is served from https://in.crispstats.com/d.js and the REST API lives at https://crispstats.com/api/v1.
> There is no npm package to install. Integrations copy the script tag, the queue stub, crispstats.d.ts and proxy code from these docs or from Settings, Tracking code in the dashboard. A first-party proxy forwards one path of the site to https://in.crispstats.com/p/ and forwards only content-type, user-agent, accept-language, referer, origin, if-none-match and x-forwarded-for, never cookies.

# API errors

> Every CrispStats API error uses one JSON envelope with a stable code, a readable message and per-parameter fields. Status codes 400 to 500 and how to handle each.

When a CrispStats API request fails, the response has a 4xx or 5xx status and a JSON body with one shape on every endpoint. Branch on `error.code`, which never changes; show or log `error.message`, which names the cause and the fix. Validation errors add a `fields` object that maps each bad parameter to its problem.

```json theme={"theme":{"light":"github-light","dark":"github-dark"}}
{
  "error": {
    "code": "invalid_request",
    "message": "range: Pass either range or from and to, not both.",
    "fields": {
      "range": "Pass either range or from and to, not both."
    }
  }
}
```

## The error envelope

**error** `object` *required*

**properties**

**code** `string` *required*

A stable, machine-readable code. One of the codes in the table below.

**message** `string` *required*

What went wrong and how to fix it, written for a person. Do not parse it: the wording can change.

**fields** `object`

On validation errors only. Each key is a parameter name and each value is the problem with it. Problems inside a nested value use a dotted path, such as `filters.0.value`.

## Error codes

| Status | Code | When it happens |
| - | - | - |
| `400` | `invalid_request` | A parameter is missing, unknown, out of range or malformed. |
| `401` | `unauthorized` | The `Authorization` header is malformed, the key is not valid, or no key was sent for a site other than `demo`. |
| `402` | `plan_limit_reached` | The workspace is in its trial and has reached 25,000 events this month, so it is paused, or it has no plan since its trial or plan ended, so it is locked. |
| `403` | `forbidden` | The key lacks the **Read** scope, or is restricted to a different site. |
| `404` | `not_found` | The site, funnel, visitor or endpoint does not exist, or the key cannot read it. |
| `405` | `method_not_allowed` | The request used a method other than `GET`. The API is read-only. |
| `429` | `rate_limited` | The key is over its burst limit or its daily allowance. |
| `500` | `internal_error` | Something failed on the CrispStats side. |

## Handle each error

> [accordion] 400 invalid\_request
>
> The request will fail the same way until you change it. Read `fields` to find the parameter. Common causes:
>
> | Request | Message |
> | - | - |
> | `/stats?rang=7d` | `rang: Unknown parameter. This endpoint takes: range, from, to, compare, filter.` The list is the one for the endpoint you called. |
> | `?range=forever` | The list of valid presets. |
> | `?range=7d&from=2026-10-01&to=2026-10-31` | `range: Pass either range or from and to, not both.` |
> | `?filter=country:is` | `filter: "country:is" is not a filter. Use dimension:op:value, for example country:is:DE.` followed by the valid dimensions and operators. |
> | `/breakdown/shoe-size` | The dimension is not one of the supported dimensions. |
> | `/timeseries?range=all&granularity=hour` | `granularity: hour buckets over this period would return about 60,279 points (the limit is 400). Choose a coarser granularity or a shorter period.` The count grows with the period. |
> | `/realtime?filter=country:is:DE` | `filter: This endpoint does not take filters.` |
>
> When several parameter values are invalid, `message` names them all, for example `2 parameters are not valid: range, limit.`, and `fields` has one entry for each. When several parameters are unknown, `message` names the first and `fields` lists every one. Do not retry a `400` unchanged.

> [accordion] 401 unauthorized
>
> The response also carries `WWW-Authenticate: Bearer realm="api"`. Check that the header reads `Authorization: Bearer cs_sk_…` and that the key has not been revoked. A malformed or revoked key is refused even on the demo site; CrispStats never falls back to anonymous access. Create a new key under **Settings**, **API keys** if needed. See [authentication](https://docs.crispstats.com/api-reference/authentication.md).

> [accordion] 402 plan\_limit\_reached
>
> The API does not answer for one of two reasons. The workspace is in its trial and has reached the trial's 25,000 events for the calendar month (UTC), so it is paused: access returns when an owner picks a plan, or at 00:00 UTC on the first day of the next month. Or the workspace has no paid plan because its trial ended or its plan was cancelled or left unpaid, so it is locked: access returns when an owner picks a plan. Nothing recorded before is lost, unless a locked workspace's sites reached their deletion 30 days after the lock. See [when the trial reaches its event limit](https://docs.crispstats.com/account/plans-and-limits.md#when-the-trial-reaches-its-event-limit) and [when the trial or a plan ends](https://docs.crispstats.com/account/plans-and-limits.md#when-the-trial-or-a-plan-ends).

> [accordion] 403 forbidden
>
> The key is valid but cannot do this. Either it has neither the **Read** nor the **Admin** scope, or it was created for one site and you asked for another. Use a key with **Read** that covers the site.

> [accordion] 404 not\_found
>
> The message says what was not found: "That site was not found, or this key does not have access to it." The same answer covers a site that does not exist and a site of another workspace. A funnel id from another site, an unknown visitor id and a path under `/api/v1` that is not an endpoint also return `404`. List what exists with `GET /sites` and `GET /sites/{siteId}/funnels`.

> [accordion] 405 method\_not\_allowed
>
> The API only reads. `POST`, `PUT`, `PATCH` and `DELETE` on `/api/v1/sites`, every endpoint under it and any unknown path under `/api/v1` return this code with `Allow: GET, OPTIONS`. To send events, use the [collector](https://docs.crispstats.com/api-reference/collector.md).

> [accordion] 429 rate\_limited
>
> Wait the number of seconds in the `Retry-After` header, then retry. The message says whether you hit the burst limit or used up the daily allowance, which resets at midnight UTC. See [rate limits](https://docs.crispstats.com/api-reference/rate-limits.md).

> [accordion] 500 internal\_error
>
> The message is usually "Something went wrong on our side. Try again in a moment." and never includes internal details. Retry with exponential backoff, for example after 1, 2 and 4 seconds. If it keeps failing, contact support with the time and the request path.

## Retry rules

| Status | Retry? |
| - | - |
| `400`, `401`, `403`, `404`, `405` | No. Change the request or the key first. |
| `402` | Not until a paid plan starts or the next month begins. |
| `429` | Yes, after `Retry-After` seconds. |
| `500` | Yes, with exponential backoff and a cap on attempts. |

```javascript theme={"theme":{"light":"github-light","dark":"github-dark"}}
async function crispstats(path) {
  const response = await fetch(`https://crispstats.com/api/v1${path}`, {
    headers: { Authorization: `Bearer ${process.env.CRISPSTATS_API_KEY}` },
  });
  if (response.ok) return response.json();

  const { error } = await response.json();
  switch (error.code) {
    case "invalid_request":
      throw new Error(`Bad request: ${JSON.stringify(error.fields ?? error.message)}`);
    case "unauthorized":
    case "forbidden":
      throw new Error(`Check the API key: ${error.message}`);
    case "not_found":
      return null;
    case "rate_limited":
      throw new Error(`Rate limited, retry in ${response.headers.get("Retry-After")} seconds`);
    default:
      throw new Error(`${response.status} ${error.code}: ${error.message}`);
  }
}

const stats = await crispstats("/sites/demo/stats?range=7d");
console.log(stats?.summary);
```

> [!NOTE]
>
> The [MCP server](https://docs.crispstats.com/api-reference/mcp.md) reports the same codes differently: a failed tool call returns this envelope as the tool result with `isError: true`, and failures before a message is handled are JSON-RPC errors.

## Next steps

> [card] [Query parameters](https://docs.crispstats.com/api-reference/query-parameters.md)
>
> Valid values for every parameter that can cause a 400.

> [card] [Authentication](https://docs.crispstats.com/api-reference/authentication.md)
>
> Fix 401 and 403 responses.

> [card] [Rate limits](https://docs.crispstats.com/api-reference/rate-limits.md)
>
> Avoid and handle 429 responses.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.