> ## Documentation Index
> Fetch the complete documentation index at: https://nuntly.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Send with a template

> Send a transactional email from a stored template by id or alias, with variable values, an optional subject override, and the errors to handle.

> [!NOTE]
>
> Before continuing, make sure you've completed the [SDK setup and requirements](https://nuntly.com/docs/guides/requirements.md). Sending with a template works with any key that can send emails, **Sending only** included. Creating and publishing templates needs the `template` permission, which **Full access** keys have. See [API keys](https://nuntly.com/docs/guides/api-keys.md#permissions).

## Overview

A transactional template stores the subject, HTML and text of an email you send often, such as a sign-in code or a password reset. Your send names the template and carries the values of its variables. Nuntly fills in the content when it accepts the send, so the HTML stays out of your code.

## Create a transactional template

Create the template with the type `transactional`, then create a version and publish it. The alias is optional: it lets your code name the template without storing its id.

```typescript theme={null}
await nuntly.templates.create({
  name: 'Sign-in code',
  type: 'transactional',
  alias: 'login-code',
});

const version = await nuntly.templates.versions.create('login-code', {
  subject: 'Your {{brand}} sign-in code',
  html: '<p>Your code is <strong>{{code}}</strong>. It expires in 10 minutes.</p>',
  text: 'Your code is {{code}}. It expires in 10 minutes.',
  variables: [
    { name: 'code', type: 'string' },
    { name: 'brand', type: 'string', default: 'Acme' },
  ],
});

await nuntly.templates.versions.publish('login-code', version.id);
```

