# Personal access tokens and API keys

> Long-lived tokens that act as a user, and the publishable and secret keys of a project, with their formats, scopes and how to revoke them.

Applies to: the Sypnia engine (open source, self-hosted) and Sypnia Cloud (paid service).  
Source: https://data.docs.sypnia.com/machines/tokens-and-api-keys/

For agents: PATs: syp_pat_ + 40 base62 + 6-char checksum (secret-scanning friendly), stored as SHA-256 + last 4, owner user, scopes, optional resource, expires_at nullable (never), shown once, used as Authorization: Bearer; acts as its user limited by scopes. Project API keys: publishable syp_pk_<ref>_… (browser-safe, role anon) and secret syp_sk_<ref>_… (server only, role service_role, bypasses RLS, several per project, hashed, revocable); sent as apikey header or Authorization: Bearer; secret keys from a browser (Origin header) are refused. Protected keys: protected true, DELETE answers 409 protected_key, POST /auth/v1/admin/api-keys/{id}/rotate-protected replaces it atomically; the Cloud console key is protected.

## Personal access tokens

A personal access token (PAT) acts as **one user**, limited to the scopes it was given. Good for scripts, integrations and CI.

```text
syp_pat_abcdefghijklmnopqrst_4Gk9qL2mZ8xR7vT1nB5cW3yH6jD0sF2aE8uP4iO1x7Kd2Q
```

- The project's ref, then 40 random characters (238 bits) and a 6-character checksum, so secret scanners can recognise a real token without calling us. The ref lets the [MCP server](https://data.docs.sypnia.com/machines/mcp) find the project. Tokens made before the ref was added (`syp_pat_` + 46 characters) still work.
- Stored as a SHA-256 hash and its last 4 characters; **shown once**.
- `expires_at` can be a date or **never**. Every token can be revoked at any time.
- Send it as `Authorization: Bearer syp_pat_…` to the Data API or SQL.

Create them in the console → Account → Tokens, or with `POST /auth/v1/tokens` while signed in. The token's rights are the **intersection** of the user's rights and its scopes: a PAT never does more than its owner. Asking for `sql:*`, `auth:admin` or `project:admin` fails with `403 scope_not_allowed` unless an admin granted them to the user ([who may hold them](https://data.docs.sypnia.com/machines#who-may-hold-the-privileged-scopes)). PATs are created only from the person's own session, never with another PAT or an app's token. In your policies a PAT, like a token an app got for the person, carries the person's **current** `app_metadata` and `user_metadata` in `auth.jwt()`, read again within a minute of any change.

## API keys

Each project has two kinds of keys.

|               | Publishable `syp_pk_<ref>_…`                             | Secret `syp_sk_<ref>_…`                         |
| ------------- | -------------------------------------------------------- | ----------------------------------------------- |
| Where         | browsers, phones, anywhere                               | servers only                                    |
| Database role | `anon`, or the user's role when a user token is sent too | `service_role`: **bypasses row-level security** |
| How many      | one (rotatable)                                          | several, each with a name                       |
| Stored        | can be shown again                                       | as a hash; last 4 shown                         |

Send a key in the `apikey` header (as Supabase does) or as `Authorization: Bearer`. A secret key sent from a browser, a request with an `Origin` header, is refused with `secret_key_in_browser`.

> **Warning**
>
> A secret key can read and change everything. Keep it in your server's environment or a secrets manager, never in your app's code or in a chat.

## Protected keys

A key created with `"protected": true` can't be revoked on its own: `DELETE /auth/v1/admin/api-keys/{id}` answers `409 protected_key`. It is replaced with `POST /auth/v1/admin/api-keys/{id}/rotate-protected`, which creates the new key and revokes the old one in one transaction, so whatever depends on it never loses its way in. In Sypnia Cloud the console's own key of each project is protected; **Machines → Rotate the console key** replaces it.

## Secret scanning

The prefixes `syp_pat_`, `syp_sk_` and `syp_cs_` are registered for GitHub secret scanning: when one is pushed to a public repository, it is reported to us and revoked, and the owner is told.
