Skip to Content

Create a mail template.

Registers a template. The From address must be one this account may already send as; the copy is added per language afterwards.

openemail.templates.create(
  accountId: string,
  body: TemplatesCreateBody,
  options?: CallOptions,
): ApiPromise<MailTemplate>

It calls POST /accounts/{accountId}/templates: Create a mail template in the API reference.

Example

const mailTemplate = await openemail.templates.create(accountId, { slug: '…', name: 'Ada', from: 'ada@example.com' })

Arguments

ArgumentTypeDescription
accountIdstringAccount ULID identifier.. The path's {accountId}.
bodyTemplatesCreateBodyThe request's body.

Options

It takes no options of its own. Every call also takes signal, timeout, retry and headers, which say how that one call is sent (call options).

Answer

Awaited, it is the API's JSON, MailTemplate. It is an ApiPromise: .withResponse() gives the same with the Response it came in, for its headers.

Errors

StatusWhat it means
400reserved_slug, from_not_sendable or an invalid variable declaration
401unauthorized: the bearer credential is missing, malformed, expired, or revoked. Carries a WWW-Authenticate: Bearer challenge
404not_found
409slug_taken, or template_limit_reached carrying the effective ceiling
411length_required: Content-Length is required
413too_large: the JSON body exceeds 2 MiB
503temporary_failure: the platform's storage could not answer just now; retry after the Retry-After window. Any operation can answer this. A write that got it may have been applied, so retry it with the same idempotency key where it takes one, or read the state back first (see Transient failures in the API description)

A failed call rejects with a RequestError: its status, and its code, the API's error. Decide on the code: one status covers several.

Types

TemplatesCreateBody

/** The body of `templates.create`. */
type TemplatesCreateBody = {
  /** Language tag, e.g. `en` or `de-AT`. Stored lowercased. */
  defaultLang?: string
  from: string
  name: string
  replyTo?: string | null
  /** The template's name within this account. */
  slug: string
  /** Documentation and dummy data per variable. Never a schema: a body may use what this omits. */
  variables?: Record<string, TemplatesCreateBodyVariablesValue>
}

TemplatesCreateBodyVariablesValue

/** The API's `TemplatesCreateBodyVariablesValue`, as the document describes it. */
type TemplatesCreateBodyVariablesValue = {
  description?: string
  /** Dummy data for the preview. A flag's sample is a boolean: the string "false" is truthy */
  sample?: string | number | boolean
}

It names these types of the API, each on a page of its own: MailTemplate.