# API keys

Organization API keys let your own backend or scheduled jobs query your organization's data without a person's session. An owner creates them in Settings with a read, write or import scope and an expiry; you send one as Authorization: Bearer chrly_org_... on the endpoints its scope reaches.

Only organization owners create, list and revoke API keys. A key is shown
once, when it is created: Cohorly stores only a hash of it. Treat it like a
password and keep it on your servers, never in a browser or a mobile app.

On Cohorly Cloud, API keys need a plan that includes them (a self-hosted server
has every feature). Without it, creating a key answers `403` with
`code: "plan_feature_required"` and `feature: "apiKeys"`, and so does every
request made with a key already created, until the organization is on such a
plan again. Owners can still list and revoke keys.

## Create a key

In the dashboard, open **Settings > Account > API keys** and choose
**Create key**. Give it:

- a **name** (1 to 80 characters), so you know later what uses it;
- a **scope**, `read`, `write` or `import` (see below);
- a **validity** of 30, 90, 180 or 365 days. Every key expires; 90 days is
  the default.

Copy the value from the dialog. It starts with `chrly_org_` and it is not
shown again. If you lose it, revoke it and create a new one.

An organization has at most **20 live keys** (not revoked, not expired), including any whose creator left.

## Use a key

Send it on the `Authorization` header, exactly like an account token:

```bash
curl -X POST https://cohorly-service.velloalabs.com/api/query/segmentation \
  -H "Authorization: Bearer $COHORLY_ORG_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "projectId": 42,
    "event": "Signed Up",
    "from": "2026-07-01",
    "to": "2026-07-31",
    "unit": "day",
    "type": "unique"
  }'
```

```js
const res = await fetch("https://cohorly-service.velloalabs.com/api/query/segmentation", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.COHORLY_ORG_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    projectId: 42,
    event: "Signed Up",
    from: "2026-07-01",
    to: "2026-07-31",
    unit: "day",
    type: "unique",
  }),
});
```

A key belongs to one organization and reaches every project in it. Name the
project with `projectId`; without it, requests use the organization's oldest
project. List the projects with `GET /api/projects`; for a key the rows leave
out the project's ingestion token.

The key acts on behalf of the owner who created it, with the permissions of
a member. An `import` key is the one exception: it imports with the
permissions of an owner, for as long as the person who created it is still an
owner. If that person leaves the organization, the key stops working.

## Scopes

| Scope    | Reaches                                                                                           |
| -------- | ------------------------------------------------------------------------------------------------- |
| `read`   | Every analysis query and the read endpoints listed below.                                         |
| `write`  | Everything `read` reaches, plus creating, editing and deleting saved reports, boards and cohorts. |
| `import` | Load historical data. Reaches only the import endpoints and the project list.                     |

### Endpoints a `read` key reaches

- `GET /api/projects`
- `GET /api/events`, `GET /api/events/names`, `GET /api/events/properties`, `GET /api/events/renames`
- `GET /api/users`, `GET /api/users/{distinctId}`
- `GET /api/stats`, `GET /api/collection-health`
- `GET /api/lexicon`, `GET /api/lexicon/usage`, `GET /api/profiles/properties`
- `GET /api/reports`, `GET /api/reports/{id}`
- `GET /api/boards`, `GET /api/boards/{id}`
- `GET /api/cohorts`, `GET /api/cohorts/{id}`, `GET /api/cohorts/{id}/members`, `POST /api/cohorts/preview`
- `GET /api/definitions`, `GET /api/annotations`
- `GET /api/errors/issues`, `GET /api/errors/issues/{id}`, `GET /api/errors/fingerprints`, `GET /api/errors/releases`
- `POST /api/query`, `POST /api/query/drilldown`, and `POST /api/query/segmentation`, `funnel`, `retention`, `flows`, `sessions` and `metric` (see [Queries](/api/queries))

### Endpoints a `write` key adds

