Sending
Templates
Save an email as a template in the dashboard, then send it from your app with its ID and the values to fill in.
On this page
A template is an email you save once in Ternwire and send many times. Your app sends the template's ID and the values that change for each recipient, such as a name or a link. Ternwire fills them in and sends the finished email. You can change the wording or design in the dashboard without changing your code.
Create a template
Open Templates in the dashboard and choose New template. There are two ways to write one:
- Visual editor lets you write and style the email like a document. Type
/to add a button, divider, columns, or another block. Use Insert variable to add a value your app fills in, and Preview to see the email with example values. Choose Desktop or Phone above the preview to check both widths. - HTML lets you write the HTML yourself, with an optional plain-text version. Email apps that can't show HTML display the plain-text version. You can also import an existing
.html,.hbs, or.txtfile.
Give the template a name and a subject line, then save it with Create template, or press Ctrl+S (Cmd+S on a Mac). The subject can contain variables too.
To try a change without touching the live template, choose Duplicate in the template's More actions menu and edit the copy. A visual template's menu also has View HTML, which shows the HTML and plain text that get sent, and Convert to HTML template, which makes a copy you edit as code. The copy can't go back to blocks.
Images
Images are added by link for now. Upload the image somewhere public first, then paste its address into the editor:
- On your website, most site builders and shops have a media or files library. Upload the image there, open it, and copy its address. On a page, you can also right-click an image and choose Copy image address.
- Any image host works too, as long as the address starts with
https://and opens without signing in.
In the visual editor, type / and choose Image to add a picture, or open Style and paste the address under Logo for your logo. Keep the image at that address, because email apps load it from there each time the email is opened.
Find the template ID
Every template has an ID that starts with template_. The Templates list shows it in the ID column, and the template's page shows it above the editor. Use the copy button next to it. Send with API on the template's page shows ready-to-copy code with the ID and your variables already filled in.
Deleting a template is permanent. After that, any request that still uses its ID is rejected with template_not_found.
Change a template from your code
PUT /v1/templates/:id replaces a template. If someone may edit it in the dashboard at the same time, send an If-Match header with the updatedAt you last read. The template is then replaced only if nobody saved it since. If someone did, you get 409 template_changed: read the template again and apply your change to that version. Without If-Match, your request replaces whatever is stored. The dashboard editor works the same way and asks before it saves over a newer version.
POST /v1/templates takes an optional Idempotency-Key header. A retry with the same key and body returns the template the first request created instead of creating a second one.
Visual editor templates through the API
A template made in the visual editor has "format": "react-email". GET /v1/templates/:id returns three parts for it: editorContent, the editor's own HTML that the dashboard loads when someone opens the template, and html and text, the email that is sent. To create or replace one through the API, send all three:
{
"format": "react-email",
"name": "Welcome",
"subject": "Welcome, {{first_name}}",
"editorContent": "<h1>Welcome, {{first_name}}</h1><p>Thanks for signing up.</p>",
"html": "<!DOCTYPE html><html><body><h1>Welcome, {{first_name}}</h1><p>Thanks for signing up.</p></body></html>",
"text": "Welcome, {{first_name}}\n\nThanks for signing up."
}editorContent is checked as the dashboard checks it: it may hold only what the editor makes, such as p, h1 to h6, a, img, lists, table and span, with no scripts, styles that load files, comments or links other than https, http and mailto. Anything else is refused with 400 and invalid_template on editorContent. All three parts are checked for Handlebars syntax. Ternwire sends html and text as they are, and the dashboard makes them again from editorContent the next time someone saves the template there. If you write templates in code and nobody edits them in the dashboard, a Handlebars template with only html and text is simpler.
Check a template before sending
POST /v1/templates/:id/render fills a saved template with values exactly as a send would, and returns the subject, html and text. Nothing is sent and nothing counts against your quota. A missing value gets 422 with template_render_failed and the missing names in missing, as a send would.
curl --request POST "https://ternwire.com/v1/templates/template_0123456789abcdef/render" \
--header "Authorization: Bearer $TERNWIRE_API_KEY" \
--header "Content-Type: application/json" \
--data '{ "values": { "customer": { "name": "Ada" }, "loginUrl": "https://app.example.com/login" } }'POST /v1/templates/render does the same for a template you have not saved: send template, with the fields POST /v1/templates takes, and values. The template is checked as a save would check it. Both need a key with the send scope.
Write a template with AI through the API
POST /v1/templates/generate writes a draft from an instruction, or changes the draft you send, and returns it with the AI credits it used. It is the same generation as the dashboard's Write with AI, with the same paid plan requirement and the same AI credits. The draft is not saved: check it, then save it with POST /v1/templates. Use "format": "handlebars", which returns a subject, html and text you can save as they are. A react-email draft has only editorContent, which the dashboard's editor turns into the email.
curl --request POST "https://ternwire.com/v1/templates/generate" \
--header "Authorization: Bearer $TERNWIRE_API_KEY" \
--header "Content-Type: application/json" \
--data '{ "format": "handlebars", "instruction": "A password reset email with a button to {{resetUrl}}" }'It needs a live key with the templates:generate scope, which no preset includes: an owner picks it under Custom on the API keys page. A generation can take a minute or more, so give the request a long timeout. GET /v1/templates/generation-credits returns the credits left this billing period, and GET /v1/me includes them too.
Send a template
Call POST /v1/emails with templateId and a values object instead of subject, html, and text. The template supplies the subject and the body. The other fields, such as from, to, and the Idempotency-Key header, work as described in the quickstart.
curl --request POST "https://ternwire.com/v1/emails" \
--header "Authorization: Bearer $TERNWIRE_API_KEY" \
--header "Content-Type: application/json" \
--header "Idempotency-Key: welcome-ada-001" \
--data '{
"from": "hello@your-verified-domain.com",
"to": ["ada@example.com"],
"templateId": "template_0123456789abcdef",
"values": {
"customer": { "name": "Ada" },
"loginUrl": "https://app.example.com/login"
}
}'To check a template before your app uses it, open it in the dashboard and choose Send test. It sends a real email to an inbox you pick, filled in with the example values from the editor. Until you verify a domain, the test comes from Ternwire's test address and only goes to you and your teammates.
Write variables
Templates use Handlebars syntax. A variable is a name in double braces. Ternwire replaces it with the matching key from values.
| In the template | In values | Result |
|---|---|---|
Hi {{name}} | { "name": "Ada" } | Hi Ada |
Hi {{customer.name}} | { "customer": { "name": "Ada" } } | Hi Ada |
{{#if trial}}Your trial ends soon.{{/if}} | { "trial": true } | Your trial ends soon. |
{{#each items}}{{this.title}}, {{/each}} | { "items": [{ "title": "Mug" }, { "title": "Hat" }] } | Mug, Hat, |
Hi {{default name "there"}} | {} | Hi there |
- Use a dot for nested values, like
customer.name. {{#if value}}…{{else}}…{{/if}}shows a part only when the value is set and not empty.{{#unless value}}does the opposite.{{#each list}}…{{/each}}repeats a part for every item in a list. Inside it,{{this.field}}reads a field of the current item.- In the HTML body, values are escaped, so a value containing
<b>shows the characters instead of bold text. The subject and the plain-text version are not escaped. - Templates can also use these helpers:
eq,ne,gt,gte,lt,lte,and,or,not,default,uppercase,lowercase,capitalize,length,includes,join, andjson. Partials are not supported.
The editor lists every variable it finds under Example values and fills each one with an example, such as Ana for first_name. Those values are used only for the preview, for test sends and in the code that Send with API shows. Your app always sends the real values.
When a value is missing
Ternwire renders templates in strict mode, so a missing value stops the email instead of sending a broken one:
- A variable used on its own, such as
{{name}}, must be present invalues. If it is missing, the request is rejected with422and the codetemplate_render_failed, and nothing is sent. - A value that is present but empty, such as
"name": ""or"name": null, renders as nothing. - A missing value used in
{{#if}}or{{#each}}counts as empty, so that part of the template is left out. {{default name "there"}}gives a fallback for a value that may be missing or empty.
Check the response of every send. A template_render_failed error usually means your code and the template disagree about a variable's name.