# 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) · [Auth reference](/docs/tags/auth.md)

## Exchange agent setup token

`POST /v1/agent/setup`

For a coding agent: the setup token in the prompt a person copied from the dashboard (nm_setup_…) becomes a lasting API key for the agent, in that person's workspace, named after client_name. It works once, within 15 minutes of copying. A wrong token gets 400 SETUP_TOKEN_INVALID, a used one 409 SETUP_TOKEN_USED (naming the key it gave, in error.details, never the key again), and an expired or replaced one 410 SETUP_TOKEN_EXPIRED or SETUP_TOKEN_REPLACED with a device code in error.details[0] (startAgentDeviceAuthorization's answer) for the person who copied it to approve: show them its link and poll it. The key is shown once.

Operation ID: `exchangeAgentSetupToken`

Permission: Needs no credential.


### Request body

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `token` | string | yes | The setup token from the prompt. |
| `client_name` | string | no | What the agent is, shown to the person as its claim and naming its key ("OpenCode, set up <date>"). |

### TypeScript SDK

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

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

const result = await exchangeAgentSetupToken({
  client,
  body: {
    "token": "nm_setup_00000000000000000000000000000000.0000000000000000000000000000000000000000000000000000000000000000"
  },
  throwOnError: true,
});

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

### cURL

```sh
curl --request POST \
  --url 'https://nightmail.dev/v1/agent/setup' \
  --header "Authorization: Bearer $NIGHTMAIL_API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{
  "token": "nm_setup_00000000000000000000000000000000.0000000000000000000000000000000000000000000000000000000000000000"
}'
```

### 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 |
| `410` | Expired, or replaced by a newer one; use error.code. | object |

#### 201: Success

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `key` | string | yes | The API key (nm_…), shown once. Save it as NIGHTMAIL_API_KEY in the project's environment, never in code or a commit. |
| `keyId` | string | yes |  |
| `name` | string | yes | "<client_name>, set up <date>" ("Agent, set up <date>" without one), under API keys, where a person revokes it. |
| `workspaceId` | string | yes |  |
| `workspaceName` | string | yes | The workspace's name: tell the person, so they can confirm the key is in theirs. |
| `permissions` | string[] | yes | read, send and manage, on every sender domain: send email, manage domains, inboxes, receiving, webhooks and campaigns. It can't create API keys, agent tokens, inbox connection keys, inbox keys or mail-client (IMAP/SMTP) passwords; members, billing and deleting the workspace are a person's. |
| `baseUrl` | string | yes | The API origin to call. |
| `mcp` | object | yes |  |
| `mcp.url` | string | yes |  |
| `mcp.config` | object | yes | A remote MCP server entry that reads the key from NIGHTMAIL_API_KEY. |
| `nextStep` | string | yes |  |

#### 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. |

#### 410: Expired, or replaced by a newer one; use error.code.

| 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. |
