# templates.render

Render a template without sending it.

Returns the subject and bodies a send would produce, and a report on every variable: what the declaration says, which body uses it, and whether a sample filled it. With `preview`, any variable the caller omits is filled from its declared sample. With `draft`, it renders that copy instead of the stored one, under the checks a save makes, and stores nothing: saving a variant publishes it, so this is how copy is seen before it is live.

```ts
openemail.templates.render(
  accountId: string,
  slug: string,
  body?: TemplatesRenderBody,
  options?: CallOptions,
): ApiPromise<MailTemplateRender>
```

It calls `POST /accounts/{accountId}/templates/{slug}/render`: [Render a template without sending it](/docs/api/templates/post_TemplateRender) in the API reference.

## Example

```ts
const mailTemplateRender = await openemail.templates.render(accountId, slug, {})
```

## Arguments

| Argument | Type | Description |
| --- | --- | --- |
| `accountId` | `string` | Account ULID identifier.. The path's `{accountId}`. |
| `slug` | `string` | The template's name within this account.. The path's `{slug}`. |
| `body` | [`TemplatesRenderBody`](#templatesrenderbody) | Optional. The request's body. |

## Options

It takes no options of its own. Every call also takes `signal`, `timeout`, `retry` and `headers`, which say how that one call is sent ([call options](/docs/sdks/typescript/calling-methods#call-options)).

## Answer

Awaited, it is the API's JSON, [`MailTemplateRender`](/docs/sdks/typescript/types/mail-template-render). It is an [`ApiPromise`](/docs/sdks/typescript/responses): `.withResponse()` gives the same with the `Response` it came in, for its headers.

## Errors

| Status | What it means |
| --- | --- |
| `400` | missing_variable, rendered_too_large, rendered_subject_too_long, template_invalid, no_variant_for_language, a draft's own refusal (text_required, subject_too_long, body_too_large, as a save answers it), or validation_failed for an unknown key |
| `401` | unauthorized: the bearer credential is missing, malformed, expired, or revoked. Carries a `WWW-Authenticate: Bearer` challenge |
| `404` | not_found |
| `411` | length_required: Content-Length is required |
| `413` | too_large: the JSON body exceeds 2 MiB |
| `503` | temporary_failure: the platform's storage could not answer just now; retry after the Retry-After window. Any operation can answer this. A write that got it may have been applied, so retry it with the same idempotency key where it takes one, or read the state back first (see Transient failures in the API description) |

A failed call rejects with a [`RequestError`](/docs/sdks/typescript/errors): its `status`, and its `code`, the API's `error`. Decide on the code: one status covers several.

## Types

### TemplatesRenderBody

```ts
/** The body of `templates.render`. */
type TemplatesRenderBody = {
  /** Copy not stored yet, rendered in the language asked for (else the template's default) with no fallback, since it IS that language's copy. Held to the checks a save makes, so a broken draft gets the refusal saving it would; nothing is written. */
  draft?: TemplatesRenderBodyDraft | null
  /** Language tag, e.g. `en` or `de-AT`. Stored lowercased. */
  lang?: string | null
  preview?: boolean
  recipient?: TemplatesRenderBodyRecipient | null
  vars?: Record<string, string | number | boolean | null>
}
```

### TemplatesRenderBodyDraft

```ts
/** Copy not stored yet, rendered in the language asked for (else the template's default) with no fallback, since it IS that language's copy. Held to the checks a save makes, so a broken draft gets the refusal saving it would; nothing is written. */
type TemplatesRenderBodyDraft = {
  html?: string | null
  subject: string
  text: string
}
```

### TemplatesRenderBodyRecipient

```ts
/** The API's `TemplatesRenderBodyRecipient`, as the document describes it. */
type TemplatesRenderBodyRecipient = {
  email: string
  name?: string | null
}
```

It names these types of the API, each on a page of its own: [`MailTemplateRender`](/docs/sdks/typescript/types/mail-template-render).
