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

## Poll agent device token

`POST /v1/agent/device/token`

With the device code from startAgentDeviceAuthorization, every interval seconds until the person decides. Each answer follows RFC 8628: 400 AUTHORIZATION_PENDING (keep polling), 400 SLOW_DOWN (polled too soon: wait five seconds longer from now on), 400 ACCESS_DENIED (the person denied it: stop), 400 EXPIRED_TOKEN (nobody approved in time: the first such answer carries a new device code in error.details[0], once; after that start again), or 201 with the API key, once. Each pending answer repeats the link to show the person. A poll after that gets 409 DEVICE_CODE_USED naming the key it gave (error.details: keyId, name, prefix, issuedAt, revokedAt), never the key again and never a second one: use the key you saved. Retry-After gives the seconds to wait.

Operation ID: `pollAgentDeviceToken`

Permission: Needs no credential.


### Request body

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `device_code` | string | yes |  |
| `grant_type` | string | no | Optional, as RFC 8628 clients send it. |

### TypeScript SDK

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

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

const result = await pollAgentDeviceToken({
  client,
  body: {
    "device_code": "nm_device_00000000000000000000000000000000.0000000000000000000000000000000000000000000000000000000000000000"
  },
  throwOnError: true,
});

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

### cURL

```sh
curl --request POST \
  --url 'https://nightmail.dev/v1/agent/device/token' \
  --header "Authorization: Bearer $NIGHTMAIL_API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{
  "device_code": "nm_device_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 |

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