# Templates

A template is an email you keep and reuse. It has variables, and it is sent in the version you published.

## Write a template

A template has a name, a subject and HTML, and can have a plain text version for mail apps that show no HTML. `{{NAME}}` marks a variable in any of the three. A name is letters, digits and `_`, and does not start with a digit.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/templates" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "name": "order-confirmation",
  "subject": "Your order is on its way",
  "html": "<p>Total: {{PRICE}}</p>"
}'
```

TypeScript:

```ts
await opensend.templates.create({
  name: 'order-confirmation',
  subject: 'Your order is on its way',
  html: '<p>Total: {{PRICE}}</p>',
})
```

CLI:

```bash
opensend templates create \
  --name order-confirmation \
  --subject 'Your order is on its way' \
  --html '<p>Total: {{PRICE}}</p>'
```

MCP:

```json
{
  "name": "templates_create",
  "arguments": {
    "name": "order-confirmation",
    "subject": "Your order is on its way",
    "html": "<p>Total: {{PRICE}}</p>"
  }
}
```

The answer lists the variables that were found, and an `alias`: a short name of your own to send with in place of the id. Without one it is made from the name.

templates.create:

```json
{
  "object": "template",
  "id": "tmpl_07",
  "name": "order-confirmation",
  "alias": "order-confirmation",
  "status": "draft",
  "subject": "Your order is on its way",
  "html": "<p>Total: {{PRICE}}</p>",
  "text": null,
  "from": "",
  "replyTo": "",
  "defaults": {},
  "design": null,
  "layout": "start",
  "variables": [
    "PRICE"
  ],
  "version": 0,
  "publishedAt": null,
  "changed": false,
  "updatedAt": "2026-11-27T09:30:00.000Z",
  "createdAt": "2026-11-27T09:30:00.000Z"
}
```

## Build it from blocks

A template does not have to be written as HTML. Give `design` in place of `html`, and the HTML and the plain text are made from it, with markup that mail apps accept. A design has a `body` of blocks, a `style` for the whole message and a `preview`, the line shown beside the subject.

```ts
import { OpenSend } from '@open-send/opensend'

const opensend = new OpenSend()

const template = await opensend.templates.create({
  name: 'Order shipped',
  subject: 'Your order {{ORDER}} is on its way',
  design: {
    preview: 'Order {{ORDER}} has shipped',
    style: { backgroundColor: '#f4f4f5', maxWidth: '600px', fontFamily: 'Arial, sans-serif' },
    body: [
      {
        kind: 'Container',
        style: { backgroundColor: '#ffffff', padding: '24px' },
        children: [
          { kind: 'Heading', as: 'h2', text: 'Thanks, {{NAME}}' },
          { kind: 'Text', text: 'Your order {{ORDER}} left our warehouse today.' },
          {
            kind: 'Button',
            href: '{{TRACKING_URL}}',
            text: 'Follow your order',
            style: { backgroundColor: '#111111', color: '#ffffff', padding: '12px 20px' },
          },
        ],
      },
    ],
  },
})

console.log(template.variables, template.text)
```

- `Section`, `Row`, `Column` and `Container` hold other blocks in `children`. A `Row` takes `Column` blocks only, and a `Column` sits in a `Row` only.
- `Text`, `Heading`, `Button`, `Link`, `Markdown`, `CodeBlock` and `CodeInline` hold `text`. `Img` shows a picture and `Hr` draws a line.
- `Html` holds markup of your own in `text`, up to 60,000 characters. It goes into the email as you wrote it.
- `style` takes CSS by property name, such as `fontSize` or `backgroundColor`. Only properties that work in mail are taken.
- A variable can stand anywhere in `text`. In `href` and `src` it has to be the whole value, and what fills it has to be a full address: anything else is sent as `#`.
- Every block gets an `id`. You can set your own to find a block again.

`html` and `text` belong to the design from then on. Sending one of them to a template built from blocks is refused with `template_designed`. Send `design` again to change it, or `design: null` to turn it into a template written as HTML, which keeps the HTML it has.

To change a part of a design, send what to change in place of the whole design. `templates.edit` takes a list of operations and runs them in order on the design that is saved. Each one names a block by its `id`.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/templates/tmpl_02/edit" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "operations": [
    {
      "op": "update",
      "id": "headline",
      "set": {
        "text": "Your week in review"
      },
      "style": {
        "color": "#111111"
      }
    }
  ]
}'
```

TypeScript:

```ts
await opensend.templates.edit('tmpl_02', {
  operations: [
    {
      op: 'update',
      id: 'headline',
      set: {
        text: 'Your week in review',
      },
      style: {
        color: '#111111',
      },
    },
  ],
})
```

CLI:

```bash
opensend templates edit tmpl_02 \
  --operations '[{"op":"update","id":"headline","set":{"text":"Your week in review"},"style":{"color":"#111111"}}]'
