Skip to main content
Before continuing, make sure you’ve completed the SDK setup and requirements. 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.

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

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

Override the subject

A subject on the send replaces the subject of the template for that email:
  • 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. 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 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 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 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

Templates

Versions, aliases, publishing, and the tokens a template can use

Bulk email sending

Name a template on each entry or on the fallback

Idempotency keys

Retry a send without sending it twice