Exchange a setup token for an API key
/v1/agent/setupFor 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.
Needs no credential.
Example
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 --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"
}'Request body
tokenrequiredstringThe setup token from the prompt.
client_namestringWhat the agent is, shown to the person as its claim and naming its key ("OpenCode, set up <date>").
Responses
201Success
AgentKeykeyrequiredstringThe API key (nm_…), shown once. Save it as NIGHTMAIL_API_KEY in the project's environment, never in code or a commit.
keyIdrequiredstringnamerequiredstring"<client_name>, set up <date>" ("Agent, set up <date>" without one), under API keys, where a person revokes it.
workspaceIdrequiredstringworkspaceNamerequiredstringThe 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.
baseUrlrequiredstringThe API origin to call.
mcprequiredobjectmcp.urlrequiredstringmcp.configrequiredobjectA remote MCP server entry that reads the key from NIGHTMAIL_API_KEY.
nextSteprequiredstring400, 401, 403, 404, 409, 413, 422, 429, 500, 503Request failed; use error.code and requestId to diagnose.
ApiErrorerrorrequiredobjecterror.coderequiredstringerror.messagerequiredstringerror.requestIdrequiredstringerror.fieldstringerror.retryablerequiredbooleanerror.resetAtstringNext 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.requiredobjectOn a 403 permission refusal: what the request needs that this credential lacks. The Nightmail-Required-Permission header says the same.
410Expired, or replaced by a newer one; use error.code.
ApiErrorerrorrequiredobjecterror.coderequiredstringerror.messagerequiredstringerror.requestIdrequiredstringerror.fieldstringerror.retryablerequiredbooleanerror.resetAtstringNext 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.requiredobjectOn a 403 permission refusal: what the request needs that this credential lacks. The Nightmail-Required-Permission header says the same.