# Send a WhatsApp Template

> Send an approved WhatsApp template to one phone number from your own system, with values for its variables

URL: https://docs.lemuai.com/api-reference/whatsapp/send-template
For: Developers
Checked against LemuAI app version 1.0.53
Related: [Get a WhatsApp Message](https://docs.lemuai.com/api-reference/whatsapp/get-message), [Numbers](https://docs.lemuai.com/whatsapp/numbers), [WhatsApp Messaging](https://docs.lemuai.com/messaging/whatsapp), [Errors](https://docs.lemuai.com/api-reference/errors), [WhatsApp and Inbox](https://docs.lemuai.com/troubleshooting/whatsapp)

Sends one approved WhatsApp template to one phone number from your own system, for example an order update, a payment reminder or a login code. You say which template to send, the number to send it to, and the values for the template's variables. It works like the "send template message" API of WATI, Interakt or AiSensy.

Any approved template can be sent: marketing, utility and authentication. To send to a list of people at once, use a broadcast or a journey in the dashboard instead.

**Info:** **Setup required.** Before you can send, your workspace needs:

- WhatsApp connected under **WhatsApp → Setup**, with at least one number under **WhatsApp → Numbers**.
- An approved template under **WhatsApp → Templates**.
- Money in your **WhatsApp balance**. Recharge under **Usage & Plan → Usage & balances**.
- An API key from **Settings → API Keys**. See [Authentication](https://docs.lemuai.com/api-reference/authentication).

**Endpoint:** `POST /api/v1/whatsapp/messages/template`

## Headers

| Name | Required | Description |
|---|---|---|
| `X-API-Key` | Yes | An API key from your workspace. See [Authentication](https://docs.lemuai.com/api-reference/authentication). |
| `Content-Type` | Yes | `application/json` |

## Request body

Name the template with `template_id`, or with `template_name` and `language`.

| Field | Type | Required | Description |
|---|---|---|---|
| `to` | string | Yes | The number to send to, with its country code: `+919876543210` or `919876543210`. Spaces and dashes are fine. A 10-digit number without a country code is refused. |
| `template_id` | integer | One of `template_id` or `template_name` | LemuAI's id for the template, a number such as `44`. Open **WhatsApp → Templates** and click the template: its side panel shows **Template ID** with a copy button. It is not the **Meta template ID** shown next to it. |
| `template_name` | string | One of `template_id` or `template_name` | The template's name exactly as shown under **WhatsApp → Templates**, for example `order_shipped`. |
| `language` | string | With `template_name`, when the template has more than one language | The template's language code, for example `en`, `en_US` or `hi`. `en` also finds `en_US` when that is the template's only English version. Ignored with `template_id`, which already points at one language. |
| `variables` | object | When the template has variables | The values for the template's variables, as text or numbers. See [Variables](#variables). Up to 100 entries. |
| `header_text` | string | When the template's text header has a variable | The value for the header's variable, up to 60 characters. Templates with named variables can give it in `variables` instead. |
| `header_media_url` | string | When the template's header is an image, video or document | A public `https://` link to the file. WhatsApp downloads it when it sends the message. |
| `header_filename` | string | No | The file name WhatsApp shows for a document header, for example `Invoice-10422.pdf`. Default: the file name the link ends in, when it ends in one (`…/Invoice-10422.pdf`). |
| `button_url_value` | string | When a link button's address ends with a variable | The part that goes in place of the variable. For a button to `https://shop.example.com/track/{{1}}`, send `SO-10422`. For a button made with **Count clicks on this button**, send the whole page address to open; see [Link Click Tracking](https://docs.lemuai.com/messaging/link-tracking#template-link-buttons). |
| `coupon_code` | string | When the template has a copy-code button | The code the button copies, up to 15 characters. |
| `product_retailer_ids` | array of strings | For a multi-product template | The content IDs (SKUs) of the products to show, from your WhatsApp catalog. Up to 30. |
| `thumbnail_product_retailer_id` | string | No | For a catalog template: the content ID (SKU) of the product whose photo shows on the message. |
| `from` | string | No | Which of your WhatsApp numbers sends the message, as shown under **WhatsApp → Numbers** (for example `+91 98111 12222`). Default: your default number. |
| `reference` | string | No | Your own id for this message, up to 100 characters, for example your order id. It comes back in the response and in [Get a WhatsApp Message](https://docs.lemuai.com/api-reference/whatsapp/get-message). |

A field that isn't in this table is refused with `422`, so a typo in a field name doesn't go unnoticed.

## Variables

`variables` holds one value for each variable in the template's header and body. The keys depend on how the template was written:

| The template's variables look like | Send |
|---|---|
| `{{1}}`, `{{2}}` (positional) | `{"1": "Priya", "2": "SO-10422"}` |
| `{{first_name}}`, `{{order_id}}` (named) | `{"first_name": "Priya", "order_id": "SO-10422"}` |
| An authentication template (login or verification code) | The code: `{"1": "482913"}`, up to 15 characters. It fills the message and the template's copy-code or autofill button. |

- Every variable needs a value. If one is missing, nothing is sent and the error lists the keys to send.
- `"1"` and `"{{1}}"` both work as keys. Numbers are sent as text: `2499` becomes `2499`.
- WhatsApp doesn't allow a new line, a tab or more than four spaces in a row inside a value. Such a value is refused before sending.
- A text header with a variable takes its value from `header_text`. In a template with positional variables, the header's `{{1}}` is separate from the body's `{{1}}`.
- Keys the template doesn't use are ignored.

**Example request:** the template `order_shipped` has the header "Order `{{1}}`", the body "Hi `{{1}}`, your order `{{2}}` is on its way." and a **Track** button that opens `https://shop.example.com/track/{{1}}`.

**cURL**

```bash
curl -X POST https://app.lemuai.com/api/v1/whatsapp/messages/template \
  -H "X-API-Key: lemu_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
    "to": "+919876543210",
    "template_id": 44,
    "variables": { "1": "Priya", "2": "SO-10422" },
    "header_text": "SO-10422",
    "button_url_value": "SO-10422",
    "reference": "order-10422"
  }'
```

**JavaScript**

```js
const res = await fetch("https://app.lemuai.com/api/v1/whatsapp/messages/template", {
  method: "POST",
  headers: {
    "X-API-Key": process.env.LEMU_API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    to: "+919876543210",
    template_id: 44,
    variables: { 1: "Priya", 2: "SO-10422" },
    header_text: "SO-10422",
    button_url_value: "SO-10422",
    reference: "order-10422",
  }),
});
if (!res.ok) throw new Error(`LemuAI API ${res.status}: ${await res.text()}`);
const data = await res.json();
console.log(data.message_id);
```

**Python**

```python
import os

import requests

response = requests.post(
    "https://app.lemuai.com/api/v1/whatsapp/messages/template",
    headers={
        "X-API-Key": os.environ["LEMU_API_KEY"],
    },
    json={
        "to": "+919876543210",
        "template_id": 44,
        "variables": {"1": "Priya", "2": "SO-10422"},
        "header_text": "SO-10422",
        "button_url_value": "SO-10422",
        "reference": "order-10422",
    },
    timeout=30,
)
response.raise_for_status()
print(response.json()["message_id"])
```

More examples of the body:
```json
{
  "to": "919876543210",
  "template_name": "plan_renewal",
  "language": "en",
  "variables": { "first_name": "Priya", "plan": "Growth" }
}
```
```json
{
  "to": "+919876543210",
  "template_name": "invoice_ready",
  "language": "en",
  "variables": { "1": "INV-10422" },
  "header_media_url": "https://files.example.com/invoices/INV-10422.pdf",
  "header_filename": "Invoice-10422.pdf",
  "from": "+91 98111 13333"
}
```
```json
{
  "to": "+919876543210",
  "template_name": "login_code",
  "language": "en",
  "variables": { "1": "482913" }
}
```

## Response

**`200`**: WhatsApp accepted the message.
```json
{
  "message_id": 90311,
  "provider_message_id": "wamid.HBgMOTE5ODc2NTQzMjEwFQIAERgSNkQ5QjE0RjU2QzM0RTI3QjdBAA==",
  "status": "sent",
  "to": "+919876543210",
  "contact_id": 5821,
  "template": {
    "id": 44,
    "name": "order_shipped",
    "language": "en",
    "category": "utility"
  },
  "reference": "order-10422"
}
```

| Field | Type | Description |
|---|---|---|
| `message_id` | integer | The message's id in LemuAI. Pass it to [Get a WhatsApp Message](https://docs.lemuai.com/api-reference/whatsapp/get-message) to see whether it was delivered and read. |
| `provider_message_id` | string | WhatsApp's id for the message (`wamid…`). Get a WhatsApp Message accepts it too. |
| `status` | string | Always `sent` here: WhatsApp accepted the message. Delivered, read or failed comes later. |
| `to` | string | The number it was sent to, in `+` and digits form. |
| `contact_id` | integer | The contact the message belongs to. A contact is created when no contact has this number yet. |
| `template` | object | The template that was sent: `id`, `name`, `language` and `category` (`marketing`, `utility` or `authentication`). |
| `reference` | string or null | The `reference` you sent. |

## What happens with the message

- **Cost:** each message is paid from your WhatsApp balance at WhatsApp's rate for the template's category and the recipient's country. A message WhatsApp refuses is refunded.
- **Inbox and contact:** the message shows in the contact's conversation in the [Inbox](https://docs.lemuai.com/messaging/inbox), labelled **API**, and in the contact's messages. When the person replies, the reply lands in the same conversation and opens WhatsApp's 24-hour window, like any other reply.
- **Delivery:** `sent` means WhatsApp accepted it. Whether it was delivered or read, or why it failed, shows a few seconds later in [Get a WhatsApp Message](https://docs.lemuai.com/api-reference/whatsapp/get-message), in the Inbox and in [Contact Messages](https://docs.lemuai.com/api-reference/contacts/messages).
- **Opt-out:** a contact who opted out of all messages is never sent anything, and a marketing template isn't sent to a contact who stopped WhatsApp marketing (STOP, Stop promotions or Stop marketing in WhatsApp). Utility and authentication templates still go. The request returns `422` with `opted_out`.
- **Sending limits:** the per-channel limits under **Settings → Sending Limits** apply to journeys and campaigns, not to this API. Your system decides how often a person gets a message.
- **Marketing templates:** WhatsApp limits how many marketing messages one person gets from all businesses. When it holds one back, the message's status becomes `failed` with WhatsApp's reason.

## Errors

Nothing is charged or sent when the request has a problem. The body is:
```json
{
  "detail": {
    "error": "missing_values",
    "message": "Missing a value for {{2}}. Send variables: {\"2\": \"...\"}.",
    "missing": ["2"],
    "variables": ["1", "2"]
  }
}
```

`error` is a fixed word your code can check. `message` is a sentence that says what to change. `missing_values` also lists the `missing` keys and every key the template takes in `variables`.

| Status | `detail.error` | When |
|---|---|---|
| `400` | `whatsapp_not_ready` | WhatsApp isn't connected, setup isn't finished, payment setup with Meta is still pending, Meta restricted the account, or marketing messages are paused on the sending number. `detail.message` says which. |
| `400` | `template_not_approved` | The template is pending, rejected, paused or disabled. `detail.template_status` holds its status. |
| `401` | | The API key is missing or not valid. |
| `402` | `balance_too_low` | The WhatsApp balance is too low. Recharge under **Usage & Plan → Usage & balances**. |
| `404` | `template_not_found` | No template with this `template_id` in your workspace, no template with this name on the sending number's WhatsApp account, or no version in this `language` (`detail.languages` lists the template's languages). |
| `422` | `template_required` | Neither `template_id` nor `template_name` was sent. |
| `422` | `language_required` | `template_name` matches the template in more than one language. Send `language`. |
| `422` | `missing_values` | A variable, the header value, `header_media_url`, `button_url_value`, `coupon_code` or `product_retailer_ids` is missing. |
| `422` | `invalid_values` | A value has a new line, a tab or more than four spaces in a row, is too long, or isn't text or a number, or a click-counting button got something other than a full `https://` address. |
| `422` | `invalid_number` | `to` isn't a phone number, or has no country code. |
| `422` | `unknown_from_number` | `from` isn't one of your numbers under **WhatsApp → Numbers**. |
| `422` | `opted_out` | The contact opted out of all messages, or stopped WhatsApp marketing and this is a marketing template. |
| `422` | | A field is missing, has the wrong type, or isn't a field of this endpoint. `detail` is then a list of the fields to fix. |
| `502` | `send_failed` | WhatsApp refused the message when it was sent. `detail.message` holds WhatsApp's reason. It shows as failed in the Inbox and is refunded. A number without WhatsApp is usually accepted first and then turns `failed` in [Get a WhatsApp Message](https://docs.lemuai.com/api-reference/whatsapp/get-message). |

See [Errors](https://docs.lemuai.com/api-reference/errors) for the general error format.
