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.

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

ParameterTypeRequiredDescription
idstringYesA unique identifier for the template. Use a consistent prefix like tmpl_.
namestringYesA human-readable name for the template.
subjectstringYesThe email subject line. Supports Liquid variables.
mjmlstringYesThe template body in MJML markup.
variablesstring[]NoA list of variable names the template expects. Used for validation and documentation.

Returns

Promise<Template> — the newly created template object.

Errors

CodeWhen
CONFLICTA template with this ID already exists.
VALIDATION_ERRORThe 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

ParameterTypeRequiredDescription
idstringYesThe template ID.

Returns

Promise<Template> — the template object.

Errors

CodeWhen
NOT_FOUNDNo 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

ParameterTypeRequiredDescription
cursorstringNoPagination cursor.
limitnumberNoMaximum 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

ParameterTypeRequiredDescription
idstringYesThe template ID to update.
namestringNoUpdated template name.
subjectstringNoUpdated subject line.
mjmlstringNoUpdated MJML body.
variablesstring[]NoUpdated list of expected variables.

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

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

Parameters

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

Returns

Promise<{ html: string; subject: string }> — the rendered HTML body and interpolated subject line.

Errors

CodeWhen
NOT_FOUNDNo 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

ParameterTypeRequiredDescription
idstringYesThe template ID.

Returns

Promise<TemplateVersion[]> — an array of version records, ordered from newest to oldest.

Errors

CodeWhen
NOT_FOUNDNo template exists with this ID.