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/templatescreate-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:
| Property | Type | Description |
|---|---|---|
| name | string | Variable name (required) |
| type | string | string | number | boolean | array | object |
| required | boolean | Whether the variable is required |
| defaultValue | any | Default value if not provided |
| description | string | Description for documentation |
List Templates
GET /v1/templateslist-templates.ts
const { data } = await client.templates.list();
for (const template of data) {
console.log(template.name, template.type);
}Update Template
PATCH /v1/templates/:idupdate-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/:iddelete-template.ts
await client.templates.delete('template_xxxxx');Template Types
Templates can be categorized by type:
| Type | Use Case |
|---|---|
transactional | Order confirmations, password resets, receipts |
marketing | Newsletters, promotions, announcements |