Kraiter
SDK Reference

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.

upsert

Creates a template, or replaces it if one already exists with the given ID. The template ID is passed as the first argument; the body is passed as the second.

const template = await kraiter.templates.upsert('tmpl_welcome', {
  name: 'Welcome Email',
  subject: 'Welcome to {{ companyName }}, {{ firstName }}!',
  content: `
    <mjml>
      <mj-body>
        <mj-section>
          <mj-column>
            <mj-text>Hi {{ firstName }}, thanks for signing up!</mj-text>
          </mj-column>
        </mj-section>
      </mj-body>
    </mjml>
  `,
});

Parameters

ParameterTypeRequiredDescription
templateIdstringYesA unique identifier for the template (first argument). Use a consistent prefix like tmpl_.
namestringYesA human-readable name for the template.
subjectstringYesThe email subject line. Supports Liquid variables.
contentstringYesThe template body in MJML markup.

Returns

Promise<Template> — the created or replaced template object.

Errors

CodeWhen
VALIDATION_ERRORThe MJML is invalid or required fields are missing.

get

Retrieves a template by ID. Returns null if not found. Pass includeContent: true to include the MJML body.

const template = await kraiter.templates.get('tmpl_welcome');

if (template) {
  console.log(template.name, template.subject);
}

// Include the MJML body
const full = await kraiter.templates.get('tmpl_welcome', true);

Parameters

ParameterTypeRequiredDescription
templateIdstringYesThe template ID.
includeContentbooleanNoWhen true, the returned object includes the content (MJML body).

Returns

Promise<Template | TemplateWithContent | null> — the template object (with content when includeContent is true), or null if not found.


list

Lists templates with cursor-based pagination.

const page = await kraiter.templates.list({ limit: 50 });

for (const template of page.items) {
  console.log(template.templateId, template.name);
}

Parameters

ParameterTypeRequiredDescription
limitnumberNoMaximum items per page.
cursorstringNoPagination cursor from a previous response's nextCursor.

Returns

Promise<{ items: Template[]; nextCursor?: string }> — a page 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

ParameterTypeRequiredDescription
templateIdstringYesThe template ID to update.
namestringNoUpdated template name.
subjectstringNoUpdated subject line.
contentstringNoUpdated MJML body.
enabledbooleanNoWhether the template is enabled for sending.
disableReasonstringNoA note describing why the template was disabled.

Returns

Promise<Template> — the updated template object.

Errors

CodeWhen
NOT_FOUNDNo template exists with this ID.
VALIDATION_ERRORThe updated MJML is invalid.

delete

Permanently deletes a template.

await kraiter.templates.delete('tmpl_welcome');

Parameters

ParameterTypeRequiredDescription
templateIdstringYesThe template ID to delete.

Returns

Promise<void>

Errors

CodeWhen
NOT_FOUNDNo 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 { subject, htmlBody } = await kraiter.templates.preview('tmpl_welcome', {
  firstName: 'Alice',
  companyName: 'Acme',
});

console.log(subject);  // "Welcome to Acme, Alice!"
console.log(htmlBody); // compiled HTML output

Parameters

ParameterTypeRequiredDescription
templateIdstringYesThe template ID to preview.
variablesRecord<string, unknown>NoVariables to interpolate into the template.

Returns

Promise<RenderPreviewResult> — an object with the following fields:

FieldTypeDescription
subjectstringThe interpolated subject line.
htmlBodystringThe compiled HTML body.
textBodystringThe plain-text body, when available.

Errors

CodeWhen
NOT_FOUNDNo template exists with this ID.

listVersions

Lists the version history for a template with cursor-based pagination. Each time a template is upserted, a new version is recorded.

const page = await kraiter.templates.listVersions('tmpl_welcome');

for (const version of page.items) {
  console.log(version.version, version.createdAt);
}

Parameters

ParameterTypeRequiredDescription
templateIdstringYesThe template ID.
limitnumberNoMaximum items per page.
cursorstringNoPagination cursor.

Returns

Promise<{ items: TemplateVersion[]; nextCursor?: string }> — a page of version records.

Errors

CodeWhen
NOT_FOUNDNo template exists with this ID.

getVersion

Retrieves a specific version of a template by its version identifier. Returns null if not found.

const version = await kraiter.templates.getVersion('tmpl_welcome', 'v2');

if (version) {
  console.log(version.version, version.createdAt);
  console.log(version.content); // the template content at this version
}

Parameters

ParameterTypeRequiredDescription
templateIdstringYesThe template ID.
versionstringYesThe version identifier.

Returns

Promise<TemplateVersion | null> — the version record, or null if not found.

Errors

CodeWhen
NOT_FOUNDNo template or version exists with these IDs.