```

MCP:

```json
{
  "name": "templates_edit",
  "arguments": {
    "id": "tmpl_02",
    "operations": [
      {
        "op": "update",
        "id": "headline",
        "set": {
          "text": "Your week in review"
        },
        "style": {
          "color": "#111111"
        }
      }
    ]
  }
}
```

- `insert` adds `block` under `parent`, or at the top level without one, at `index` or at the end.
- `update` changes the fields in `set` and merges `style` into the style of the block. `null` takes a field or a property off.
- `move` puts a block under `parent` at `index`. `remove` takes one out with everything inside it, and `duplicate` copies one right after itself with new ids.
- `page` changes the `style`, the `preview` or the `fonts` of the whole message.

One request takes up to 50 operations. The design has to hold after each one, by the rules a whole design follows. When one is refused nothing is saved, and the message names it by its place in the list, such as `operations.2.id`. The answer is the template, with the HTML and the plain text made again.

A design can use a font of your own. Host the file on your domain or a CDN and list it in `fonts`: its `family`, the address of the file in `url`, its `format` and a `fallback`, the fonts to use where yours does not load. A block or the page uses the font by naming its family in `fontFamily`.

- `url` is a full `https` address. A variable is not taken there.
- `format` is `woff2`, `woff`, `truetype` or `opentype`.
- `weight` and `style` say what the file holds: a weight from 100 to 900 in hundreds, or `normal` or `bold`, and `normal` or `italic`. List a family once for each file, up to 4 fonts in a design.
- Many mail apps load no web fonts. They show the fallback, so pick one that sits close to your font.

To see what a design makes without saving anything, render it. A variable you give a value is filled in, and the rest stay as written. The design in this example lists a font, so the answer has its `@font-face` rule in the head and the fallback after the family.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/templates/render" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "design": {
    "fonts": [
      {
        "family": "Inter",
        "fallback": "Helvetica, Arial, sans-serif",
        "url": "https://cdn.example.com/fonts/inter.woff2",
        "format": "woff2"
      }
    ],
    "body": [
      {
        "kind": "Heading",
        "text": "Welcome, {{NAME}}",
        "style": {
          "fontFamily": "Inter"
        }
      },
      {
        "kind": "Text",
        "text": "Your plan is {{PLAN}}."
      },
      {
        "kind": "Button",
        "text": "Open your account",
        "href": "{{URL}}"
      }
    ]
  },
  "variables": {
    "NAME": "Ada"
  }
}'
```

TypeScript:

```ts
await opensend.templates.render({
  design: {
    fonts: [
      {
        family: 'Inter',
        fallback: 'Helvetica, Arial, sans-serif',
        url: 'https://cdn.example.com/fonts/inter.woff2',
        format: 'woff2',
      },
    ],
    body: [
      {
        kind: 'Heading',
        text: 'Welcome, {{NAME}}',
        style: {
          fontFamily: 'Inter',
        },
      },
      {
        kind: 'Text',
        text: 'Your plan is {{PLAN}}.',
      },
      {
        kind: 'Button',
        text: 'Open your account',
        href: '{{URL}}',
      },
    ],
  },
  variables: {
    NAME: 'Ada',
  },
})
```

CLI:

```bash
opensend templates render \
  --design '{"fonts":[{"family":"Inter","fallback":"Helvetica, Arial, sans-serif","url":"https://cdn.example.com/fonts/inter.woff2","format":"woff2"}],"body":[{"kind":"Heading","text":"Welcome, {{NAME}}","style":{"fontFamily":"Inter"}},{"kind":"Text","text":"Your plan is {{PLAN}}."},{"kind":"Button","text":"Open your account","href":"{{URL}}"}]}' \
  --variables '{"NAME":"Ada"}'
```

MCP:

```json
{
  "name": "templates_render",
  "arguments": {
    "design": {
      "fonts": [
        {
          "family": "Inter",
          "fallback": "Helvetica, Arial, sans-serif",
          "url": "https://cdn.example.com/fonts/inter.woff2",
          "format": "woff2"
        }
      ],
      "body": [
        {
          "kind": "Heading",
          "text": "Welcome, {{NAME}}",
          "style": {
            "fontFamily": "Inter"
          }
        },
        {
          "kind": "Text",
          "text": "Your plan is {{PLAN}}."
        },
        {
          "kind": "Button",
          "text": "Open your account",
          "href": "{{URL}}"
        }
      ]
    },
    "variables": {
      "NAME": "Ada"
    }
  }
}
```

