> ## Documentation Index
> Fetch the complete documentation index at: https://docs.routa.chat/llms.txt
> Use this file to discover all available pages before exploring further.

# Templates

> How to create WhatsApp templates, follow their approval and send them, including header, footer and buttons.

A **template** is a reusable message body that WhatsApp must approve before you can send it. Use templates for messages your business starts, such as order confirmations, reminders and verification codes.

You create the template, and Routa sends it for review and follows the result. **The provider decides on approval, in its own time.** A template belongs to a **channel**: the same name can be approved on one channel and rejected on another.

## Lifecycle

```text theme={null}
draft → submitted → pending → approved
                            → rejected
        submitted → failed             (Routa could not send it to the provider)
approved → paused | disabled           (the provider can do this at any time)
failed | rejected → submitted         (edit in the dashboard)
```

| Status | Meaning |
| - | - |
| `draft` | Created, not sent for review yet. |
| `submitted` | Sent to the provider. |
| `pending` | Under review. |
| `approved` | Approved. **Only approved templates can be used to send.** |
| `rejected` | Rejected. The `rejection_reason` field explains why. |
| `paused` | Paused by the provider, for low quality for example. It cannot be used to send. |
| `disabled` | Disabled by the provider. |
| `failed` | Routa could not send the template to the provider. The `failure_code` field explains why. See [When sending for approval fails](#when-sending-for-approval-fails). |

`rejected` and `failed` are not the same thing. With `rejected`, the provider reviewed the template and refused it. With `failed`, no review happened, because the template never made it through to the provider.

Each change produces an event (`template.submitted`, `template.pending`, `template.approved`, `template.rejected`, `template.paused`, `template.disabled`, `template.failed`, `template.updated`, `template.deleted`). Subscribe to `template.*` to follow approval without polling the API. The `template.category_changed` event tells you when the provider changes the category of a template. `template.updated` tells you a `failed` or `rejected` template was edited and sent again, and `template.deleted`, that it was removed. See [Event types](https://docs.routa.chat/en/webhooks/event-types.md#template-events).

### Template fields

Besides `id`, `channel`, `name`, `language`, `category`, `variables`, `components`, `status` and the reason fields, the template carries:

| Field | Description |
| - | - |
| `body_text` | The body as written, with the `{{1}}`, `{{2}}` placeholders. It is `null` only on an imported, read-only template, which Routa cannot reproduce. |
| `variables` | One entry per body variable, with `index` and, when there is one, `example`. |
| `components` | Header, footer and buttons. The text header and URL buttons carry `example` when there is one. Absent on a body-only template. |
| `failure_detail` | The provider's own sentence about the refusal, only while `status` is `failed` and when the provider gave one. Otherwise it is `null`. It is text for people: do not compare against it; use `failure_code`. |
| `deletion_requested` | `true` from the moment the deletion is accepted until the template leaves the list. In between, it cannot be used to send. |
| `deletion_failure_detail` | Why the last deletion did not finish, when the provider gave a reason. It is `null` if no deletion failed. |

### Rejection reasons

`rejection_reason` uses a closed set of Routa values, not provider codes.

| Value | Description |
| - | - |
| `abusive_content` | Abusive content. |
| `incorrect_category` | The chosen category does not match the content. |
| `invalid_format` | Invalid format. |
| `scam` | Suspected scam. |
| `tag_content_mismatch` | The content does not match the declared tag. |
| `other` | Another reason. |

### When sending for approval fails

Routa tries to send the template to the provider **up to 5 times**, with growing delays between attempts (from 1 minute to 1 hour). Only a temporary provider failure is retried. Any other reason ends the process on the first attempt. When it ends without success, the template becomes `failed` and `template.failed` is produced.

`failure_code` is filled in only when `status` is `failed`. In every other status it is `null`. It uses a closed set of Routa values, not provider codes.

| Value | Description |
| - | - |
| `provider_unavailable` | The provider was unavailable, rate limited the requests or answered with an error on every attempt. |
| `provider_auth_failed` | The provider refused the channel credential. Check the channel connection in the dashboard. |
| `provider_rejected_request` | The provider found the approval request invalid. Review the fields and create the template again. |
| `channel_unavailable` | The channel is disabled, was removed or no longer supports templates. |
| `media_unavailable` | The file of the media header is no longer available. Upload the media again. |
| `attempts_exhausted` | The attempts ran out without a more specific reason being recorded. |

Through the API, you do not resend a `failed` template: create it again with `POST /v1/templates`. In the dashboard, you can [edit and send again](#edit-and-delete-a-template) a `failed` or `rejected` template.

> [!NOTE]
>
> A failed attempt may have reached the provider without Routa seeing the response. In that case, the provider can still answer later, and the template moves from `failed` to `pending`, `approved` or `rejected`. Treat `failed` as a state the provider's answer can correct.

## Imported templates

Besides the templates you create through the API, Routa lists the ones that **already existed on the WhatsApp side** of the channel. These templates are imported, with the status the provider has for them.

| Field | Description |
| - | - |
| `origin` | `routa` for a template created by Routa. `provider` for a template that already existed and was imported. |
| `read_only` | `true` when Routa cannot reproduce the imported template. Always `false` when `origin` is `routa`. |
| `unsupported_components` | List of the components Routa cannot reproduce. Empty when `read_only` is `false`. |

> [!WARNING]
>
> A template with `read_only: true` appears in the list but **cannot be used to send**. `POST /v1/messages` with it returns `422 template_not_sendable`. If it is `failed` or `rejected`, you can [edit it in the dashboard](#edit-and-delete-a-template). After the edit, it is no longer read-only. Otherwise, create an equivalent template through the API or use another one.

The import is **silent**: it produces no event or webhook, because you did nothing to make the template appear. Call `GET /v1/templates` to see what was imported.

`unsupported_components` uses a closed set of Routa values.

| Value | Description |
| - | - |
| `named_parameters` | The body uses named variables instead of positional ones (`{{1}}`, `{{2}}`). |
| `media_header` | The template has an image, video or document header. |
| `location_header` | The template has a location header. |
| `carousel` | The template is a carousel. |
| `flow_button` | The template has a button that opens a flow. |
| `otp_button` | The template has a one-time code button. |
| `copy_code_button` | The template has a copy code button. |
| `catalog` | The template uses a product catalog. |
| `limited_time_offer` | The template is a limited-time offer. |
| `other` | Another component Routa cannot reproduce. |

## Create a template

`POST /v1/templates` creates the template and sends it for review. Provide the channel, the name, the language, the category, the body with positional `{{1}}`, `{{2}}` variables and an [example for each variable](#variable-examples).

```bash theme={null}
curl https://api.routa.chat/v1/templates \
  -H "Authorization: Bearer $ROUTA_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "channel": "chan_01J8...",
    "name": "order_confirmation",
    "language": "en_US",
    "category": "utility",
    "body_text": "Hi {{1}}! Your order {{2}} is confirmed.",
    "variable_examples": ["Ana", "1234"]
  }'
```

| Field | Description |
| - | - |
| `channel` | Channel the template belongs to. |
| `name` | Template name. |
| `language` | The language code WhatsApp uses, such as `pt_BR`, `en_US` or `en`. The region is joined with an underscore: `en_US`, not `en-US`. |
| `category` | `marketing`, `utility` or `authentication`. |
| `body_text` | Body with positional `{{1}}`, `{{2}}`, … placeholders. |
| `variable_examples` | One example per body variable. Required when the body has a variable. |
| `components` | Optional. Header, footer and buttons. See below. |

The response returns the template with `variables` (the body placeholders and their examples) and the current `status`.

## Variable examples

WhatsApp reads one example of each variable when it reviews the template. The API requires an example for **every** variable:

| Where the variable is | Example field | What goes in it |
| - | - | - |
| Body | `variable_examples` | One item per variable, the first one for `{{1}}`. The order of the numbers counts, not the order in the text. |
| Text header | `components.header.example` | The example of the header `{{1}}`. |
| URL button | `components.buttons[].example` | **Only the value** that replaces `{{1}}`, not the whole URL. |

An example cannot be empty or blank. Whatever has no variable takes no example: a media header, a fixed text, a quick reply, a phone button or a fixed URL.

```json theme={null}
{
  "channel": "chan_01J8...",
  "name": "invoice_available",
  "language": "en_US",
  "category": "utility",
  "body_text": "Hi {{1}}, invoice {{2}} is here.",
  "variable_examples": ["Ana", "1234"],
  "components": {
    "header": { "kind": "text", "text": "Order {{1}}", "example": "A-10" },
    "buttons": [
      {
        "kind": "url",
        "text": "View invoice",
        "url": "https://example.com/invoices/{{1}}",
        "example": "1234"
      }
    ]
  }
}
```

> [!WARNING]
>
> Do not use real personal data or tokens in examples. No recipient receives them, but they are stored on the template, come back in API responses and travel in events and webhooks.

A missing, extra or empty example returns `422 template_variables_invalid`, and `error.param` points at the field:

| `error.param` | Problem |
| - | - |
| `variable_examples` | The list is missing, or does not have exactly one item per body variable. |
| `variable_examples.<position>` | A body example is empty. |
| `components.header.example` | The header example is missing or empty, or the header has no variable. |
| `components.buttons.<position>.example` | The button example is missing or empty, or the button has no variable. |

Positions start at 0: `variable_examples.1` is the example of `{{2}}`, and the number in `components.buttons.0.example` is the position of the button. The API reports one problem at a time, and `error.details.reason` says [which case it is](https://docs.routa.chat/en/errors/error-codes.md#reasons-for-each-code).

A template created before examples were required, or imported from WhatsApp without them, has no `example`: the key is absent, and you can still send it. On an imported template, the example is whatever WhatsApp stores, so a button example can be a whole URL.

## Rich components

The optional `components` field accepts three slots, all optional. When the template is text only, omit `components` altogether.

```json theme={null}
{
  "components": {
    "header": { "kind": "text", "text": "Order {{1}}", "example": "A-10" },
    "footer": { "text": "Thanks for shopping with us" },
    "buttons": [
      { "kind": "quick_reply", "text": "Confirm" },
      { "kind": "phone_number", "text": "Call us", "phone_number": "+5581999999999" },
      {
        "kind": "url",
        "text": "Track order",
        "url": "https://example.com/orders/{{1}}",
        "example": "1234"
      }
    ]
  }
}
```

### Header

| `kind` | Fields | Rules |
| - | - | - |
| `text` | `text`, `example` | At most 60 characters and at most one `{{1}}` variable. With the variable, `example` is required. |
| `media` | `media_id` | References media uploaded beforehand with [`POST /v1/templates/media`](https://docs.routa.chat/en/api-reference/endpoint/templates/upload-media). Does not accept `example`. |

For a media header, upload the file first and use the returned `id`:

```bash theme={null}
curl https://api.routa.chat/v1/templates/media \
  -H "Authorization: Bearer $ROUTA_API_KEY" \
  -H "Content-Type: image/png" \
  --data-binary @banner.png
```

| Format | Accepted types | Maximum size |
| - | - | - |
| `image` | `image/jpeg`, `image/png` | 5 MB |
| `video` | `video/mp4` | 16 MB |
| `document` | `application/pdf` | 100 MB |

The format is inferred from the `Content-Type` of the upload.

This file is the sample WhatsApp reviews. Recipients never receive it: each message names its own file, uploaded with `POST /v1/media`. See [File in the header](https://docs.routa.chat/en/guides/send-template-message.md#file-in-the-header).

### Footer

An object `{ "text": "..." }` with up to 60 characters.

### Buttons

Up to **10 buttons**, in the order you send them. Each button text has at most 25 characters.

| `kind` | Extra fields | Rules |
| - | - | - |
| `quick_reply` | n/a | Quick reply. |
| `phone_number` | `phone_number` | At most one button of this kind per template. |
| `url` | `url`, `example` | The URL can hold exactly one `{{1}}` variable. With the variable, `example` is required. |

A component outside these rules returns `422` with the `template_components_invalid` code. See all the rules in [Validation rules at accept time](#validation-rules-at-accept-time).

## Validation rules at accept time

Routa refuses **right away**, with `422`, what WhatsApp is already known to refuse, and a variable with no example. Without this, the template would become `failed` minutes later. When the error carries `param`, it points at the part to fix.

| Part | Rule | Error and `param` |
| - | - | - |
| Name | Only `a-z`, `0-9` and `_`, up to 512 characters. | `template_name_invalid`, `name` |
| Body | Up to 1024 characters. | `template_components_invalid`, `body_text` |
| Body | Cannot start or end with a `{{n}}` variable. Write text before the first one and after the last one. | `template_variables_invalid` |
| Variables | Numbered in sequence from `{{1}}`, with no gaps or repeats. | `template_variables_invalid` |
| Examples | One per variable, and only where there is a variable. See [Variable examples](#variable-examples). | `template_variables_invalid`, `param` with the example field |
| Header | Text of up to 60 characters and at most one variable. | `template_components_invalid`, `components.header` |
| Footer | Up to 60 characters. | `template_components_invalid`, `components.footer` |
| Buttons | At most 10, 1 phone button and 2 URL buttons. | `template_components_invalid`, `components.buttons` |
| Buttons | Quick replies stay together, before or after the other buttons, never mixed among them. | `template_components_invalid`, `components.buttons` |
| Button | Text of up to 25 characters. | `template_components_invalid`, `components.buttons` |
| URL button | Up to 2000 characters. The `{{1}}` variable can only be at the end of the URL. | `template_components_invalid`, `components.buttons` |
| URL button | Cannot point to `api.whatsapp.com`. | `template_components_invalid`, `components.buttons` |
| Phone button | The number has up to 20 characters. | `template_components_invalid`, `components.buttons` |
| `authentication` category | Does not accept custom buttons. | `template_components_invalid`, `components.buttons` |

> [!NOTE]
>
> Passing these rules does not guarantee approval. WhatsApp decides, and can refuse a template for other reasons.

## Edit and delete a template

Editing and deleting a template are **dashboard** actions. The data API does not expose them, but you see their effect in the template fields and in events.

### Edit

* You can only edit a `failed` or `rejected` template, including one imported from WhatsApp. A template with a requested deletion cannot be edited.
* The edit replaces the category, the body, the examples and every component. Name and language do not change. Every variable needs an example, even on a template that had none.
* The template goes back to `submitted`, the refusal reason and `failure_detail` are cleared, and the `template.updated` event is produced. From there, the cycle runs as for a new template.
* An imported, read-only template stops being `read_only` after the edit, because it now has a body and components Routa knows. `origin` stays `provider`.

### Delete

Any template can be deleted. What happens depends on whether WhatsApp already has the template:

* **No provider reference, or `disabled`:** Routa removes it right away and produces `template.deleted`.
* **Otherwise:** the deletion is requested and the template gets `deletion_requested: true`. Meanwhile, it cannot be used to send (`template_not_sendable`) or edited. When the provider confirms, the template is removed and `template.deleted` is produced.
* **If the deletion does not finish**, the template goes back to normal with `deletion_requested: false`, and `deletion_failure_detail` carries the reason, when the provider gave one. Routa tries a few times before giving up.

Under the provider's rules, an approved template that was deleted **cannot be created again with the same name for 30 days**. A `disabled` template deleted only in Routa can come back on the next sync, if the provider still has it.

Removal by sync (the template no longer exists on WhatsApp) and removal along with a channel produce no event. From then on, a send that cites the template responds `template_not_found`.

## Send a template

With the template `approved`, send it in `POST /v1/messages` with its `id` and the placeholder values, in order:

```json theme={null}
{
  "channel": "chan_01J8...",
  "to": "+5581999999999",
  "template": { "id": "tmpl_01J8...", "body_parameters": ["Ana", "1234"] }
}
```

Send `template` without `text`. The number of `body_parameters` must match the number of body variables.

A template can also ask for `header_parameter`, when the header is text with a variable or is media, and `button_parameters`, when a URL button has a variable. See [Header and buttons](https://docs.routa.chat/en/guides/send-template-message.md#header-and-buttons).

> [!WARNING]
>
> Routa checks the template and the parameters **at accept time**. If the template is not approved, the call fails right away with `template_not_approved` (or `template_paused`) instead of producing a `message.failed` later. A read-only template, or one being deleted, fails with `template_not_sendable`. A missing or extra parameter fails with `content_rejected`.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.