# API overview

> mettr's HTTP API: the keys, the endpoints for events, history, subscriptions and annotations, the limits, and what each error means.

Source: https://mettr.app/docs/api

Everything the SDKs do is a plain HTTP request you can make from any language. The API takes JSON and answers JSON, at `https://mettr.app/api/v1`.

## Keys

Each site has two kinds of key, both under the site's settings, **Tracking**:

- The **public key** (`pk_…`) is for the browser. It goes in the request body as `key`, since a browser's `sendBeacon` cannot set headers, and is taken only from pages on the site's own domains (the request's `Origin`). It may send `pageview`, `event`, `engagement` and `presence`, and none of the fields that would let a page speak for your server (`ts`, `user_id`, `ip`, `user_agent`, `revenue`).
- A **secret key** (`sk_…`) is for your server. It goes in the `Authorization` header as a bearer token, never in a URL, and may send everything. A site can have several (one per app or environment): each is shown once when it is made, kept by mettr only as a hash, shows when it was last used, and can be revoked at once.

```http
POST /api/v1/events HTTP/1.1
Host: mettr.app
Authorization: Bearer sk_...
Content-Type: application/json

{"type": "event", "name": "signup"}
```

## Endpoints

| Endpoint | Key | What it takes |
|---|---|---|
| `POST /api/v1/events` | public or secret | [Events](/docs/events-api), live: one, or up to 100 in a batch |
| `POST /api/v1/import` | secret | [History](/docs/import-api): up to 1,000 past events a batch |
| `POST /api/v1/subscriptions` | secret | [Subscriptions](/docs/subscriptions-api) from your own billing, for MRR |
| `POST /api/v1/annotations` | secret | An [annotation](/docs/annotations-api) under the traffic chart: a deploy, a release, a campaign |
| `POST /api/v1/deploys/{hook}` | the hook's secret | A [deploy](/docs/deploys) from a repository's webhook or a CI step |
| `GET /api/v1/sites…` | a person's token | The [widget API](/docs/widget-api): read only, for widgets and scripts |

## Bodies

- JSON, sent as `application/json`. The ingest reads the body as JSON whatever its type, so the browser can send `text/plain` with no CORS preflight.
- Gzip is welcome: send `Content-Encoding: gzip`.
- Events, subscriptions: at most 256 KB as sent and 1 MB unzipped. Import: 2 MB and 8 MB.
- Every field is known: a field mettr does not know is refused, not ignored, so a typo shows at once.

## Limits

- 6,000 requests a minute per key, and 600 a minute per address for the public key (an office behind one address is many browsers).
- Annotations and deploys: 60 a minute.
- The month's events are counted against the team's [plan](/docs/plans-and-limits). Past the plan's events and a fifth more, events are refused with `402` until the plan changes or the month turns. Presence is still taken, so the dashboard still shows who is online.

## Errors

Errors answer JSON with a `message`, and for a body that does not fit, the `errors` of each field. In a batch, a field is named by the event's place in it, and one bad event refuses the whole batch, so nothing is dropped in silence:

```json
{
    "message": "Only a secret key may send this field. (and 1 more error)",
    "errors": {
        "events.0.user_id": ["Only a secret key may send this field."],
        "events.3.props": ["Prop \"plan\" must be a string, number, boolean or null."]
    }
}
```

| Status | Means |
|---|---|
| `202` | Accepted: `{"accepted": 3}`. Events are on the dashboard within a second or two |
| `400` | The body is not a JSON object, or not valid gzip, or unzips past its cap |
| `401` | No key, or a key that is not valid (revoked, mistyped, another site's) |
| `402` | The team's plan has used its events for the month, or the feature needs a paid plan |
| `403` | A public key from a page that is not on one of the site's domains |
| `413` | The body is too large |
| `422` | The body does not fit: see `errors` |
| `429` | Too many requests: wait for the minute to turn (the `Retry-After` header says how long) |

A `5xx` is ours: retry later with the same body, and see the [status page](/status) for what is going on. A secret key may send `ts`, the time an event happened (up to 7 days back), so a queue can retry without moving events in time.