A transactional template uses declared variables only, such as `{{code}}`. A contact token or an unsubscribe link in it is refused when you publish. See [Template types](https://nuntly.com/docs/guides/broadcast-templates.md#template-types).

## Send an email from a template

Pass the id or the alias of the template in `template`, and the values of its variables in `variables`:

```typescript TypeScript theme={null}
const email = await nuntly.emails.send({
  from: 'Acme <hello@yourdomain.com>',
  to: 'you@example.com',
  template: 'login-code',
  variables: { code: '4821' },
});

console.log(`Email sent ${email.id}`);
```

```bash curl theme={null}
curl -X POST https://api.nuntly.com/emails \
  -H 'Authorization: Bearer ntly_your_api_key_here' \
  -H 'Content-Type: application/json' \
  -d '{
    "from": "Acme <hello@yourdomain.com>",
    "to": "you@example.com",
    "template": "login-code",
    "variables": { "code": "4821" }
  }'
```

The recipient gets the subject `Your Acme sign-in code`: `brand` has no value on the send, so it takes its default.

* The send uses the version that is active when Nuntly accepts it. You cannot name a version.
* `html` and `text` cannot be sent with `template`. The request is refused with `email_template_content_conflict`.
* The other fields of a send work as usual: `cc`, `bcc`, `replyTo`, `headers`, `attachments`, `tags` and `scheduledAt`.
* Every other check of a send then runs on the filled-in email with its usual error, such as the sending domain, the size limits and your plan usage.

## Variable values

Each value must fit a variable that the active version declares:

* Names match with their exact letter case. A name the version does not declare is refused.
* A value must have the declared type: a string, a number, or a boolean. `"2"` is not accepted for a `number` variable.
* A variable with no value, or with `null`, takes its default. With no default, the send is refused.
* An empty string and `0` are values, used as given.

A value replaces its token as plain text. It is HTML-escaped in the HTML body and left as is in the subject and the text body. A value that looks like a token, such as `{{code}}`, stays as that text.

The email keeps the values it was sent with, defaults included, in the `variables` field of [the email](https://nuntly.com/docs/api-reference/emails/retrieve-email.md).

## Override the subject

A `subject` on the send replaces the subject of the template for that email:

```typescript theme={null}
await nuntly.emails.send({
  from: 'Acme <hello@yourdomain.com>',
  to: 'you@example.com',
  template: 'login-code',
  subject: 'Your code for {{brand}}: {{code}}',
  variables: { code: '4821' },
});
```

* An empty or whitespace-only `subject` is ignored, and the subject of the template is used.
* The subject can use only the variables the active version declares. A contact token, `{{system.unsubscribeUrl}}`, a block or a helper is refused.
* Nuntly checks it when you send. A token the version does not declare gets `template_version_undeclared_variable`, any other construct `template_version_placeholder_unsupported`.
* The 200-character limit of a subject applies.

## Errors

The checks run in this order, and the send reports the first one that fails. `email_template_variable_unknown` and `email_template_variable_type_mismatch` are checked value by value, in the order of `variables`. A send refused with one of these codes creates no email.

| Code | Status | When |
| - | - | - |
| `email_template_content_conflict` | 422 | The send carries `html` or `text`. |
| `not_found` | 404 | No template of your organization has this id or alias. |
| `email_template_type_invalid` | 422 | The template is a broadcast template. |
| `email_template_not_published` | 422 | The template has no published version. |
| `template_version_placeholder_unsupported` | 422 | The `subject` of the send uses a construct a transactional template does not support. |
| `template_version_undeclared_variable` | 422 | The `subject` of the send uses a variable the active version does not declare. |
| `email_template_variable_unknown` | 422 | A value names a variable the active version does not declare. |
| `email_template_variable_type_mismatch` | 422 | A value does not have the declared type. |
| `email_template_variable_missing` | 422 | A declared variable has no value and no default. |
| `email_template_content_unavailable` | 500 | The content of the template could not be read. Nothing was sent. Retry the request. |

A `template` value that is neither a template id nor a valid alias, such as `Welcome`, fails the validation of the request with a 422. The title of a variable error names the variable, never its value.

## Retries and idempotency keys

The outcome of a send refused with a 4xx is stored with its [idempotency key](https://nuntly.com/docs/guides/idempotency.md) for 24 hours. A retry with the same key within 24 hours sends nothing and replays the stored outcome, even after you fix the cause, for example by publishing the template. Fix the request or the template, then send with a new key.

A 500 `email_template_content_unavailable` is not stored, so you can retry it with the same key.

## Change the variables of a template

Publishing a version is never refused because of your current sends. Once it is active, a send that no longer fits it is refused: a value for a variable the version removed, or a missing value for a variable it added without a default. Change variables in this order to keep every send accepted:

**1. Add the variable with a default**

Publish a version that declares the new variable with a default value. Current sends stay accepted and get the default.

**2. Send the value**

Deploy the code that sends the new value. Stop sending the values of any variable you plan to remove.

**3. Publish the final version**

Publish a version without the removed variables. If the new variable must always come from the send, remove its default in this version.

## Scheduled sends

A send with `scheduledAt` gets its content when Nuntly accepts it. The email is delivered with that subject and content, even if a new version is published, the alias changes, or the template is deleted before the scheduled time.

## Find the template an email was sent from

[Retrieve the email](https://nuntly.com/docs/api-reference/emails/retrieve-email.md) to read `templateId` and `templateVersionId`, the template and the version it was sent with. Both are omitted for an email sent without a template, and they stay on the email after the template is deleted. The email list, the email events and the webhook payloads do not carry them.

The `subject` of the email is the subject before its variables were replaced, tokens included.

The `subjectTemplate`, `htmlTemplate` and `textTemplate` fields of [the email content](https://nuntly.com/docs/api-reference/emails/retrieve-email-content.md) do not name a template. They hold the source of this email's own subject and bodies before rendering, and are returned for failed emails only.

## Next steps

> [card] [Templates](https://nuntly.com/docs/guides/broadcast-templates.md)
>
> Versions, aliases, publishing, and the tokens a template can use

> [card] [Bulk email sending](https://nuntly.com/docs/guides/bulk-email-sending.md#send-with-a-template)
>
> Name a template on each entry or on the fallback

> [card] [Idempotency keys](https://nuntly.com/docs/guides/idempotency.md)
>
> Retry a send without sending it twice


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.