templates.render:

```json
{
  "html": "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html dir=\"ltr\" lang=\"en\"><head><meta content=\"text/html; charset=UTF-8\" http-equiv=\"Content-Type\"/><meta name=\"x-apple-disable-message-reformatting\"/><style>@font-face{font-family:Inter;font-style:normal;font-weight:400;font-display:swap;mso-font-alt:Helvetica;src:url(\"https://cdn.example.com/fonts/inter.woff2\") format('woff2')}</style></head><body><h1 style=\"font-family:Inter, Helvetica, Arial, sans-serif\">Welcome, Ada</h1><p style=\"font-size:14px;line-height:24px;margin-top:16px;margin-bottom:16px\">Your plan is {{PLAN}}.</p><a href=\"{{URL}}\" style=\"line-height:100%;text-decoration:none;display:inline-block;max-width:100%;mso-padding-alt:0px;padding:0px 0px 0px 0px\" target=\"_blank\"><span><!--[if mso]><i style=\"mso-font-width:0%;mso-text-raise:0\" hidden></i><![endif]--></span><span style=\"max-width:100%;display:inline-block;line-height:120%;mso-padding-alt:0px;mso-text-raise:0\">Open your account</span><span><!--[if mso]><i style=\"mso-font-width:0%\" hidden>&#8203;</i><![endif]--></span></a></body></html>",
  "text": "Welcome, Ada\n\nYour plan is {{PLAN}}.\n\nOpen your account {{URL}}"
}
```

## Start from a ready-made template

You do not have to begin with an empty page. There are ready-made templates, called starters, for the mail most products send: sign-in codes, receipts, shipping notices, invitations, reminders and more. Each one is built from blocks. List them, by `category` or with a text in `q`.

cURL:

```bash
curl "$OPENSEND_API_URL/v1/template-starters?category=marketing" \
  -H "Authorization: Bearer $OPENSEND_API_KEY"
```

TypeScript:

```ts
await opensend.templates.starters({
  category: 'marketing',
})
```

CLI:

```bash
opensend templates starters --category marketing
```

MCP:

```json
{
  "name": "templates_starters",
  "arguments": {
    "category": "marketing"
  }
}
```

In the list a starter has an `id`, what it is for, the `subject` it starts with and the `styles` it can be drawn in.

A style is a look: the same words and the same blocks, in other colours and type. To see a starter before you use it, ask for it in a style. The answer has its `variables`, its `design`, its `defaults` and the `html` and `text` it makes with the defaults filled in. A variable with a `default` is filled with it until a send gives a value of its own. One whose `default` is `null` has to come with every send. Nothing is saved.

cURL:

```bash
curl "$OPENSEND_API_URL/v1/template-starters/verify-code?style=modern" \
  -H "Authorization: Bearer $OPENSEND_API_KEY"
```

TypeScript:

```ts
await opensend.templates.starter('verify-code', {
  style: 'modern',
})
```

CLI:

```bash
opensend templates starter verify-code --style modern
```

MCP:

```json
{
  "name": "templates_starter",
  "arguments": {
    "id": "verify-code",
    "style": "modern"
  }
}
```

- `minimal`: The house look: a white card on pale grey, system type and a black button.
- `modern`: Ink teal on warm grey, with a header bar and tinted detail panels.
- `bold`: A vermilion poster block, a heavy headline and square black buttons.
- `classic`: Stationery: serif type on warm paper, a double rule and an oxblood button.
- `dark`: Near black, with lime actions and references set in a monospace face.
- `soft`: Cream and sage, rounded panels, friendly type and pill buttons.

To make a template from one, give its id as `starter` when you create the template, and a `style` when you do not want its default one.

```ts
import { OpenSend } from '@open-send/opensend'

const opensend = new OpenSend()

const starters = await opensend.templates.starters({ category: 'commerce' })

const template = await opensend.templates.create({
  name: 'Shipping notice',
  starter: 'order-shipped',
  style: 'modern',
})

console.log(starters.total, template.subject, template.defaults)
```

The template gets the blocks of the starter, its subject and the defaults of its variables. From there it is a template built from blocks like any other: change it with `templates.update` or `templates.edit`, then publish it. A `subject` or `defaults` of your own take the place of the ones of the starter.

- `starter` stands in place of `design`, `html` and `text`. Sending one of them with it is refused, and the message names `starter`.
- `style` goes with `starter` only, and an id or a style that does not exist is refused the same way, by name.
- A starter written for a mailing list ends with an unsubscribe link whose address is `{{unsubscribe_url}}`. That is the name a broadcast fills in for each contact.

