# API reference

Read your analytics and handle person requests over HTTP, from your server. The machine-readable spec is [/openapi.json](https://datacub.to/openapi.json) (OpenAPI 3.1); this page is generated from it.

## Authentication

Use a read key (`rk_`). The dashboard no longer makes read keys: a key you already have keeps working, and it belongs on your server, never in a browser or app. To handle one person's data without a key, use **One person's data** in [Settings, Privacy](https://datacub.to/en/settings?tab=privacy).

- `/api/backend/...` takes the key as `Authorization: Bearer rk_...`.
- The analytics endpoints take a session: `POST /api/session` with the key returns a session cookie for the calls that follow.

AI tools connect to `/mcp` and sign in with OAuth instead: see [MCP](https://docs.datacub.to/mcp.md). Wherever a path takes a project ID, the short 8-character ID works as well as the long one.

## Endpoints

### POST `/api/session`

Open a session with a read key

- Body: JSON, schema in `/openapi.json`
- Responses: `200` Sets the xs session cookie; `401`; `429`

### DELETE `/api/session`

Close the session

- Auth: session cookie from POST /api/session
- Responses: `200` Closed

### GET `/api/projects`

The organisation's projects

- Auth: session cookie from POST /api/session
- Responses: `200` Your projects, each with your role on it; `401`

### POST `/api/projects/{id}/query`

Ask one analytics question. The body is a typed query model (never SQL). People, event and session lists need the analyst role. Errors are codes such as invalid_query, range_too_long, busy (429) or timeout (504).

- Auth: session cookie from POST /api/session
- Parameters: `id` (path, required)
- Body: JSON, schema in `/openapi.json`
- Responses: `200` {meta, data}; data depends on the kind; `400`; `401`; `403`; `404`; `429`

### DELETE `/api/backend/projects/{id}/persons/{type}/{pid}`

Delete a person's data. A Registered user (type user) with every browser and phone linked to them, or a Guest (type browser)

- Auth: rk_ read key allowed to handle person requests; server only
- Parameters: `id` (path, required), `type` (path, required), `pid` (path, required)
- Responses: `202` {id, status}: status deleted, or held during a legal hold; `400`; `401`; `403`; `429`

### GET `/api/backend/projects/{id}/persons/{type}/{pid}/export`

Export a person's data. Everything datacub holds about them, as one JSON document

- Auth: rk_ read key allowed to handle person requests; server only
- Parameters: `id` (path, required), `type` (path, required), `pid` (path, required)
- Responses: `200` datacub.person_export.v1; `400`; `401`; `403`; `429`

### POST `/api/backend/projects/{id}/consent/revoke`

Report a withdrawal of consent. Deletes what was collected about the person, as a deletion does

- Auth: rk_ read key allowed to handle person requests; server only
- Parameters: `id` (path, required)
- Body: JSON, schema in `/openapi.json`
- Responses: `202` {id, status}: status deleted, or held during a legal hold; `400`; `401`; `403`; `429`

### DELETE `/api/backend/projects/{id}/tombstones/user/{pid}`

Allow a deleted user ID again. Events for this user ID are accepted again from now on; nothing deleted returns

- Auth: rk_ read key allowed to handle person requests; server only
- Parameters: `id` (path, required), `pid` (path, required)
- Responses: `200` {ok: true}; `400`; `401`; `403`; `404`; `429`

### POST `/mcp`

MCP server (Streamable HTTP, JSON-RPC 2.0). Model Context Protocol for AI tools. Every number is exact, the same as the dashboard, and every answer carries a url to the same view. Read only unless the connection was allowed to make changes (scope write:events) and the person is an analyst or above. People one by one (find_people, get_person) need the analyst role; email, name and phone never leave. ?readonly=1, ?features=<areas> and ?tools=<names> narrow the tools. 300 requests a minute per connection.

- Auth: an OAuth access token (the client signs in through /.well-known/oauth-protected-resource/mcp), or a datacub_mcp_ token
- Body: JSON, schema in `/openapi.json`
- Responses: `200` JSON-RPC response; `401`; `429`


## Sending events

The SDKs send `POST /v1/batch` to the ingestion host, with up to 100 events. You do not call it yourself; if you proxy it, pass the body and the response status (including `Retry-After`) through unchanged. Responses are listed in [troubleshooting](https://docs.datacub.to/reference/troubleshooting#responses-from-datacub).
