# Nightmail API

Send, receive and reply to email on your own domains. 202 means queued, not delivered.

- [Interactive documentation](/docs)
- [OpenAPI 3.1 JSON](/openapi.json)
- [Agent index](/llms.txt)
- TypeScript package: `@nightmail/sdk`

[API index](/docs.md) · [ApiKeys reference](/docs/tags/api-keys.md)

## Create api key

`POST /v1/api-keys`

One key type with permissions. By default a workspace key (permissions, optional sender domains). With access, a key limited to receiving addresses or to one agent inbox (bearer nm_mbx_…): it reads and, with send, replies only there, and never reaches the rest of the workspace. expiresAt ends it at a chosen time. The secret is returned once; everything else about the key changes later on the same secret (updateApiKey). A key created over MCP, or with a key that was, is tied to that approval: approval and nextStep in the result say until when.

Operation ID: `createApiKey`

Permission: Needs Configuration: write on the whole workspace. A person needs to be an owner or admin. Not a temporary agent token.


### Request body

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `name` | string | yes | Any text, 1 to 100 characters once trimmed. Need not be unique. |
| `permissions` | string[] | no | Workspace keys: read, send and manage. Defaults to send. |
| `domains` | string[] | no | Workspace keys: sender domains it is limited to, including their subdomains. Defaults to all. |
| `access` | object or object or object | no | What the key is limited to. Defaults to the workspace. A key for addresses or an inbox takes its permissions in access.mail and can never send from other addresses, read other mail or manage the workspace. |
| `expiresAt` | string or null | no | When the key stops working. Omit or null for a key that never expires; an inbox key also ends with its inbox, and its expiry is at most that end. |

### TypeScript SDK

```ts
import { createClient } from '@nightmail/sdk/client';
import { createApiKey } from '@nightmail/sdk';

const client = createClient({
  baseUrl: 'https://nightmail.dev',
  auth: scheme => scheme.scheme === 'bearer'
    ? process.env.NIGHTMAIL_API_KEY
    : undefined,
});

const result = await createApiKey({
  client,
  body: {
    "name": "Example"
  },
  throwOnError: true,
});

console.log(result.data);
```

### cURL

```sh
curl --request POST \
  --url 'https://nightmail.dev/v1/api-keys' \
  --header "Authorization: Bearer $NIGHTMAIL_API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{
  "name": "Example"
}'
```

### Responses

| Status | Description | Schema |
| --- | --- | --- |
| `201` | Success | object |
| `400`, `401`, `403`, `404`, `409`, `413`, `422`, `429`, `500`, `503` | Request failed; use error.code and requestId to diagnose. | object |

#### 201: Success

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `id` | string | yes |  |
| `name` | string | yes |  |
| `permissions` | string[] | yes | Workspace permissions. Empty for a key limited to addresses or an inbox: see access. |
| `domains` | string[] | yes |  |
| `prefix` | string | yes |  |
| `createdAt` | string | yes |  |
| `lastUsedAt` | string or null | yes |  |
| `revokedAt` | string or null | yes |  |
| `access` | object | yes |  |
| `access.type` | string | yes | workspace: the whole workspace, with permissions and domains. addresses: only mail at receiving addresses. inbox: only one agent inbox; the key stops working when the inbox is deleted. |
| `access.addressIds` | array or null | yes | addresses: the receiving addresses it may use, or null for every address including ones added later. inbox: the inbox. Null for workspace keys. |
| `access.inboxId` | string or null | yes | inbox: the agent inbox the key belongs to. |
| `access.mail` | string[] | yes | addresses and inbox: read (mail, events, attachments), send (reply and start conversations from those addresses only), modify (mark read, archive, star, label, delete an inbox), create (create agent inboxes and use the ones it created). Empty for workspace keys. |
| `access.createdInboxIds` | string[] | yes | With create: the live agent inboxes the key created, which it reaches as well as addressIds (up to 1,000, by ID). Empty otherwise. |
| `expiresAt` | string or null | yes | When the key stops working: the expiry set on it, or for an inbox key its inbox's end if that is sooner. Null: it never expires. updateApiKey changes it on the same secret. |
| `approval` | object or null | yes | Set when this is tied to MCP approvals: created, or for a webhook set up or changed, over MCP with a person’s sign-in or with a key that is tied. It works only while every one of them is active. Null: it lasts, not tied to any approval. |
| `approval.active` | boolean | yes | Every approval that owns it is active, so a key is accepted and a webhook is delivered to. False: a key is refused and a webhook is not delivered to until all of them are active again. |
| `approval.endsAt` | string | yes | The earliest end among its owners: when it stops working unless their agents connect again before then. |
| `approval.owners` | object[] | yes | Each approval that owns it, by ID. A key has one; a webhook has one for each approval that set it up or changed it. |
| `lastChange` | object or null | yes | The newest entry in its history (listApiKeyChanges): when it was created, changed or revoked, and by whom. Null when none is kept (after a year). |
| `lastChange.at` | string | yes | When it happened (ISO 8601, UTC). |
| `lastChange.action` | string | yes | What happened, as <area>.<verb>: api_key.create, member.role_change, domain.delete, support.view. |
| `lastChange.actor` | object | yes |  |
| `agent` | object or null | yes | A key an agent got through agent setup (exchangeAgentSetupToken or the device flow): the agent it was made for. Null for every other key. |
| `agent.client` | string or null | yes | What the agent said it is (client_name), its claim, never verified. Null when it didn't say. |
| `secret` | string | yes |  |
| `nextStep` | string | no | Only for a key tied to an MCP approval: what it depends on and how to make a lasting one. |

#### 400, 401, 403, 404, 409, 413, 422, 429, 500, 503: Request failed; use error.code and requestId to diagnose.

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `error` | object | yes |  |
| `error.code` | string | yes |  |
| `error.message` | string | yes |  |
| `error.requestId` | string | yes |  |
| `error.field` | string | no |  |
| `error.retryable` | boolean | yes |  |
| `error.resetAt` | string | no | Next capacity change for a Hosted rolling send limit. |
| `error.details` | object[] | no | Every problem found, when there is more than one part, such as rule diagnostics with rule, line, column, message and hint, or a measurement, such as CONTENT_TOO_LARGE_TO_SCREEN with screenedBytes and limitBytes (and index in a batch). |
| `error.required` | object | no | On a 403 permission refusal: what the request needs that this credential lacks. The Nightmail-Required-Permission header says the same. |