## Images are copied

An email links its images by address, and an image stops showing once that address moves or goes away. So a copy is kept of every image of a template or a broadcast, and the email that goes out links the copy. What you saved keeps the address you gave.

- A copy is made of a PNG, JPEG, GIF or WebP file of up to 5 MB at a public `https` address: the `src` of an `Img` block or an `<img>` tag, and the background image of a block. An SVG, a variable and an address that is not public are linked as they are.
- Saving fetches nothing. A new address is noted as `pending`, up to 50 for one save, and copied within about a minute. An email sent before that links the address as it is.
- A copy that cannot be made is tried 3 times. After that the image is `failed`, with the `reason` in words, and emails link the address as it is.

To have the copy at once, add the image yourself. The answer has the address of the copy in `url`. The same address a second time answers with the copy that is there, and an image that failed is tried again.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/images" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "url": "https://cdn.example.com/logo.png"
}'
```

TypeScript:

```ts
await opensend.images.add({
  url: 'https://cdn.example.com/logo.png',
})
```

CLI:

```bash
opensend images add --url https://cdn.example.com/logo.png
```

MCP:

```json
{
  "name": "images_add",
  "arguments": {
    "url": "https://cdn.example.com/logo.png"
  }
}
```

images.add:

```json
{
  "object": "image",
  "id": "img_366370f5fd51b418111aa30a",
  "url": "https://api.opensend.sh/v1/files/img_366370f5fd51b418111aa30a.png",
  "source": "https://cdn.example.com/logo.png",
  "origin": "address",
  "status": "ready",
  "type": "image/png",
  "size": 70,
  "reason": null,
  "createdAt": "2026-11-27T09:30:00.000Z",
  "updatedAt": "2026-11-27T09:30:00.000Z"
}
```

The list shows what was copied and what was not. `status` narrows it.

cURL:

```bash
curl "$OPENSEND_API_URL/v1/images?status=ready" \
  -H "Authorization: Bearer $OPENSEND_API_KEY"
```

TypeScript:

```ts
await opensend.images.list({
  status: 'ready',
})
```

CLI:

```bash
opensend images list --status ready
```

MCP:

```json
{
  "name": "images_list",
  "arguments": {
    "status": "ready"
  }
}
```

Deleting an image deletes its copy, so emails that were already sent stop showing it. An image that a template or a broadcast still uses is copied again the next time that one is saved.

cURL:

```bash
curl -X DELETE "$OPENSEND_API_URL/v1/images/img_366370f5fd51b418111aa30a" \
  -H "Authorization: Bearer $OPENSEND_API_KEY"
```

TypeScript:

```ts
await opensend.images.remove('img_366370f5fd51b418111aa30a')
```

CLI:

```bash
opensend images remove img_366370f5fd51b418111aa30a
```

MCP:

```json
{
  "name": "images_remove",
  "arguments": {
    "id": "img_366370f5fd51b418111aa30a"
  }
}
```

> A workspace can switch this off with `hostImages: false` on `workspace.update`, and emails then link every image where it is. It is not recommended: an image that moves or is deleted on its own site stops loading in every email you have sent.

## Give it a sender and defaults

A template can hold what its emails have in common, so a send does not have to repeat it.

- `from`: the sender of an email built from it, when the send names none.
- `replyTo`: the address replies go to, when the send names none. Empty leaves it to the default of the workspace.
- `defaults`: a value for a variable, by name, used when a send gives none for it.
- `text`: the plain text version. A send that gives its own `text` replaces it.

cURL:

```bash
curl -X PATCH "$OPENSEND_API_URL/v1/templates/tmpl_06" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "subject": "Welcome aboard",
  "from": "Acme <hello@mail.example.com>",
  "defaults": {
    "NAME": "there"
  }
}'
```

TypeScript:

```ts
await opensend.templates.update('tmpl_06', {
  subject: 'Welcome aboard',
  from: 'Acme <hello@mail.example.com>',
  defaults: {
    NAME: 'there',
  },
})
```

CLI:

```bash
opensend templates update tmpl_06 \
  --subject 'Welcome aboard' \
  --from 'Acme <hello@mail.example.com>' \
  --defaults '{"NAME":"there"}'