- `POST /api/reports`, `PUT /api/reports/{id}`, `DELETE /api/reports/{id}`
- `POST /api/boards`, `POST /api/boards/starter`, `PUT /api/boards/{id}`, `DELETE /api/boards/{id}`
- `POST /api/cohorts`, `PUT /api/cohorts/{id}`, `DELETE /api/cohorts/{id}`

### Endpoints an `import` key reaches

- `GET /api/projects`, to check the project you import into
- `POST /api/import/events?projectId=...`
- `POST /api/import/profiles?projectId=...`

Nothing else, not even the read endpoints above. Importing is an owner's
action: if the person who created the key is no longer an owner, the import
endpoint answers `403 { "error": "forbidden" }` (the project list still
works). `read` and `write` keys cannot import. An `import` key expires like
any other key, so pick a validity that covers your whole backfill.

Every other endpoint refuses a key, whatever its scope: organization members
and invites, other credentials (API keys, MCP tokens, upload tokens), the
audit log, event export, data dictionary changes, feature flags,
alerts, privacy requests, session replays and the backoffice. Keys are not
accepted by the MCP server either; use an [MCP token](/api/mcp) there.

## Key status

Each key in `GET /api/orgs/current/api-keys` (owner only) carries a `status`:

| Status            | Meaning                                                                                                                                                              |
| ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `active`          | The key authenticates.                                                                                                                                               |
| `revoked`         | An owner revoked it.                                                                                                                                                 |
| `expired`         | Its expiry date passed.                                                                                                                                              |
| `creator_removed` | The person who created it is no longer a member of the organization (or was removed and invited again later). The key answers `401`: revoke it and create a new one. |

When more than one applies, the first in this order wins: `revoked`,
`expired`, `creator_removed`, `active`. The status is decided by the same
check that authenticates a request, so any status other than `active` is a
`401`. In Settings, a `creator_removed` key is shown as inactive and can still
be revoked. The limit of 20 counts live keys (not revoked and not expired),
so a `creator_removed` key still counts until you revoke it.

## Errors

| Status | Body                                                                            | Meaning                                                                                                           |
| ------ | ------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| `401`  | `{ "error": "unauthorized", "code": "authentication_required" }`                | The key is unknown, revoked or expired, or its creator left the organization (`creator_removed` in the key list). |
| `403`  | `{ "error": "forbidden", "code": "api_key_not_allowed" }`                       | Keys cannot call this endpoint.                                                                                   |
| `403`  | `{ "error": "forbidden", "code": "api_key_scope" }`                             | The endpoint needs a `write` key.                                                                                 |
| `403`  | `{ "error": "forbidden" }`                                                      | The `projectId` belongs to another organization, or an `import` key's creator is no longer an owner.              |
| `409`  | `{ "error": "cohort is used by feature flags", "code": "api_key_flag_cohort" }` | A key cannot change the definition of a cohort a feature flag targets. Change it from the dashboard.              |
| `403`  | `{ "code": "two_factor_required" }`                                             | The organization requires two-factor authentication and the key's creator has not turned it on.                   |
| `429`  | `{ "status": 0, "error": "rate limited" }`                                      | The key went over its rate limit. Wait for the seconds in `Retry-After`.                                          |

## Limits

Each key has its own budget, separate from the dashboard of the person who
created it:

| Limit                                                                                                | Default          |
| ---------------------------------------------------------------------------------------------------- | ---------------- |
| Requests per key, any endpoint                                                                       | 120 / 60 seconds |
| `/api/query/*`, error issue reads, cohort reads and writes (not delete) and `GET /api/users` per key | 30 / 60 seconds  |

Analysis queries also have a time limit; a query that runs too long answers
`504` with `code: "query_timeout"`. A query that would read too many rows
answers `422` with `code: "query_too_large"`.

## Revoke a key

In **Settings > Account > API keys**, choose **Revoke** on the key. It
answers `401` from its next request. Creating and revoking keys, and every
report, board or cohort a key changes, are recorded in the organization's
audit log. So is each import made with an `import` key.
