# Authentication

> Source: https://developers.anymail.watch/authentication/

Every request to `/api/v1/` and to the MCP server carries a token in the `Authorization` header.

```
Authorization: Bearer sgn_…
```

There are two kinds of token. Both work on the REST API and on the MCP server, with the same scopes, the same [rate limit](/rate-limits/) and the same workspace isolation.

| | API token | OAuth token |
|---|---|---|
| Prefix | `sgn_` | `amo_` |
| Made by | You, in **Settings** | An MCP client, after you allow it in the browser |
| Lifetime | Until you revoke it | One hour, renewed by the client with a refresh token |
| Use it for | Scripts, servers, Zapier, the [CLI](/cli/) | Claude, ChatGPT, Claude Code, Cursor. See [MCP server](/mcp-server/). |

## Generate and revoke API tokens

Open **Settings**, then **Integrations**, then the **API keys** tab. The **API tokens** section lists your tokens and has the **Generate token** button. Pick the **Access** of the token before you generate it: **Read & write** or **Read only**.

- A token is `sgn_` followed by 43 characters.
- The app shows the full token once, when you generate it. anymail stores only a hash of it, so nobody can show it again.
- **Revoke** disables a token. A revoked token returns `401`.
- The token list shows the name, the first characters of each token, the access, the creation date and the date of last use. Use the last use date to find tokens that are safe to revoke.

## Scopes

A token has the `read` scope, or the `read` and `write` scopes.

| Scope | Allows |
|---|---|
| `read` | `GET` requests: lists, single objects, statistics. |
| `write` | `POST`, `PATCH` and `DELETE` requests: create, change, delete, start and stop campaigns, send replies. |

The method decides the scope, so every endpoint is covered without a list to keep up to date. A read only token is the right choice for reporting, a dashboard or an LLM client that should look but not touch.

A write with a read only token returns `403` and changes nothing:

```
HTTP/1.1 403 Forbidden
WWW-Authenticate: Bearer realm="anymail API", error="insufficient_scope", scope="write"
Content-Type: application/json

{"error":"insufficient_scope: this token lacks the \"write\" scope.","code":"insufficient_scope"}
```

On the MCP server every request is a `POST`, so connecting needs only `read`. A write tool called with a read only token returns the same `403` as a tool error.

Tokens generated before scopes existed have both scopes.

## Workspace

A token gives access to one workspace. Everything the API returns belongs to that workspace. An id from another workspace behaves like an id that does not exist and returns `404`.

To make a request fail when it would reach the wrong workspace, send the `X-Workspace` header with the workspace id. A mismatch returns `403` with `code: "workspace_forbidden"`. See [Workspaces](/workspaces/#the-x-workspace-header).

## OAuth for MCP clients

anymail runs an OAuth 2.1 authorization server for the MCP endpoint, so a client such as Claude or ChatGPT connects without a pasted token. You add the server URL in the client, the client opens anymail in the browser, you sign in and press **Allow**.

On the consent screen you choose:

- **Workspace.** The workspace this connection may reach, when you are a member of more than one.
- **Access.** **Read only** (campaigns, prospects, companies, replies and stats) or **Read and write** (also add prospects, enrol them and change campaigns). This becomes the scope of the token.

**Deny** sends the client away without a token.

| Item | Value |
|---|---|
| Authorization server metadata | `https://app.anymail.watch/.well-known/oauth-authorization-server` |
| Protected resource metadata | `https://app.anymail.watch/.well-known/oauth-protected-resource` |
| Authorization endpoint | `https://app.anymail.watch/api/oauth/authorize` |
| Token endpoint | `https://app.anymail.watch/api/oauth/token` |
| Client registration | `https://app.anymail.watch/api/oauth/register` (dynamic client registration, RFC 7591) |
| Resource | `https://app.anymail.watch/api/mcp` |
| Grants | `authorization_code` with PKCE `S256` (required), `refresh_token` |
| Client authentication | None. Clients are public. |
| Scopes | `read`, `write` |
| Access token | `amo_…`, valid for one hour |
| Refresh token | `amr_…`, valid for 30 days, replaced on every use |

Redirect URIs must use `https`, `http` on a loopback address (`localhost`, `127.0.0.1`, `[::1]`, any port) or a private app scheme. A `401` from the MCP endpoint carries `WWW-Authenticate: Bearer resource_metadata="https://app.anymail.watch/.well-known/oauth-protected-resource"`, which is how a client finds the authorization server.

A refresh token used twice ends every token of that client, user and workspace, because reuse means a copy leaked. A person removed from the workspace loses access at once, even with a valid token.

## Where the token goes

The API accepts a token only in the `Authorization` header. A token in a query string is ignored, because URLs end up in access logs and browser history.

## Responses

| Situation | Status | Body |
|---|---|---|
| No `Authorization` header, or not a `Bearer` scheme | `401` | `{"error":"Missing bearer token."}` |
| Malformed, unknown, expired or revoked token | `401` | `{"error":"Invalid API token."}` |
| A write with a read only token | `403` | `{"error":"insufficient_scope: …","code":"insufficient_scope"}` |
| `X-Workspace` names another workspace | `403` | `{"error":"This token does not have access to the workspace named in X-Workspace.","code":"workspace_forbidden"}` |

A `401` response from the REST API carries `WWW-Authenticate: Bearer realm="anymail API"`. The message is the same for an unknown token and a revoked one, so a response never confirms that a token exists.

A request with a wrong token never produces a `500`.

## Keep tokens safe

- Store tokens in a secret manager or an environment variable.
- Create one token per integration, so you can revoke one without touching the others.
- Give an integration a read only token when it does not write.
- The `sgn_` prefix lets secret scanners find a token that leaks into a repository. Revoke it at once if that happens.

## Related

- [Quick start](/quick-start/)
- [MCP server](/mcp-server/)
- [Workspaces](/workspaces/)
- [Rate limits](/rate-limits/)
