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

## Plan audience mutation

`POST /v1/audience-query/plan`

Create a 15-minute immutable plan for a bounded AudienceQL bulk mutation. Does not modify contacts or memberships. Each statement stops after 5 seconds: a request that needs longer is refused with 422 READ_TOO_LARGE. Narrow the dates or filters. At most 10,000 contacts per plan; newest 25 unapplied plans kept per workspace.

Operation ID: `planAudienceMutation`

Permission: Needs Audience & content: write on the whole workspace. A person needs to be an owner or admin.


### Request body

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `statement` | string | yes | AudienceQL statement. Supported: SELECT fields\|count(*) FROM contacts [WHERE ...] [LIMIT n]; UPDATE contacts SET name\|consent\|properties.key = value WHERE ... [LIMIT n]; ADD contacts TO LIST 'name-or-id' WHERE ...; REMOVE contacts FROM LIST 'name-or-id' WHERE .... WHERE supports AND with =, !=, CONTAINS, IS NULL, IS NOT NULL and timestamp comparisons over id, email, name, consent, suppressed, list, segment, lastOpenAt, lastClickAt and properties.key. No raw tables, joins, comments or semicolons. |

### TypeScript SDK

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

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

const result = await planAudienceMutation({
  client,
  body: {
    "statement": "example"
  },
  throwOnError: true,
});

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

### cURL

```sh
curl --request POST \
  --url 'https://nightmail.dev/v1/audience-query/plan' \
  --header "Authorization: Bearer $NIGHTMAIL_API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{
  "statement": "example"
}'
```

### 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 |
| --- | --- | --- | --- |
| `id` | string | yes |  |
| `operation` | string | yes |  |
| `matched` | integer | yes |  |
| `sample` | object[] | yes |  |
| `sample[].id` | string | yes |  |
| `sample[].email` | string | yes |  |
| `sample[].name` | string or null | yes |  |
| `sample[].consent` | string | yes |  |
| `sample[].suppressed` | boolean | yes |  |
| `warnings` | string[] | yes |  |
| `requiresResubscribeConfirmation` | boolean | yes |  |
| `expiresAt` | 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. |
