# Authentication

Every request carries an API key. The key decides what the request may do.

## Send the key

Put the key in the `Authorization` header of every request.

```http
Authorization: Bearer os_...
```

Without a key, or with one that is wrong or revoked, the answer is `401` with the code `unauthorized`. To see who a key is and what it may do, ask for the account.

cURL:

```bash
curl "$OPENSEND_API_URL/v1/account" \
  -H "Authorization: Bearer $OPENSEND_API_KEY"
```

TypeScript:

```ts
await opensend.account.get()
```

CLI:

```bash
opensend account get
```

MCP:

```json
{
  "name": "account_get",
  "arguments": {}
}
```

account.get:

```json
{
  "object": "account",
  "kind": "key",
  "user": null,
  "workspaces": [
    {
      "id": "ws_demo",
      "name": "Acme Mail",
      "role": null,
      "current": true
    }
  ]
}
```

## What a key may do

A key has one of two levels of access. You choose it when you make the key.

- `full`: every method of the API.
- `sending`: sending email, and nothing else. It can also read the method catalog and its own account.

A `sending` key can be limited to one domain with `domainId`. It then sends from that domain only, and an email from another domain is refused with `key_domain`. Use one for each app that sends, so a leaked key can do little.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/api-keys" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "Marketing site",
  "access": "sending"
}'
```

TypeScript:

```ts
await opensend.apiKeys.create({
  name: 'Marketing site',
  access: 'sending',
})
```

CLI:

```bash
opensend api-keys create --name 'Marketing site' --access sending
```

MCP:

```json
{
  "name": "api_keys_create",
  "arguments": {
    "name": "Marketing site",
    "access": "sending"
  }
}
```

## Scopes

Every method needs one scope, named on its page in the reference. A request without it gets `403` with the code `forbidden`. The table lists each scope, whether a `sending` key has it, and which roles do.

| Scope | What it opens | Sending key | Roles |
| --- | --- | --- | --- |
| `emails:send` | Send an email or a batch, and move or cancel a scheduled email. | Yes | owner, admin, member |
| `emails:read` | Read sent emails and received emails. | No | owner, admin, member, viewer |
| `broadcasts:read` | Read broadcasts. | No | owner, admin, member, viewer |
| `broadcasts:write` | Write, send and delete broadcasts. | No | owner, admin, member |
| `audiences:read` | Read audiences and their contacts. | No | owner, admin, member, viewer |
| `audiences:write` | Add and delete audiences, and add, change and delete their contacts. | No | owner, admin, member |
| `templates:read` | Read templates. | No | owner, admin, member, viewer |
| `templates:write` | Write, publish and delete templates. | No | owner, admin, member |
| `automations:read` | Read automations and their runs. | No | owner, admin, member, viewer |
| `automations:write` | Write, publish, test, pause, resume and delete automations, enrol people and cancel runs. | No | owner, admin, member |
| `metrics:read` | Read the metrics. | No | owner, admin, member, viewer |
| `domains:read` | Read domains and their DNS records. | No | owner, admin, member, viewer |
| `domains:write` | Add, verify, change and delete domains. | No | owner, admin |
| `api_keys:read` | List the API keys. A key itself is never shown again. | No | owner, admin, member, viewer |
| `api_keys:write` | Make and revoke API keys. | No | owner, admin |
| `webhooks:read` | Read webhooks and their deliveries. | No | owner, admin, member, viewer |
| `webhooks:write` | Add, change, test and delete webhooks. | No | owner, admin |
| `logs:read` | Read the request logs. | No | owner, admin, member, viewer |
| `workspace:read` | Read the workspace, its members, the plans and the invoices, and ask the assistant. | No | owner, admin, member, viewer |
| `workspace:write` | Change the workspace, its members and its plan. | No | owner, admin |
| `methods:read` | Read the method catalog. | Yes | owner, admin, member, viewer |
| `account:read` | Read who the caller is. | Yes | owner, admin, member, viewer |
| `suppressions:read` | Read the suppression list. | No | owner, admin, member, viewer |
| `suppressions:write` | Put an address on the suppression list, or take one off. | No | owner, admin |
| `events:read` | Read the events and the latest times each was sent. | No | owner, admin, member, viewer |
| `events:write` | Send events, and define, change and delete them. | No | owner, admin, member |
| `imports:read` | Read the imports and their reports. | No | owner, admin, member, viewer |
| `imports:write` | Look into another account, start an import from it and cancel one. | No | owner, admin |
| `images:read` | Read the images that were copied or uploaded and where each is served. | No | owner, admin, member, viewer |
| `images:write` | Copy an image from its address, upload one, and delete one. | No | owner, admin, member |

## People and roles

A person signed in to the dashboard calls the same methods with the scopes of their role.

- `owner` and `admin`: everything.
- `member`: reads everything, and writes emails, broadcasts, audiences, templates, automations and events.
- `viewer`: reads everything and changes nothing.

> A few methods answer a signed-in person only, and refuse every key: closing the workspace, and the kept chats of the assistant. Their pages in the reference say so.

## Keeping a key safe

- A key is shown once, when it is made. Only a hash of it is stored, so it cannot be read again.
- Keep it on a server. A key in a browser or an app can be read by anyone who has the app.
- Revoke a key the moment it may have leaked. It stops working at once.
- The [logs](https://opensend.sh/docs/api/logs) list every request a key made, so you can see what a key was used for.

cURL:

```bash
curl -X DELETE "$OPENSEND_API_URL/v1/api-keys/key_03" \
  -H "Authorization: Bearer $OPENSEND_API_KEY"
```

TypeScript:

```ts
await opensend.apiKeys.remove('key_03')
```

CLI:

```bash
opensend api-keys remove key_03
```

MCP:

```json
{
  "name": "api_keys_remove",
  "arguments": {
    "id": "key_03"
  }
}
```
