Skip to main content
routa.templates creates templates, uploads the file of a media header, and retrieves and lists templates. For the lifecycle, the statuses and the WhatsApp rules, see Templates.

Create a template

A body-only template needs channel, name, language, category and bodyText. When the body has a variable, it also needs variableExamples.
language is the code WhatsApp uses, such as pt_BR, en_US or en. The region is joined with an underscore: en_US, not en-US. The response carries status: 'submitted', not approved. Routa records the template first and sends it to the provider afterwards, and the provider decides in its own time. Follow the result through the template.* events, with webhooks or routa.events.list().

Variables and examples

The API requires an example for every variable. These fields exist from version 0.5.0. An example cannot be empty or blank, and whatever has no variable takes no example. A media header, a quick_reply button and a phone_number button have no variable, so the types do not accept example on them. See Variable examples.
Use made-up values. Examples are stored with the template, returned by the API and sent in template.* events and webhooks, so do not put real personal data or a real token in them.
A version earlier than 0.5.0 does not have these fields and drops a value passed anyway. That is why the API refuses the submit() of a template with a variable, with 422 template_variables_invalid. Upgrade the SDK.

Components

components accepts a header, a footer and up to 10 buttons. Leave components out for a body-only template.
Each part is a union on kind. That is why a url button without url, a media header without mediaId or a quick_reply with example does not compile. The SDK only checks the shape. Lengths, the number of buttons, the URL and phone formats and the WhatsApp rules belong to the API, which responds 422 with the field at fault. See the rules in Templates.

Image, video or document header

Upload the file with uploadMedia() and pass the returned id as mediaId:
The upload is synchronous: the TemplateMedia can be used right away. The API derives the format from the contentType. This file is the sample WhatsApp reviews. To send the template, upload the file of the message with media.upload() and pass its id in headerParameter.mediaId. See Send a template.
  • contentType is sent as the Content-Type of the request. The SDK does not read it from the type of a Blob.
  • The first bytes of the file must match the contentType.
  • contentType is compared exactly: image/png; charset=binary is refused.
  • Unlike media.upload(), there is no sourceUrl mode.

The TemplateMediaId

media.id is a TemplateMediaId (tmedia_...), a branded type of its own. A MediaId from media.upload() or a plain string does not work as mediaId: the compiler refuses it. It is a different kind of file, and only a template header can use it. On a template you read, components.header.media_id is already a TemplateMediaId, so you can pass it back as mediaId.

Read a template

components does not exist on a body-only template: it is omitted, not an empty object. Guard your reads with template.components?.. The new fields of 0.3.0 are bodyText, components, failureCode, failureDetail, deletionRequested, deletionFailureDetail, origin, readOnly and unsupportedComponents. From 0.5.0, the examples come back in variables[].example, components.header.example and components.buttons[].example. The key is absent when there is no example, as on an old template. On an imported template, the example is whatever WhatsApp stores, so a button example can be a whole URL.

Handle the status

See the values of failureCode and what each one means in When sending for approval fails.

Errors

The API refuses to create an invalid template with one of these errors. The SDK throws them as RoutaInvalidRequestError, with the param field when the API gives it.
  • The param of template_components_invalid names the component, not the item inside it. There is no such thing as components.buttons.0.url. The message says what is wrong.
  • param uses the API names (variable_examples, not variableExamples), with positions starting at 0: variable_examples.1 is the example of {{2}}.
  • On template_variables_invalid, error.details?.['reason'] says which case it is. See the reasons.
  • The API reports the first problem it finds, so fixing one can reveal the next.
  • The API checks the channel before the media: an unknown channel hides an invalid mediaId.
  • A file over 100 MiB gets 413 payload_too_large, as a base RoutaApiError. It is not retried.

Not idempotent

POST /v1/templates and POST /v1/templates/media do not read Idempotency-Key, so the SDK does not send one. The client still retries a network failure or a 5xx. If the first attempt was already recorded and only the response was lost, the retry creates a second template with the same name (with another tmpl_...), which the provider can refuse as a duplicate. On an upload, it stores the file twice.
Set maxRetries: 0 on the client if a duplicate matters more to you than resilience to an unstable connection. After an error whose response you did not read, use templates.list() before you create again.

What the SDK does not do

The SDK does not edit, delete or sync templates. Those operations exist only in the dashboard, and the API does not offer them to an API key. A failed or rejected template can be edited and sent again in the dashboard. Through the SDK, create a new one with submit(). When the dashboard edits or deletes a template, you receive template.updated or template.deleted.