# Authentication

> API keys, session tokens, browser sign-in, and which endpoints need neither.

Source: https://openpipe.health/docs/authentication/

Send a bearer token on every request under `/api/`, except the public endpoints listed below.

```http
Authorization: Bearer oph_your_key_here
```

## Two kinds of token

| Token | Looks like | Use it for |
|---|---|---|
| API key | Starts with `oph_` | Servers, scripts, notebooks and the [MCP server](https://openpipe.health/docs/mcp.md). Everything except key management. |
| Session token | A short-lived JWT issued to a signed-in person | The browser app. Required to create or revoke API keys. |

The two do not always resolve to the same place:

- An **API key** always acts in the [workspace](https://openpipe.health/docs/workspaces.md) that created it.
- A **session token** acts in your personal workspace, unless the request also sends `X-Oph-Workspace: <organization id>` to pick an organization workspace you belong to.

Credits, usage and keys belong to the workspace, so a key and a session share a balance only when they act in the same workspace.

## API keys

- Create and revoke keys in the app, or with [`POST /api/keys`](https://openpipe.health/docs/api/account.md#create-a-key) using a session token.
- In an organization workspace, only owners and admins can create or revoke keys. Members get `403`.
- The full key is returned once, at creation. Only a SHA-256 hash is stored. Afterwards you can see its 12-character prefix, its name and when it was last used.
- A workspace can have 20 active keys. Creating a 21st returns `409`. Revoked keys do not count.
- A revoked key stops working on the next request.
- An API key cannot create or revoke keys. Those calls return `403` with `Sign in to do that; an API key cannot.`
- A key is not tied to the person who created it. It keeps working if that person leaves the workspace.

Keep keys on a server. Any key can spend its workspace's credits.

### The `X-Oph-Token` header

If a proxy between you and the API replaces the `Authorization` header with its own credentials, send the same token in `X-Oph-Token` instead. When both are present, `X-Oph-Token` wins.

```http
X-Oph-Token: oph_your_key_here
```

## Public endpoints

These need no token:

| Endpoint | Returns |
|---|---|
| `GET /api/health` | Database and service status |
| `GET /api/config` | Values the browser app needs to start |
| `GET /api/meta` | The loaded datasets, their versions and row counts |

The sign-in endpoints under `/api/auth/` take no bearer token either. They are described next.

## Browser sign-in

The endpoints under `/api/auth/` are how the web app signs people in. They are **not for API-key clients**: they work with a cookie, not a bearer token, and they may change without notice. If you are calling the API from code, use an API key and skip this section.

How it works:

1. The app posts credentials to one of the sign-in endpoints. On success the server sets an `oph_session` cookie: `HttpOnly`, `SameSite=Lax`, scoped to `/api/auth`, lasting 7 days.
2. The app calls `GET /api/auth/token` with that cookie and gets `{ "token": "…" }`, a short-lived JWT.
3. The app sends the JWT as `Authorization: Bearer …` to the rest of the API, and fetches a new one before it expires.

| Endpoint | Body | Does |
|---|---|---|
| `POST /api/auth/sign-up` | `email`, `password` | Creates an account with a password and signs in |
| `POST /api/auth/sign-in` | `email`, `password` | Signs in with a password |
| `POST /api/auth/code` | `email`, `purpose` | Emails a 6-digit code. `purpose` is `sign-in` (default), `verify` or `reset`. |
| `POST /api/auth/code/sign-in` | `email`, `code` | Signs in with the emailed code, creating the account if there is none. Also confirms the email address. |
| `POST /api/auth/code/verify` | `email`, `code` | Confirms the signed-in person's email address. Needed before [accepting an invitation](https://openpipe.health/docs/workspaces.md#invitations). |
| `POST /api/auth/code/reset` | `email`, `code`, `password` | Sets a new password using a `reset` code |
| `GET /api/auth/token` | | Returns a session token for the cookie |
| `GET /api/auth/me` | | Returns `user`: `id`, `email`, `name`, `emailVerified` |
| `POST /api/auth/sign-out` | | Ends the session and clears the cookie |

Things to know:

- Passwords are 8 to 128 characters.
- `POST` bodies must be JSON sent as `application/json`, at most about 8 KB.
- `POST /api/auth/code` answers `{ "sent": true }` whether or not the email has an account.
- Errors carry a `code` next to the message, for example `{ "error": "Wrong email or password.", "code": "INVALID_EMAIL_OR_PASSWORD" }`.
- Sign-in attempts are rate limited per client address and per email. See [Rate limits](https://openpipe.health/docs/api/errors.md#rate-limits).

Workspaces, members and invitations are also managed under `/api/auth/`. See [Workspace endpoints](https://openpipe.health/docs/workspaces.md#workspace-endpoints).

## Failures

| Status | Message | Cause |
|---|---|---|
| `401` | `Missing Authorization: Bearer <api key>.` | No header, or not a bearer token. The response has a `WWW-Authenticate: Bearer` header. |
| `401` | `Unknown or revoked API key.` | The `oph_` key does not match an active key |
| `401` | `Invalid or expired session token.` | The JWT failed verification |
| `403` | `You are not a member of that workspace.` | `X-Oph-Workspace` names an organization you do not belong to, or one that does not exist |
| `403` | `Sign in to do that; an API key cannot.` | Key management called with an API key |
| `403` | `Only workspace owners and admins can do that.` | Key management called by a member of an organization workspace |

## CORS

Under `/api/`, the API allows any origin, the `Authorization`, `Content-Type`, `X-Oph-Token` and `X-Oph-Workspace` headers, and `GET`, `POST`, `DELETE` and `OPTIONS`. The [rate limit headers](https://openpipe.health/docs/api/errors.md#rate-limits) are exposed to browser code. Calling the API from a browser works, but puts your key in front of your users; prefer a session token there.
