Templates
Create reusable, dynamic email templates with Handlebars syntax. Templates support conditionals, loops, and partials.
Creating a Template
Templates are created in the PostaSend dashboard under Templates, or programmatically via the API. Each template has a unique ID (e.g. tmpl_welcome_v1) and supports Handlebars syntax for dynamic content. Templates can include an HTML body, plain text fallback, and a subject line — all of which support variable interpolation.
const template = await client.templates.create({
name: 'welcome-email-v1',
subject: 'Welcome to {{appName}}, {{firstName}}!',
html: `
<h1>Welcome, {{firstName}}!</h1>
<p>Your {{plan}} plan is now active.</p>
{{#if trialDays}}
<p>You have {{trialDays}} days left in your free trial.</p>
{{/if}}
<a href="{{dashboardUrl}}">Go to dashboard</a>
`,
text: 'Welcome, {{firstName}}! Your {{plan}} plan is now active.',
});
console.log(template.id); // 'tmpl_abc123'Sending with a Template
To send an email using a template, pass the templateId and a data object containing the values for your template variables. PostaSend validates that all required variables are present before queuing the email.
await client.emails.send({
from: 'hello@yourdomain.com',
to: ['newuser@example.com'],
templateId: 'tmpl_abc123',
data: {
firstName: 'Alice',
appName: 'Acme',
plan: 'Growth',
trialDays: 14,
dashboardUrl: 'https://app.acme.com/dashboard',
},
});Template Versioning
Every time you update a template, PostaSend creates a new version. By default, sends use the latest published version. You can pin a send to a specific version by passing templateVersion alongside templateId. This is useful for A/B testing or rolling back a bad template change.
// Send with specific version
await client.emails.send({
from: 'hello@yourdomain.com',
to: ['user@example.com'],
templateId: 'tmpl_abc123',
templateVersion: 3,
data: { firstName: 'Alice' },
});
// List template versions
const versions = await client.templates.versions('tmpl_abc123');
// [{ version: 4, publishedAt: ..., status: 'published' }, ...]Previewing Templates
Preview a rendered template with sample data before sending. The preview API returns the fully rendered HTML and text, making it easy to build a preview UI in your dashboard.
const preview = await client.templates.preview('tmpl_abc123', {
data: {
firstName: 'Preview User',
appName: 'Acme',
plan: 'Starter',
dashboardUrl: 'https://app.acme.com',
},
});
console.log(preview.html); // fully rendered HTML string
console.log(preview.text); // fully rendered text string
console.log(preview.subject); // 'Welcome to Acme, Preview User!'