Templates
SDK reference for creating, managing, and previewing email templates in Kraiter.
The kraiter.templates namespace provides methods for managing email templates. Templates define the subject line and MJML body of your emails. They support Liquid variables for dynamic content.
create
Creates a new template.
const template = await kraiter.templates.create({
id: 'tmpl_welcome',
name: 'Welcome Email',
subject: 'Welcome to {{ companyName }}, {{ firstName }}!',
mjml: `
<mjml>
<mj-body>
<mj-section>
<mj-column>
<mj-text>Hi {{ firstName }}, thanks for signing up!</mj-text>
</mj-column>
</mj-section>
</mj-body>
</mjml>
`,
variables: ['firstName', 'companyName'],
});Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | A unique identifier for the template. Use a consistent prefix like tmpl_. |
name | string | Yes | A human-readable name for the template. |
subject | string | Yes | The email subject line. Supports Liquid variables. |
mjml | string | Yes | The template body in MJML markup. |
variables | string[] | No | A list of variable names the template expects. Used for validation and documentation. |
Returns
Promise<Template> — the newly created template object.
Errors
| Code | When |
|---|---|
CONFLICT | A template with this ID already exists. |
VALIDATION_ERROR | The MJML is invalid or required fields are missing. |
get
Retrieves a template by ID.
const template = await kraiter.templates.get('tmpl_welcome');
console.log(template.name, template.subject);Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | The template ID. |
Returns
Promise<Template> — the template object.
Errors
| Code | When |
|---|---|
NOT_FOUND | No template exists with this ID. |
list
Lists all templates. Returns an async iterator.
for await (const template of kraiter.templates.list()) {
console.log(template.id, template.name);
}Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
cursor | string | No | Pagination cursor. |
limit | number | No | Maximum items per page. |
Returns
AsyncIterable<Template> — an async iterator of template objects.
update
Updates an existing template. Only the fields you include are changed.
const updated = await kraiter.templates.update('tmpl_welcome', {
subject: 'Welcome aboard, {{ firstName }}!',
});Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | The template ID to update. |
name | string | No | Updated template name. |
subject | string | No | Updated subject line. |
mjml | string | No | Updated MJML body. |
variables | string[] | No | Updated list of expected variables. |
Returns
Promise<Template> — the updated template object.
Errors
| Code | When |
|---|---|
NOT_FOUND | No template exists with this ID. |
VALIDATION_ERROR | The updated MJML is invalid. |
delete
Permanently deletes a template.
await kraiter.templates.delete('tmpl_welcome');Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | The template ID to delete. |
Returns
Promise<void>
Errors
| Code | When |
|---|---|
NOT_FOUND | No template exists with this ID. |
preview
Renders a template with the given variables and returns the compiled HTML and subject line. Useful for showing a preview in your UI before sending.
const { html, subject } = await kraiter.templates.preview('tmpl_welcome', {
firstName: 'Alice',
companyName: 'Acme',
});
console.log(subject); // "Welcome to Acme, Alice!"
console.log(html); // compiled HTML outputParameters
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | The template ID to preview. |
variables | Record<string, unknown> | No | Variables to interpolate into the template. |
Returns
Promise<{ html: string; subject: string }> — the rendered HTML body and interpolated subject line.
Errors
| Code | When |
|---|---|
NOT_FOUND | No template exists with this ID. |
getVersions
Returns the version history for a template. Each time a template is updated, a new version is recorded.
const versions = await kraiter.templates.getVersions('tmpl_welcome');
for (const version of versions) {
console.log(version.versionId, version.createdAt, version.subject);
}Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
id | string | Yes | The template ID. |
Returns
Promise<TemplateVersion[]> — an array of version records, ordered from newest to oldest.
Errors
| Code | When |
|---|---|
NOT_FOUND | No template exists with this ID. |