NightmailDocs
Docs

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.

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

Example

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 --request POST \
  --url 'https://nightmail.dev/v1/api-keys' \
  --header "Authorization: Bearer $NIGHTMAIL_API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{
  "name": "Example"
}'

Request body

CreateApiKey
namerequiredstring

Any text, 1 to 100 characters once trimmed. Need not be unique.

permissionsstring[]

Workspace keys: read, send and manage. Defaults to send.

domainsstring[]

Workspace keys: sender domains it is limited to, including their subdomains. Defaults to all.

accessobject or object or object

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.

expiresAtstring or null

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.

Responses

201Success
idrequiredstring
namerequiredstring
permissionsrequiredstring[]

Workspace permissions. Empty for a key limited to addresses or an inbox: see access.

domainsrequiredstring[]
prefixrequiredstring
createdAtrequiredstring
lastUsedAtrequiredstring or null
revokedAtrequiredstring or null
accessrequiredobject
access.typerequiredstring

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.addressIdsrequiredarray or null

addresses: the receiving addresses it may use, or null for every address including ones added later. inbox: the inbox. Null for workspace keys.

access.inboxIdrequiredstring or null

inbox: the agent inbox the key belongs to.

access.mailrequiredstring[]

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.createdInboxIdsrequiredstring[]

With create: the live agent inboxes the key created, which it reaches as well as addressIds (up to 1,000, by ID). Empty otherwise.

expiresAtrequiredstring or null

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.

approvalrequiredobject or null

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

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

The earliest end among its owners: when it stops working unless their agents connect again before then.

approval.ownersrequiredobject[]

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.

lastChangerequiredobject or null

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

When it happened (ISO 8601, UTC).

lastChange.actionrequiredstring

What happened, as <area>.<verb>: api_key.create, member.role_change, domain.delete, support.view.

lastChange.actorrequiredobject
agentrequiredobject or null

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.clientrequiredstring or null

What the agent said it is (client_name), its claim, never verified. Null when it didn't say.

secretrequiredstring
nextStepstring

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