Templates

Send with a template

Reference a saved template from POST /emails and fill its variables.

To send an email from a template, pass template_id (and any variables) to POST /emails instead of inline html/text. MailBlastr loads the template, substitutes the variables, and sends the result. Everything else about the send — from, to, attachments, scheduling, tracking — works exactly as a normal send.

import { MailBlastr } from 'mailblastr';

const mb = new MailBlastr('mb_xxxxxxxxx');

const { data, error } = await mb.emails.send({
  "from": "Acme <hello@yourdomain.com>",
  "to": ["user@example.com"],
  "template_id": "tmpl_8f5c2a1e",
  "variables": { "first_name": "Ada", "company": "Acme" }
});
console.log({ data, error });

Overrides

Any field you set directly on the request wins over the template. For example, passing subject alongside template_id uses your subject and the template's body. This lets one template back many variations. The from, subject, and reply_to in the payload take precedence over the template's defaults for these fields; if the template defines no default for one of them, you must supply it in the payload.

When a template_id is provided, you cannot also send html or text in the same request — doing so returns a validation_error.

Variables and fallbacks

Each value in variables replaces the matching {{ key }} placeholder. If you omit a variable that the template defines, its fallback_value is used. If that variable has no fallback value, the email is not sent and a validation_error is returned.

If the resolved email has neither html nor text (e.g. the template has only a subject and you sent no body), the request fails with validation_error. A template_id that isn't yours returns not_found.