```

MCP:

```json
{
  "name": "templates_update",
  "arguments": {
    "id": "tmpl_06",
    "subject": "Welcome aboard",
    "from": "Acme <hello@mail.example.com>",
    "defaults": {
      "NAME": "there"
    }
  }
}
```

They are part of the version: a change to one is sent after you publish again, like a change to the HTML.

## Send a test

A test sends the template as it is saved now, published or not, with "[Test]" before the subject. A variable takes the value you give, then its default, and stays in the message as `{{NAME}}` when it has neither. It needs a subject and a message, and it is a real email that counts as one.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/templates/tmpl_06/test" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "to": "you@example.com",
  "from": "Acme <hello@mail.example.com>",
  "variables": {
    "NAME": "Ada"
  }
}'
```

TypeScript:

```ts
await opensend.templates.test('tmpl_06', {
  to: 'you@example.com',
  from: 'Acme <hello@mail.example.com>',
  variables: {
    NAME: 'Ada',
  },
})
```

CLI:

```bash
opensend templates test tmpl_06 \
  --to you@example.com \
  --from 'Acme <hello@mail.example.com>' \
  --variables '{"NAME":"Ada"}'
```

MCP:

```json
{
  "name": "templates_test",
  "arguments": {
    "id": "tmpl_06",
    "to": "you@example.com",
    "from": "Acme <hello@mail.example.com>",
    "variables": {
      "NAME": "Ada"
    }
  }
}
```

To start a new template from an old one, copy it. The copy is a draft with a free alias.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/templates/tmpl_06/duplicate" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}'
```

TypeScript:

```ts
await opensend.templates.duplicate('tmpl_06')
```

CLI:

```bash
opensend templates duplicate tmpl_06
```

MCP:

```json
{
  "name": "templates_duplicate",
  "arguments": {
    "id": "tmpl_06"
  }
}
```

## Publish it

A new template is a `draft` and cannot be sent. Publishing makes the version you saved the one that is sent.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/templates/tmpl_02/publish" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}'
```

TypeScript:

```ts
await opensend.templates.publish('tmpl_02')
```

CLI:

```bash
opensend templates publish tmpl_02
```

MCP:

```json
{
  "name": "templates_publish",
  "arguments": {
    "id": "tmpl_02"
  }
}
```

You can keep editing after that. Emails use the published version until you publish again, and `changed` is `true` while the saved version is newer than the published one.

## Versions

Every publish that changes something is kept as a version, numbered from 1, and `version` on the template says how many there were. The last 50 are kept. Publishing what is already published makes no new version.

cURL:

```bash
curl "$OPENSEND_API_URL/v1/templates/tmpl_06/versions" \
  -H "Authorization: Bearer $OPENSEND_API_KEY"
```

TypeScript:

```ts
await opensend.templates.versions('tmpl_06')
```

CLI:

```bash
opensend templates versions tmpl_06
```

MCP:

```json
{
  "name": "templates_versions",
  "arguments": {
    "id": "tmpl_06"
  }
}
```

`live` marks the version emails are built from now. One version comes back with everything it holds: the subject, the HTML, the plain text, the sender, the reply address and the defaults.

cURL:

```bash
curl "$OPENSEND_API_URL/v1/templates/tmpl_06/versions/1" \
  -H "Authorization: Bearer $OPENSEND_API_KEY"
```

TypeScript:

```ts
await opensend.templates.version('tmpl_06', {
  version: 1,
})
```

CLI:

```bash
opensend templates version tmpl_06 --version 1
```

MCP:

```json
{
  "name": "templates_version",
  "arguments": {
    "id": "tmpl_06",
    "version": 1
  }
}
```

Restoring a version copies it into the version you edit. What is sent stays as it is until you publish again, so you can look at it, change it or send a test first.

cURL:

```bash
curl -X POST "$OPENSEND_API_URL/v1/templates/tmpl_06/versions/1/restore" \
  -H "Authorization: Bearer $OPENSEND_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{}'
```

TypeScript:

```ts
await opensend.templates.restore('tmpl_06', {
  version: 1,
})
```

CLI:

```bash
opensend templates restore tmpl_06 --version 1
```

MCP:

```json
{
  "name": "templates_restore",
  "arguments": {
    "id": "tmpl_06",
    "version": 1
  }
}
```

## Send with it

Name the template by id or alias when you send, with a value for each variable. [Sending email](https://opensend.sh/docs/sending) shows the request.

- `template_missing`: there is no published template with that id or alias.
- `template_variable_missing`: a variable has no value and no default. The message names it.
- `email_no_sender`: neither the send nor the template names a sender.
- `alias_taken`: the workspace already has a template with that alias.
- `template_designed`: `html` or `text` was sent to a template built from blocks.
- `template_written`: `templates.edit` was sent to a template written as HTML. Give it a `design` with `templates.update` first.
