> ## 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.

# Authenticate API requests

> Authenticate CrispStats REST API and MCP requests with a secret cs_sk_ key as a bearer token. Scopes, site-restricted keys and what 401, 403 and 404 mean.

CrispStats authenticates REST API requests with a secret API key that starts with `cs_sk_`. Send it in the `Authorization` header as a bearer token on every request. The same key works for the [MCP server](https://docs.crispstats.com/api-reference/mcp.md), and a key with the **Events**, **Write** or **Admin** scope also sends [server-side events](https://docs.crispstats.com/track/server-side-events.md) to the collector.

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl "https://crispstats.com/api/v1/sites" \
  -H "Authorization: Bearer cs_sk_your_key"
```

## Create a key

**1. Open the API keys settings**

In the dashboard, open the workspace page (**Workspace** in the account menu, or **Team and API keys** below a site's settings tabs) and find **API keys**. Only owners and admins of the workspace can create, revoke or delete keys; other members see a note to ask one of them.

**2. Name the key and pick scopes**

Select **Create key**, give it a name that says what uses it (for example `Production backend`), and keep the **Read** scope. The REST API and the MCP server accept a key that has **Read** or **Admin**.

**3. Choose which sites it can read**

Under **Access**, choose **Every site in** your workspace, or **Only** the current site. A key always belongs to one workspace.

**4. Copy the key**

The full key is shown once, right after you create it. CrispStats keeps only a hash, so it cannot show it again. Store it in a secret manager or an environment variable such as `CRISPSTATS_API_KEY`. If you lose it, revoke it and create a new one.

> [!WARNING]
>
> Keep secret keys on a server. The API allows requests from any origin, so a key in browser code works for anyone who reads your page source, until you revoke it.

## Scopes

A key carries one or more scopes. Only **Read** and **Admin** matter to the REST API and the MCP server.

| Scope | What it allows |
| - | - |
| **Read** | Read stats and reports through the REST API and the MCP server. |
| **Events** | Send server-side events and payments to the collector. Not accepted by the REST API. |
| **Write** | Send server-side events to the collector. The REST API is read-only and does not accept this scope on its own. |
| **Admin** | Read through the REST API and the MCP server, read [raw events](https://docs.crispstats.com/api-reference/raw-events.md), and send server-side events to the collector. |

A valid key without **Read** or **Admin** gets `403 forbidden` on every API request. The [raw events](https://docs.crispstats.com/api-reference/raw-events.md) endpoint needs **Admin**: a key with only **Read** gets `403 forbidden` there.

## Send the key

Use the exact form `Authorization: Bearer <key>`. The scheme name is case-insensitive.

```bash cURL theme={"theme":{"light":"github-light","dark":"github-dark"}}
curl "https://crispstats.com/api/v1/sites/YOUR_SITE_ID/stats?range=30d" \
  -H "Authorization: Bearer $CRISPSTATS_API_KEY"
```

```javascript JavaScript theme={"theme":{"light":"github-light","dark":"github-dark"}}
const response = await fetch("https://crispstats.com/api/v1/sites/YOUR_SITE_ID/stats?range=30d", {
  headers: { Authorization: `Bearer ${process.env.CRISPSTATS_API_KEY}` },
});
if (!response.ok) {
  const { error } = await response.json();
  throw new Error(`${response.status} ${error.code}: ${error.message}`);
}
const report = await response.json();
console.log(report.summary);
```

```python Python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import os
import requests

response = requests.get(
    "https://crispstats.com/api/v1/sites/YOUR_SITE_ID/stats",
    params={"range": "30d"},
    headers={"Authorization": f"Bearer {os.environ['CRISPSTATS_API_KEY']}"},
)
response.raise_for_status()
print(response.json()["summary"])
```

A header that is present but wrong is always an error. CrispStats never falls back to anonymous access when a key is malformed, revoked or expired, even on the demo site.

## Which sites a key can read

| Request | Result |
| - | - |
| A site of the key's workspace, key covers every site | `200` |
| The one site a site-restricted key was created for | `200` |
| Another site of the same workspace, with a site-restricted key | `403 forbidden`: "That API key is limited to one site, and this is not it." |
| A site of another workspace, or a site id that does not exist | `404 not_found`. The two cases look the same, so a key cannot discover other workspaces' sites. |
| The demo site `demo`, with or without a key | `200` |

`GET /sites` lists exactly the sites the key can read: all sites of the workspace (up to 200, oldest first), or the single site of a site-restricted key. Without a key it lists only `demo`.

## Requests without a key

Without an `Authorization` header you can read the public demo site only:

* `GET /sites` returns the demo site.
* `GET /sites/demo/…` works on every endpoint.
* Any other site returns `401 unauthorized` with the message "Without a key only the demo site (demo) can be read."

Anonymous requests have a lower [rate limit](https://docs.crispstats.com/api-reference/rate-limits.md): 500 a day and 10 every 10 seconds.

## Authentication errors

| Status | Code | Cause | Fix |
| - | - | - | - |
| `401` | `unauthorized` | The header is not `Bearer <key>`. | Send `Authorization: Bearer cs_sk_…`. |
| `401` | `unauthorized` | The key is unknown, revoked or expired. | Create a new key under **API keys** on the workspace page. |
| `401` | `unauthorized` | No key, for a site other than `demo`. | Add the header. |
| `403` | `forbidden` | The key has neither **Read** nor **Admin**. | Create a key with **Read**. |
| `403` | `forbidden` | A key without **Admin** asked for raw events. | Create a key with **Admin**. |
| `403` | `forbidden` | The key is restricted to a different site. | Use a key for this site or one that covers every site. |
| `404` | `not_found` | The site does not exist or belongs to another workspace. | Check the id with `GET /sites`. |

A `401` response carries `WWW-Authenticate: Bearer realm="api"`. All errors use the [error envelope](https://docs.crispstats.com/api-reference/errors.md).

## Revoke or delete a key

Under **API keys** on the workspace page, select **Revoke** to stop a key at once while keeping it in the list as a record, or **Delete** to stop it and remove it from the list. Requests with a revoked or deleted key return `401 unauthorized`. The list shows each key's prefix, scopes, which sites it covers, and when it was created and last used.

## Next steps

> [card] [Manage API keys](https://docs.crispstats.com/account/api-keys.md)
>
> Create, review and revoke keys in the dashboard.

> [card] [Rate limits](https://docs.crispstats.com/api-reference/rate-limits.md)
>
> How many requests each key can make per day and per 10 seconds, and the ceiling all keys of a workspace share.

> [card] [Errors](https://docs.crispstats.com/api-reference/errors.md)
>
> Every error code and how to handle it.


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