# API keys and scopes

API keys (they start with `bm_`) authenticate agents over REST and MCP. A key can be limited to some permissions (**scopes**) and to some inboxes. Keys are created by a signed-in person: in the webmail under **Settings, API keys & MCP, Create key**, or with `POST /api/v1/keys` from a webmail session. A key can't create other keys.

## Scopes

| Scope | Allows |
| --- | --- |
| `mail:read` | List, read, search and wait for mail; attachments and original source; labels and drafts. Reading a message marks it read. |
| `mail:write` | Mark read, star, archive, trash and delete mail; labels, snooze, filters and contacts. |
| `mail:send` | Send, reply, forward and send drafts. |
| `drafts:write` | Create, edit and delete drafts. |
| `inboxes:manage` | Create, rename and delete inboxes; custom domains. |
| `webhooks:manage` | Create and manage webhooks. |
| `admin` | Everything above. This is the default when you don't pass scopes, so keys created before scopes existed keep full access. |

The scope each REST endpoint and MCP tool needs is shown on the [REST API](https://mailforbots.com/docs/rest) and [MCP tools](https://mailforbots.com/docs/mcp) pages. `GET /api/v1/scopes` lists the valid names, and `GET /api/v1/me` shows the `scopes` and `inbox_ids` of the key you are using.

## Restricting to inboxes

`inbox_ids` limits a key to some inboxes (ids or full addresses). For such a key, other inboxes and their messages, threads, drafts and search results look nonexistent (`404`). Account-wide features are refused with `403`: creating inboxes, webhooks, filters, contacts, custom domains and suppressions, and renaming or deleting labels. Creating and applying labels still works.

## Create a key

curl POST /keys (needs a webmail session)

```
curl -X POST "https://mailforbots.com/api/v1/keys" \
  -H "Authorization: Bearer $SESSION_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{ "name": "support agent", "scopes": ["mail:read", "mail:send"], "inbox_ids": ["support@mailforbots.com"] }'
```

`scopes` must be a non-empty array of the names above (otherwise `400`); omit it for `["admin"]`. `inbox_ids` must be a non-empty array, or omitted or `null` for all inboxes. The response contains the full `key` exactly once, next to its `id`, `prefix`, `scopes` and `inbox_ids`. At most 10 keys can be created per hour. Created while acting in a team, the key belongs to the team (see [Teams](https://mailforbots.com/docs/teams)).

## When a key isn't allowed

A missing scope returns `403` over REST, and an error result over MCP:

403 response

```
HTTP/1.1 403 Forbidden

{ "error": "This API key lacks the mail:send scope" }
```

MCP tool error

```
Error: This API key lacks the mail:send scope
```

Inbox-restricted keys get messages such as `Inbox-restricted API keys can't manage webhooks`. Other credentials problems are in [Errors](https://mailforbots.com/docs/errors).

## Guides

- [How to require human approval before an AI agent sends email](https://mailforbots.com/blog/human-approval-for-ai-agent-email)
- [Prompt injection in email: how to let an AI agent read it safely](https://mailforbots.com/blog/read-email-safely-prompt-injection-agents)
- [AI agents and email verification codes: how to handle OTPs safely](https://mailforbots.com/blog/ai-agents-email-otp-verification-codes)
