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

## Set inbound address flow

`PATCH /v1/inbound/addresses/{addressId}/flow`

Change where one address’s mail goes, in one version. Changes the route for the address’s own address and its two-way rule together: one transaction and one routing version, so all of it is saved or none of it. Omitted fields stay as they are, a field equal to what is saved writes nothing, and when nothing changes the version stays. deliver replaces only the route’s destinations (a paused route stays paused). Send the version from getInboundOverview or getRouting as If-Match; a stale one is refused with 409 and nothing changes.

Operation ID: `setInboundAddressFlow`

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

### Parameters

| Name | In | Type | Required | Description |
| --- | --- | --- | --- | --- |
| `addressId` | path | string | yes | Opaque inbound address ID (mbx_…). |
| `if-match` | header | string | no | Apply only if routes and rules are still at this version, from getInboundOverview, GET /v1/routing or a previous ETag. |

### Request body

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `endpoints` | object[] | no | New endpoints this change uses, made with it: a refusal or failure makes none of them. |
| `endpoints[].type` | string | yes |  |
| `endpoints[].name` | string | yes | Endpoint name, unique in the workspace across every type. |
| `endpoints[].url` | string | yes | As createEndpoint takes it: a Slack or Discord webhook URL, or a public HTTPS URL for a webhook (its signing secret is not returned here). |
| `forward` | object | no | A new forward: its recipient is asked by this address when it can send the request (else as createEndpoint picks one). deliver and split may name it at once: mail received from the moment they allow it is forwarded, never mail received before. The answer’s forward says whether they were emailed. |
| `forward.name` | string | yes | Endpoint name, unique in the workspace across every type. |
| `forward.address` | string | yes | Recipient. They confirm with an emailed link, sent from the new address when it can send it (else as createEndpoint picks one), unless preverify applies. |
| `forward.preverify` | boolean or string | no | As createEndpoint takes it: auto skips the confirmation only for an address on a domain this workspace has verified. |
| `deliver` | string or string or string[] or null | no | Where the address's mail goes: the destinations of the route for its own address, as createRoute takes them (inbox, webhook and forward IDs or keys, [] to keep it without an inbox, or 'drop'). Creates the route when there is none; null deletes it. |
| `rule` | string | no | The key of the two-way rule (split or split-by-meaning) on this address that split changes. |
| `split` | object or null | no | With rule: rewrites that rule. A rule other addresses also run is left as it is for them, and this address gets its own copy under a new key. Without rule: adds a new two-way rule on this address. null, with rule: takes the rule off this address, and deletes it once no address runs it. |
| `split.recipe` | string | yes | split, or split-by-meaning to let the AI decide. |
| `split.params` | object | no | The fields createRuleFromRecipe takes for this recipe: split { test, value, then, otherwise }, split-by-meaning { meaning, then, otherwise }. |

### TypeScript SDK

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

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

const result = await setInboundAddressFlow({
  client,
  path: {
    "addressId": "address_123"
  },
  body: {},
  throwOnError: true,
});

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

### cURL

```sh
curl --request PATCH \
  --url 'https://nightmail.dev/v1/inbound/addresses/address_123/flow' \
  --header "Authorization: Bearer $NIGHTMAIL_API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{}'
```

### Responses

| Status | Description | Schema |
| --- | --- | --- |
| `200` | Success | object |
| `400`, `401`, `403`, `404`, `409`, `413`, `422`, `429`, `500`, `503` | Request failed; use error.code and requestId to diagnose. | object |

#### 200: Success

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `version` | integer | yes | The routing and rules version after the change, also the ETag; unchanged when nothing changed. |
| `forward` | object | no | The forward flow.forward made, as getEndpoint returns it. |
| `forward.id` | string | yes | Endpoint ID: mwh_… for a webhook, con_… for Slack, Discord or a forward. Routes list webhooks and forwards by ID. |
| `forward.name` | string | yes | Endpoint name, unique in the workspace across every type. Rules call endpoints.<name>(…). |
| `forward.type` | string | yes |  |
| `forward.status` | string | yes | ready: callable. pending: a forward waiting for its recipient to confirm. expired: the confirmation link lapsed. declined: the recipient declined or stopped forwards. paused: deliveries are held. |
| `forward.call` | string | yes | How a rule calls it. |
| `forward.target` | string | yes | Where it delivers. Slack, Discord and webhook URLs are write-only; only the last four characters are shown. |
| `forward.policy` | object | yes |  |
| `forward.expiresAt` | string or null | yes | When a pending forward confirmation link expires. |
| `forward.requestFrom` | string or null | yes | A forward: the address that emailed its recipient to confirm. null when none was asked (preverified) and for other types. |
| `forward.nextStep` | string | yes | What to do next, for people and agents. |
| `forward.createdAt` | string | yes |  |
| `forward.confirmation` | string | yes | emailed: its recipient was asked to confirm (requestFrom names who asked); skipped: preverified, ready at once. |
| `route` | object or null | yes | The route for the address's own address, as getRoute returns it; null when there is none. |
| `route.key` | string or null | yes | Stable key you choose (lowercase letters, digits, hyphens). Config files refer to inbox:<key>, webhook:<key> and forward:<name>. |
| `route.match` | string | yes | Recipient, case-insensitive: an address (support@acme.com), a local-part pattern (reply+*@acme.com, *-invoices@acme.com) or a catch-all (*@acme.com, or @acme.com). The most specific route wins: exact address, then the pattern with the most literal characters, then the plus-address base, then the catch-all, then the domain default. |
| `route.to` | string or string or string[] | yes | Inbox, webhook and forward IDs, [] to keep, or 'drop'. |
| `route.subaddresses` | boolean | yes |  |
| `route.enabled` | boolean | yes |  |
| `route.note` | string | yes |  |
| `route.id` | string | yes |  |
| `route.createdAt` | string | yes |  |
| `rule` | string or null | yes | The two-way rule this change left on the address: the rule it named, its own copy when the rule was shared, or the new rule. null when it was taken off, or no rule was named or added. |

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