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

## Preview email

`POST /v1/emails/preview`

Read-only offline emulation, not a screenshot. Supply HTML or an outbound email ID; stored HTML has the same access and redaction as getEmailContent. Render untrusted output only via iframe.srcdoc with sandbox=""; the embedded CSP blocks remote assets. Results follow the requested client order. Classic Outlook is lint only (empty html). New Outlook for Windows models Outlook web; actual client behavior is unverified. Input over 256 KiB UTF-8, excessive document complexity, or combined output HTML over 4 MiB returns 413.

Operation ID: `previewEmail`

Permission: Needs Sent mail: read for the sender domain.


### Request body

Supply exactly one source: html or emailId.

One of 2:

#### Variant 1

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `html` | string | yes | HTML to preview, up to 256 KiB in UTF-8. |
| `clients` | string[] | yes |  |
| `theme` | string | yes |  |
| `device` | string | no | Viewport width in CSS pixels: mobile 390, desktop 800. Defaults to mobile for iOS, Android and Samsung Email; otherwise desktop. |

#### Variant 2

| Field | Type | Required | Description |
| --- | --- | --- | --- |
| `emailId` | string | yes | Outbound email with retained HTML visible to this credential. |
| `clients` | string[] | yes |  |
| `theme` | string | yes |  |
| `device` | string | no | Viewport width in CSS pixels: mobile 390, desktop 800. Defaults to mobile for iOS, Android and Samsung Email; otherwise desktop. |

### TypeScript SDK

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

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

const result = await previewEmail({
  client,
  body: {
    "html": "<h1>Hello</h1><p>Your message is ready.</p>",
    "clients": [
      "gmail-web"
    ],
    "theme": "light"
  },
  throwOnError: true,
});

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

### cURL

```sh
curl --request POST \
  --url 'https://nightmail.dev/v1/emails/preview' \
  --header "Authorization: Bearer $NIGHTMAIL_API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{
  "html": "<h1>Hello</h1><p>Your message is ready.</p>",
  "clients": [
    "gmail-web"
  ],
  "theme": "light"
}'
```

### 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 |
| --- | --- | --- | --- |
| `previews` | object[] | yes |  |
| `previews[].client` | string | yes |  |
| `previews[].html` | string | yes | Untrusted offline document; render only as the srcdoc of an iframe with sandbox="". Empty for classic Outlook (lint only). |
| `previews[].issues` | object[] | 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. |
