Email

Templates

Store a template once, then send it with variables. Edits save to a draft, and publishing makes the draft the next numbered version.

Templates

Templates use double braces around a variable name of letters, digits and underscores. Triple braces are accepted too and are treated the same: values are always HTML escaped in html. There are no conditionals or loops.

text
Subject: Your order has shipped, {{ first_name }}
<p>Hi {{ first_name }}, track it with {{ tracking }}.</p>

"variables": [
  { "key": "first_name", "fallback": "there" },
  { "key": "tracking" }
]

Every variable a template uses must be declared, optionally with a fallback. A send that leaves a variable with neither a value nor a fallback is refused and lists the missing keys.

Edits save to a draft. Publishing makes the draft the next numbered version, which sends then use; rolling back publishes an old version again as a new one. Scheduled emails keep the version they were sent with. Send by id or alias, and a from or reply_to in the request overrides the template's.

// Create a draft
const { data: template, error } = await honkio.templates.create({
  name: 'Shipping update',
  alias: 'shipping-update',
  subject: 'Shipped, {{ first_name }}',
  html: '<p>Track it with {{ tracking }}.</p>',
  variables: [{ key: 'first_name', fallback: 'there' }, { key: 'tracking' }],
})
if (error) throw new Error(error.message)
console.log(template.id)
// Publish it as version 1
const { data, error } = await honkio.templates.publish('shipping-update')
if (error) throw new Error(error.message)
console.log(data.published_version) // 1
// Send it by alias
const { data, error } = await honkio.emails.send({
  from: 'noreply@mail.acme.ca',
  to: 'ada@example.com',
  template: { id: 'shipping-update', variables: { tracking: '1Z999' } },
})
if (error) throw new Error(error.message)
console.log(data.id)
// Roll back: copies version 1 into the draft and publishes it as a new version
const { data, error } = await honkio.templates.rollback('shipping-update', { version: 1 })
if (error) throw new Error(error.message)
console.log(data.published_version)