> ## 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.

# Send a template

> Create a template with its variable examples, wait for approval and send it with the body, header and button parameters.

Use a template to send a message approved by WhatsApp, with variable values you fill in. To understand the approval cycle, see [Templates](https://docs.routa.chat/en/concepts/templates.md).

> [!NOTE]
>
> Required scopes: `templates:write` to create, `templates:read` to retrieve and `messages:write` to send. For a media header, you also need `media:write`.

**1. Create the template**

Send one example for each variable in `variable_examples`. The first item is the example of `{{1}}`.

```bash cURL 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"]
  }'
```

```ts Node.js theme={null}
const template = await routa.templates.submit({
  channel: 'chan_01J8...',
  name: 'order_confirmation',
  language: 'en_US',
  category: 'utility',
  bodyText: 'Hi {{1}}! Your order {{2}} is confirmed.',
  variableExamples: ['Ana', '1234'],
})

console.log(template.id, template.status)
```

> [!WARNING]
>
> Use made-up values in the examples, never real personal data or a real token. They are stored with the template, returned by the API and sent in events and webhooks.

The response carries the initial `status` and `variables`, with the `index` and the `example` of each body variable. A text header or a URL button with a variable also requires its own `example`. Without the example of a variable, the API responds `422 template_variables_invalid`. See [Variable examples](https://docs.routa.chat/en/concepts/templates.md#variable-examples).

`language` is the code WhatsApp uses, such as `en_US` or `pt_BR`, with an underscore and not a hyphen.

**2. Wait for approval**

The provider decides in its own time. Follow it in one of two ways:

* **Webhook:** subscribe to `template.*` and wait for `template.approved`, `template.rejected` or `template.failed`.
* **Querying:** call `GET /v1/templates/{id}` and check `status`.

```bash theme={null}
curl https://api.routa.chat/v1/templates/tmpl_01J8... \
  -H "Authorization: Bearer $ROUTA_API_KEY"
```

* If the template is **rejected** (`rejected`), `rejection_reason` gives the reason. Fix it and create another template with a different name, or edit the template in the dashboard.
* If **sending it for approval fails** (`failed`), Routa could not send the template to the provider. `failure_code` gives the reason, and `failure_detail` carries the provider's sentence, when there is one. Through the API, you do not resend it: create the template again. In the dashboard, you can edit it and send it again. See [When sending for approval fails](https://docs.routa.chat/en/concepts/templates.md#when-sending-for-approval-fails).

**3. Send with parameters**

With the template `approved`, pass `template.id` and the `body_parameters` in the order of the placeholders.

```bash cURL theme={null}
curl https://api.routa.chat/v1/messages \
  -H "Authorization: Bearer $ROUTA_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{
    "channel": "chan_01J8...",
    "to": "+5581999999999",
    "template": {
      "id": "tmpl_01J8...",
      "body_parameters": ["Ana", "1234"]
    }
  }'
```

```ts Node.js theme={null}
await routa.messages.send({
  channel: 'chan_01J8...',
  to: '+5581999999999',
  template: { id: 'tmpl_01J8...', bodyParameters: ['Ana', '1234'] },
})
```

If the header or a button of the template has a variable, or if the header is media, also send the fields in [Header and buttons](#header-and-buttons).

## Header and buttons

A send carries **exactly** what the template asks for, on every send:

| The template has | Send in `template` |
| - | - |
| Variables in the body | `body_parameters`: one value per variable, the first one for `{{1}}`. |
| Text header with `{{1}}` | `header_parameter.text`. |
| Image, video or document header | `header_parameter.media_id`. On a document, also `header_parameter.filename`, if you want. |
| URL buttons with `{{1}}` | `button_parameters`: one `{ "index", "text" }` item for each of those buttons. |
| Fixed text, footer, quick reply, phone button or fixed URL | Nothing. |

To find out what a template asks for, retrieve it with [`GET /v1/templates/{id}`](https://docs.routa.chat/en/api-reference/endpoint/templates/retrieve) and read `variables` and `components`.

### Text in the header and values in the buttons

In this example, the header is `Order {{1}}`, the body is `Hi {{1}}, invoice {{2}} is here.`, the first button is a quick reply and the second one opens `https://example.com/invoices/{{1}}`.

```bash cURL theme={null}
curl https://api.routa.chat/v1/messages \
  -H "Authorization: Bearer $ROUTA_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{
    "channel": "chan_01J8...",
    "to": "+5581999999999",
    "template": {
      "id": "tmpl_01J8...",
      "body_parameters": ["Ana", "1234"],
      "header_parameter": { "text": "A-10" },
      "button_parameters": [{ "index": 1, "text": "1234" }]
    }
  }'
```

```ts Node.js theme={null}
await routa.messages.send({
  channel: 'chan_01J8...',
  to: '+5581999999999',
  template: {
    id: 'tmpl_01J8...',
    bodyParameters: ['Ana', '1234'],
    headerParameter: { text: 'A-10' },
    buttonParameters: [{ index: 1, text: '1234' }],
  },
})
```

* `index` is the position of the button in `components.buttons`, starting at 0. **Every** button counts, of any kind. In the example, the quick reply is button `0`, so the URL button is `1`.
* `text` replaces only the `{{1}}` of the URL. Do not send the whole URL.
* Routa appends `text` to the URL exactly as it arrives. Percent-encode it first, only once: a space becomes `%20`, and `é` becomes `%C3%A9`. In Node.js, `encodeURIComponent()` works for a path segment or a query value. The SDK does not encode for you.

### File in the header

For a media header, upload the file with [`POST /v1/media`](https://docs.routa.chat/en/api-reference/endpoint/media/upload) and use its `id` (`med_...`) in `header_parameter.media_id`. You name the file on **every** send. The sample used to create the template (`tmedia_...`, from `POST /v1/templates/media`) is not reused.

```bash cURL theme={null}
# 1. Upload the file and keep the id from the response
curl https://api.routa.chat/v1/media \
  -H "Authorization: Bearer $ROUTA_API_KEY" \
  -H "Content-Type: application/pdf" \
  --data-binary @invoice.pdf

# 2. Send the template with the file
curl https://api.routa.chat/v1/messages \
  -H "Authorization: Bearer $ROUTA_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: $(uuidgen)" \
  -d '{
    "channel": "chan_01J8...",
    "to": "+5581999999999",
    "template": {
      "id": "tmpl_01J8...",
      "header_parameter": { "media_id": "med_01J8...", "filename": "invoice.pdf" }
    }
  }'
```

```ts Node.js theme={null}
import { readFile } from 'node:fs/promises'

// 1. Upload the file
const file = await routa.media.upload({
  file: await readFile('./invoice.pdf'),
  contentType: 'application/pdf',
})

// 2. Send the template with the file
await routa.messages.send({
  channel: 'chan_01J8...',
  to: '+5581999999999',
  template: {
    id: 'tmpl_01J8...',
    headerParameter: { mediaId: file.id, filename: 'invoice.pdf' },
  },
})
```

The file type must match the header format:

| Header format | Accepted types |
| - | - |
| `image` | `image/jpeg`, `image/png` |
| `video` | `video/mp4` |
| `document` | `application/pdf` |

* The file must belong to this project and be `ready`.
* A file **received** from a contact is refused. Upload it again with `POST /v1/media` and use the new `id`.
* `filename` is the name the recipient sees. It only applies to a document header.
* If the file becomes unavailable before sending, for example because [media retention](https://docs.routa.chat/en/concepts/media.md#retention) expired, the message ends up `failed` with `failure_code: media_unavailable`.

### Refused values

The API refuses at accept time, with `422 content_rejected`:

* A missing or extra parameter, a repeated `index`, or an item for a button that has no variable.
* `text` and `media_id` together, `media_id` on a text header, `text` on a media header, or `filename` outside a document header.
* An empty or blank header `text`, `media_id` or `filename`, or button `text`.
* A `body_parameters` value or header text with a line break, a tab or more than 4 spaces in a row.
* A button value with any character outside visible ASCII, such as a space, a line break, an accented letter or an emoji.

`message` says which field failed and never repeats the value. A refused request stores no message and uses none of your allowance.

> [!NOTE]
>
> The values you send are kept in the message `content` and travel in events and webhooks, `filename` included.

## Validation rules

Routa validates the template at accept time, so these errors come back right away:

| Code | Status | Cause |
| - | - | - |
| `template_not_found` | `404` | The template does not exist in this project. |
| `template_not_approved` | `422` | The template is not approved yet, or was rejected or disabled. |
| `template_paused` | `422` | The provider paused the template. |
| `template_not_sendable` | `422` | The template is read-only (imported from WhatsApp) or is being deleted. Use another template. |
| `content_rejected` | `422` | A body, header or button parameter is missing, extra, mismatched or has a [refused value](#refused-values). |
| `media_not_found` | `404` | `header_parameter.media_id` is not a file of this project. |
| `media_not_ready` | `422` | The header file is not available. Upload it again. |
| `media_content_type_rejected` | `422` | The file type does not match the header format. |
| `request_invalid` | `422` | A field is missing or has the wrong type. `error.param` says which one, such as `template.button_parameters.0.index`. |

When you **create** the template, Routa also refuses right away a name, a body, a header or buttons WhatsApp would reject, and a variable with no example (`template_name_invalid`, `template_variables_invalid` and `template_components_invalid`). See [Validation rules at accept time](https://docs.routa.chat/en/concepts/templates.md#validation-rules-at-accept-time).

> [!NOTE]
>
> In the SDK, `variableExamples`, `example`, `headerParameter` and `buttonParameters` exist from version 0.5.0. An earlier version does not send the examples, and the API refuses the `submit()` of a template with a variable. See [Upgrade the SDK](https://docs.routa.chat/en/sdk/upgrading.md). `components` and `templates.uploadMedia()` exist since 0.3.0. See [Templates in the SDK](https://docs.routa.chat/en/sdk/templates.md) and [Rich components](https://docs.routa.chat/en/concepts/templates.md#rich-components).


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