# Templates

> Templates at Sendtide: visual editor or HTML, variables, conditions and loops, languages, versions, test sends and previews through the API.

URL: https://sendtide.ch/en/docs/templates/

With templates, product and support edit the copy in the editor and your code only sends values. Each template can have any number of languages and versions.
## Structure

A template has:
- an alias (welcome, invoice) that you use when sending (^[a-z0-9][a-z0-9-]{1,62}$), or its tpl_ id
- a kind: visual (blocks in the editor) or code (your own HTML)
- one content per language: subject, preview text (preheader) and either blocks or html and text
- a draft and a published version. Sending always uses the published version

Visual templates consist of blocks of the types logo, heading, text, button, details, items, image, divider, spacer, footer and columns, with a style (accent, bg, font, width). A visual template can be converted to code in the editor.
## Variables, conditions, loops

The values come from the data field of the request.

```
Hello {{ data.name | default: "customer" }},

your invoice for {{ data.total | chf }} is due on {{ data.due }}.

{{#if data.coupon}}
Your coupon: {{ data.coupon | upper }}
{{else}}
Use the code WELCOME next time and save.
{{/if}}

{{#each data.items}}
- {{ this.name }}: {{ this.price | chf }}
{{/each}}
```
| Syntax | Meaning |
| --- | --- |
| {{ data.name }} | Insert a value. HTML characters are escaped. |
| {{#if data.x}}…{{else}}…{{/if}} | Condition: true when data.x is set. |
| {{#each data.items}}…{{/each}} | Loop. Inside, the current element is this. |

You attach filters with | and give an argument after a colon (default: "customer"). Available filters: default, upper, lower, trim, chf (as CHF 1'234.50) and number (with Swiss thousands separators). There are no functions and no scripting, templates run in a sandbox.

In visual blocks cond shows a block only if data.<cond> is set, and list repeats an items block for every element of a list.
### Missing values

If a value the template needs is missing, the API refuses the request with 422 missing_variables and names the variables. No email goes out with “Hello,”. When rendering (see below) you find them in missing_variables.
## Languages

locale picks the language when sending, for example de-CH. Sendtide looks in this order: exactly this language, then the base language (de), then the account’s default language. Which languages exist and what happens when one is missing (missing_language: fallback or error) you set in the account settings. Without a matching language and without a fallback the API answers 422 language_not_available.

A sub-account takes its languages from the main account.
## Create a template through the API

```
curl https://api.sendtide.ch/v1/templates \
  -H "Authorization: Bearer $SENDTIDE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "alias": "welcome",
    "name": "Welcome",
    "kind": "code",
    "langs": {
      "de-CH": {
        "subject": "Willkommen, {{ data.name }}",
        "preheader": "Schön, dass du da bist",
        "html": "<h1>Hallo {{ data.name }}</h1><p>Willkommen bei Acme.</p>"
      },
      "en": {
        "subject": "Welcome, {{ data.name }}",
        "html": "<h1>Hello {{ data.name }}</h1><p>Welcome to Acme.</p>"
      }
    },
    "publish": true
  }'
```

With "publish": true the draft is published as a new version at once. An alias that exists already answers 409 alias_taken.

PATCH /v1/templates/{id} changes the draft. A language you set to null is removed. Without publish nothing changes about what is being sent. GET /v1/templates lists all templates, see [Pagination](https://sendtide.ch/en/docs/pagination/).
## Versions

Every publish creates a new version with an optional note.

```
# publish the draft
curl -X POST https://api.sendtide.ch/v1/templates/welcome/publish \
  -H "Authorization: Bearer $SENDTIDE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "note": "Shorter subject" }'

# list versions, newest first
curl https://api.sendtide.ch/v1/templates/welcome/versions \
  -H "Authorization: Bearer $SENDTIDE_API_KEY"

# copy an old version back into the draft
curl -X POST https://api.sendtide.ch/v1/templates/welcome/versions/3/restore \
  -H "Authorization: Bearer $SENDTIDE_API_KEY"
```

restore copies the version into the draft. The published version stays until you publish the draft again. So a rollback is a restore plus a publish.
## Preview and test send

POST /v1/templates/{id}/render renders subject, HTML and text with your data without sending anything. With version you pick draft (default), published or a version number, with content you pass an unsaved working copy.

```
curl https://api.sendtide.ch/v1/templates/welcome/render \
  -H "Authorization: Bearer $SENDTIDE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "locale": "en", "data": { "name": "Anna" } }'
```

The answer has subject, html, text and missing_variables. For the test send use POST /v1/templates/{id}/test: Sendtide renders the template and sends one email with “[Test]” before the subject. The endpoint needs a key with send rights and expects from and to, optionally data, locale, version, attachments and stream.
## Templates for sub-accounts

Main accounts can release templates to sub-accounts (sub_allow) and lock single blocks (locked). Details in [Sub-accounts](https://sendtide.ch/en/docs/sub-accounts/).
