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.
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)# Create a draft
curl -X POST https://api.honkio.ca/v1/email-templates -H "Authorization: Bearer mk_live_YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{ "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" }] }'// 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# Publish it as version 1
curl -X POST https://api.honkio.ca/v1/email-templates/shipping-update/publish -H "Authorization: Bearer mk_live_YOUR_KEY"// 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)# Send it by alias
curl -X POST https://api.honkio.ca/v1/emails -H "Authorization: Bearer mk_live_YOUR_KEY" \
-H "Content-Type: application/json" \
-d '{ "from": "noreply@mail.acme.ca", "to": "ada@example.com",
"template": { "id": "shipping-update", "variables": { "tracking": "1Z999" } } }'// 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)# Roll back: copies version 1 into the draft and publishes it as a new version
curl -X POST https://api.honkio.ca/v1/email-templates/shipping-update/rollback -H "Authorization: Bearer mk_live_YOUR_KEY" \
-H "Content-Type: application/json" -d '{ "version": 1 }'
HonkIO