NightmailDocs
Docs

Poll for the agent's API key

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.

Needs no credential.

Example

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 --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"
}'

Request body

device_coderequiredstring
grant_typestring

Optional, as RFC 8628 clients send it.

Responses

201Success
AgentKey
keyrequiredstring

The API key (nm_…), shown once. Save it as NIGHTMAIL_API_KEY in the project's environment, never in code or a commit.

keyIdrequiredstring
namerequiredstring

"<client_name>, set up <date>" ("Agent, set up <date>" without one), under API keys, where a person revokes it.

workspaceIdrequiredstring
workspaceNamerequiredstring

The workspace's name: tell the person, so they can confirm the key is in theirs.

permissionsrequiredstring[]

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.

baseUrlrequiredstring

The API origin to call.

mcprequiredobject
mcp.urlrequiredstring
mcp.configrequiredobject

A remote MCP server entry that reads the key from NIGHTMAIL_API_KEY.

nextSteprequiredstring
400, 401, 403, 404, 409, 413, 422, 429, 500, 503Request failed; use error.code and requestId to diagnose.
ApiError
errorrequiredobject
error.coderequiredstring
error.messagerequiredstring
error.requestIdrequiredstring
error.fieldstring
error.retryablerequiredboolean
error.resetAtstring

Next capacity change for a Hosted rolling send limit.

error.detailsobject[]

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

On a 403 permission refusal: what the request needs that this credential lacks. The Nightmail-Required-Permission header says the same.