# Tokens and scopes

> Personal tokens and OAuth tokens, the eight scopes, project and IP limits, expiry, revocation, and what a token can never do.

## Token types

| Prefix | Type | How you get it | Lifetime |
|---|---|---|---|
| `sysnee_pat_` | Personal token | **Console → Security → Tokens** | 1 to 365 days (default 90) |
| `sysnee_oat_` | OAuth access token | An app's sign-in (MCP, CLI) | 1 hour |
| `sysnee_ort_` | OAuth refresh token | Issued with the access token | 30 days without use, renewed on each use |

All are opaque: 36 random characters and a 6-character checksum after the prefix. The prefixes exist so secret scanners can recognize a leaked token.

Sysnee only stores an HMAC of the token. A personal token is shown **once**, when you create it. If you lose it, create another.

Send the token in the header:

```bash
curl -H "Authorization: Bearer $SYSNEE_TOKEN" https://api.cloud.sysnee.com/v1/resources/deploy
```

## Scopes

| Scope | Allows | Who can have it |
|---|---|---|
| `account:read` | See the user, organization and projects | Everyone |
| `sites:read` | See sites, deployments, logs, domains and analytics | Everyone |
| `sites:deploy` | Create sites, deploy, roll back, add and verify domains | Everyone |
| `sites:delete` | Delete sites and remove domains | Owner |
| `databases:read` | See databases, without passwords | Everyone |
| `databases:write` | Create, stop and start databases | Owner, account level 3 |
| `databases:delete` | Request database deletions | Owner |
| `billing:read` | See usage, cost and invoices | Everyone |

What a token can do is the **intersection** of its scopes and the user's role in the organization. If the user's role changes, the token loses what the role lost.

The routes of each scope are in the [API reference](https://docs.sysnee.com/en/api.md) and in [openapi.json](https://docs.sysnee.com/openapi.json) (field `x-required-scope`).

## Create a personal token

In **Console → Security → Tokens → New token**:

- **Name** (up to 100 characters), so you know where it is used.
- **Scopes:** check only what you need.
- **Projects:** all, or up to 50 specific ones. A token limited to projects can't see or create anything outside them.
- **IP ranges** (optional): up to 20 IPv4 or IPv6 ranges in CIDR. Outside them, the token is refused.
- **Expiry:** 1 to 365 days.

Requirements:

- A verified email.
- Scopes that change resources (`sites:deploy`, `sites:delete`, `databases:write`, `databases:delete`) need two-factor authentication turned on and confirmed in the last 10 minutes.
- `databases:write` and `databases:delete` need account level 3, the same requirement as creating databases.
- At most 20 active personal tokens per user.

You get an email for every token created and every app authorized.

## Roll and revoke

- **Roll** issues a new token with the same settings and invalidates the old one at once. Tokens with write scopes ask for your second factor.
- **Revoke** invalidates the token at once.
- The list shows each token's last use and last IP.

OAuth apps are in **Console → Security → Authorized apps**. Revoking an app invalidates all its tokens. An app unused for 90 days is revoked on its own.

If a token leaked, revoke it first and investigate after: the **Activity** tab shows what each token did.

## What a token can't do

Some actions stay in the console, with a person's session:

- Delete a database. With a token, `POST /v1/resources/postgres/{id}/deletion-request` files a request someone confirms in the console.
- See a database's password.
- Create, roll or revoke tokens, and authorize apps.
- Change password, email, second factor, members, Git integrations and billing details.

Routes not listed in the [API reference](https://docs.sysnee.com/en/api.md) refuse tokens.

## Errors

| Status | Reason |
|---|---|
| `401` | Invalid, expired or revoked token |
| `403` | Missing scope, project outside the token, IP not allowed, or a console-only route |
| `429` | Rate limit: 120 requests per minute per token, 30 uploads per hour per token |

The error message names the missing scope, project or step, in Portuguese or English depending on `Accept-Language`.

## Audit

Every write made with a token or app records an event with the token, the app, the resource, the IP and the result. Token and app creation, device sign-ins, revocations, and deletion requests and confirmations are recorded too. Events are kept for 365 days in **Console → Security → Activity**.
