
# Authentication

The API uses bearer token authentication. Tokens belong to your workspace and can reach every site in it, limited by the scopes chosen at creation. API tokens are a Pro plan feature, and only the workspace owner can create or revoke them.

## Creating a token

1. Log in to the dashboard and open Account.
2. In the Tokens section, click "Create new token".
3. Give it a descriptive name, such as "GitHub Actions" or "Phone shortcut".
4. Choose the scopes the token needs (all are selected by default).
5. Copy the token immediately. It is shown only once.

Tokens start with `sp_`. You can have up to 10 active tokens; revoke one if you have reached the limit. A token that has expired no longer counts toward it. A token's lifetime is chosen at creation: 30, 90, 180, or 365 days, or never expiring. The default is 365 days, so plan to rotate them about once a year.

## Using a token

Include the token in the `Authorization` header of every request:

```bash
curl https://sitepaste.com/api/v1/public/sites/default/pages \
  -H "Authorization: Bearer sp_your_token_here"
```

## Scopes

Every token carries a set of scopes chosen when it is created. Each API route requires one scope, and a token without it is refused. Scopes cannot be changed after creation; create a new token instead.

| Scope | Grants |
|-------|--------|
| `content` | Pages, authors, events, event types, sections, tags, and the pages export |
| `media` | Media files, folders, ordering, and uploads |
| `settings` | Workspace settings, sites, site settings, and domains |
| `deploy` | Deployments, pending changes, previews, and taking a site offline |
| `data` | Contact submissions, subscribers, metrics, and the subscriber export |

An export carries the scope of the collection it belongs to. The zip representation of `GET /sites/{siteId}/pages` needs content, and the CSV representation of `GET /sites/{siteId}/newsletter-subscribers` needs data.

Content and media overlap in one place. [Inline media](/api/media/inline-media) is part of the pages request carrying it, so a content token can add files to the library, while media gates the standalone media routes that list, rename, move, and delete files.

Deploy does not overlap with any other scope. Publishing with [POST /sites/{siteId}/deployments](/api/deployments/post-deployments) needs deploy, so a token holding content alone can write and rewrite your pages but cannot publish any of them. Use content and deploy together for a publishing pipeline, and content on its own for an agent that drafts but must never publish.

A leaked token can only do what its scopes allow, so give automation the narrowest set that covers its job. Neither the pipeline nor the drafting agent needs settings.

A request missing its scope returns 403, with the scope it needs in `scope`:

```json
{
  "error": "this token does not carry the \"media\" scope",
  "code": "token_scope_required",
  "scope": "media"
}
```

A pipeline whose token lacks deploy writes its pages normally and is refused at the deploy, and the content it wrote stays saved. Mint a token that carries `deploy`, or deploy from the dashboard, and post to the deploy route again.

## Storing tokens safely

Keep the token in an environment variable rather than hardcoding it:

```bash
# In your shell profile or .env file (add .env to .gitignore)
export SITEPASTE_API_TOKEN="sp_your_token_here"
```

In GitHub Actions, use a repository secret:

```yaml
env:
  SITEPASTE_TOKEN: ${{ secrets.SITEPASTE_TOKEN }}
```

## Managing tokens

The Tokens section of the Account page lists your active tokens with their last-used times, and lets you revoke any of them permanently.

## Error responses

401 Unauthorized means the token is missing, malformed, revoked, or expired:

```json
{"error": "missing authorization header", "code": "unauthorized"}
{"error": "invalid authorization format, expected: Bearer <token>", "code": "unauthorized"}
{"error": "invalid or revoked token", "code": "unauthorized"}
```

503 Service Unavailable means the token could not be checked at all. Retry the request. There is no need to rotate the token:

```json
{"error": "could not verify the token, try again", "code": "service_unavailable"}
```

403 Forbidden means the workspace is not on the Pro plan:

```json
{"error": "API tokens require Pro plan", "code": "forbidden"}
```

A 403 with `"code": "payment_past_due"` means the subscription payment failed; requests that modify content are blocked until billing is fixed. Reads keep working.