Templates

Create reusable email templates with variable substitution for consistent messaging across your application.

Create a Template

Create a new email template with support for variables.

POST /v1/templates
create-template.ts
const template = await client.templates.create({
  name: 'Order Confirmation',
  subject: 'Order #{{orderNumber}} confirmed',
  html: `
    <h1>Thanks for your order, {{firstName}}!</h1>
    <p>Order #{{orderNumber}} has been confirmed.</p>

    <h2>Items</h2>
    <ul>
      {{#each items}}
        <li>{{this.name}} - ${{this.price}}</li>
      {{/each}}
    </ul>

    <p><strong>Total: ${{total}}</strong></p>
  `,
  variables: [
    { name: 'firstName', type: 'string', required: true },
    { name: 'orderNumber', type: 'string', required: true },
    { name: 'items', type: 'array', required: true },
    { name: 'total', type: 'number', required: true },
  ],
  type: 'transactional',
});

Template Variables

Templates support Handlebars-style variable syntax for dynamic content.

Basic Variables

<!-- Template -->
<p>Hello, {{firstName}}!</p>

<!-- With variables: { firstName: 'John' } -->
<p>Hello, John!</p>

Conditionals

{{#if isPremium}}
  <p>Thanks for being a premium member!</p>
{{else}}
  <p>Upgrade to premium for more features.</p>
{{/if}}

Loops

<ul>
  {{#each items}}
    <li>{{this.name}}: ${{this.price}}</li>
  {{/each}}
</ul>

Variable Schema

Define your variable schema for validation and documentation:

PropertyTypeDescription
namestringVariable name (required)
typestringstring | number | boolean | array | object
requiredbooleanWhether the variable is required
defaultValueanyDefault value if not provided
descriptionstringDescription for documentation

List Templates

GET /v1/templates
list-templates.ts
const { data } = await client.templates.list();

for (const template of data) {
  console.log(template.name, template.type);
}

Update Template

PATCH /v1/templates/:id
update-template.ts
const template = await client.templates.update('template_xxxxx', {
  subject: 'Updated: Order #{{orderNumber}} confirmed',
  html: '<h1>Updated content</h1>',
});

Delete Template

DELETE /v1/templates/:id
delete-template.ts
await client.templates.delete('template_xxxxx');

Template Types

Templates can be categorized by type:

TypeUse Case
transactionalOrder confirmations, password resets, receipts
marketingNewsletters, promotions, announcements