Templates
Versioned transactional templates with variables, edited in the dashboard or through the Resend-compatible API.
A template holds the subject, sender and body of a transactional email (welcome, password reset, receipt) with variables. Your code sends the template's id or alias and the values; your team edits the email in the dashboard (Settings → Templates, with the visual editor) and publishes it, without a deploy. The smartletter SDK, the API and the Resend SDK's templates.* methods all work the same.
import { Smartletter } from "smartletter";
const sl = new Smartletter(process.env.SMARTLETTER_API_KEY);
// Once (or in the dashboard): create and publish.
await sl.templates
.create({
name: "Order receipt",
alias: "order-receipt",
from: "Acme <[email protected]>",
subject: "Your order {{{ORDER_NUMBER}}}",
html: "<p>Thanks! Order {{{ORDER_NUMBER}}} is on its way.</p>",
variables: [{ key: "ORDER_NUMBER", type: "string" }],
})
.publish();
// Each order: the alias and the values.
await sl.emails.send({
from: "Acme <[email protected]>",
to: ["[email protected]"],
template: { id: "order-receipt", variables: { ORDER_NUMBER: "A-1042" } },
});Already using the Resend SDK?
The official Resend SDK works too: point it at Smartletter and the same templates.* calls run unchanged.
import { Resend } from "resend";
const resend = new Resend(process.env.SMARTLETTER_API_KEY, { baseUrl: "https://api.smartletter.ai" });
// Once (or in the dashboard): create and publish.
await resend.templates
.create({
name: "Order receipt",
alias: "order-receipt",
from: "Acme <[email protected]>",
subject: "Your order {{{ORDER_NUMBER}}}",
html: "<p>Thanks! Order {{{ORDER_NUMBER}}} is on its way.</p>",
variables: [{ key: "ORDER_NUMBER", type: "string" }],
})
.publish();
// Each order: the alias and the values.
await resend.emails.send({
to: ["[email protected]"],
template: { id: "order-receipt", variables: { ORDER_NUMBER: "A-1042" } },
});Variables
- Declare each variable:
key(letters, digits, underscore),type(stringornumber), an optionalfallback_value, andrequired(defaulttruewithout a fallback). Up to 50 per template. - Write them as
{{{KEY}}}(as Resend) or{{KEY}}in the subject, HTML and text. Filters work too:{{KEY | default: "friend" | capitalize}}. - Values are always HTML-escaped in the body, triple braces included: a value can never inject markup.
- At send time: the value you pass, else the fallback. A required variable without a value, or a number variable given text, refuses the email with a 422 that lists them (
details.missing,details.invalid). Keys you pass that the template does not declare are ignored. - Reserved keys:
FIRST_NAME,LAST_NAME,EMAIL,UNSUBSCRIBE_URL,RESEND_UNSUBSCRIBE_URL,contact,this. A tag that is not declared is refused when you save.
Versions and publishing
- Every change of content saves a new version (number, note, author, date). Versions never change.
- Sends always use the published version. Editing never touches what is live until you publish again (
POST /templates/{id}/publish), andversion_idrolls back to an older one. GET /templates/{id}/versionslists the history; the dashboard restores any version as a new draft.- Archive a template to stop it from sending (422
This template is archived.); delete it to remove it and its versions. Emails already sent keep their content.
Sending
POST /emails,POST /emails/batchand scheduled emails accepttemplate: { id, variables }, whereidis the template's id or alias.html,textandreactare refused next totemplate.from,subjectandreply_tofrom the call override the template's; without either, the template must have them.- The text part is the template's own text when it has one, otherwise generated from the rendered HTML.
GET /emails/{id}shows the rendered email.