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

# Send a WhatsApp template

> Find an approved template, fill its variables and send it through the API.

Templates are the only messages WhatsApp accepts outside the 24-hour window. They are created and approved in the dashboard, see [WhatsApp templates](https://docs.shortext.ny-corp.io/channels/whatsapp-templates.md).

## 1. Find the template

`GET /templates?channel_id={whatsapp channel id}&status=APPROVED` lists the templates a number can send:

```json theme={null}
{
  "id": "t4Hd6sFe",
  "name": "order_update",
  "language": "fr",
  "status": "APPROVED",
  "category": "UTILITY",
  "channels": ["532867649916076"],
  "header": { "type": "NONE", "text": null, "variables": [], "media_url": null },
  "body": { "text": "Bonjour {{name}}, votre commande {{order}} est prête.", "variables": ["name", "order"] },
  "footer": null,
  "buttons": []
}
```

`body.variables` (and `header.variables` for a text header) are the values you must give.

## 2. Send it

Use `message.type` `template`, the template `name` and `language`, and the variables by name:

```bash cURL theme={null}
curl -X POST https://shortext.ny-corp.io/api/v2.0/conversations \
  -H "Authorization: Bearer $SHORTEXT_API_KEY" \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -d '{
    "channel": "whatsapp",
    "channel_id": "532867649916076",
    "contact": {
      "identifier": "237699999999",
      "profile": { "firstname": "Brice", "lastname": "Mbarga", "preferred_language": "fr" }
    },
    "message": {
      "type": "template",
      "content": {
        "template_name": "order_update",
        "language": "fr",
        "body": { "name": "Brice", "order": "A-1042" }
      }
    }
  }'
```

```php PHP theme={null}
<?php
$response = (new GuzzleHttp\Client())->post('https://shortext.ny-corp.io/api/v2.0/conversations', [
    'headers' => [
        'Authorization' => 'Bearer ' . getenv('SHORTEXT_API_KEY'),
        'Accept'        => 'application/json',
    ],
    'json' => [
        'channel'    => 'whatsapp',
        'channel_id' => '532867649916076',
        'contact'    => [
            'identifier' => '237699999999',
            'profile'    => ['firstname' => 'Brice', 'lastname' => 'Mbarga', 'preferred_language' => 'fr'],
        ],
        'message'    => [
            'type'    => 'template',
            'content' => [
                'template_name' => 'order_update',
                'language'      => 'fr',
                'body'          => ['name' => 'Brice', 'order' => 'A-1042'],
            ],
        ],
    ],
]);

$result = json_decode((string) $response->getBody(), true);
if (!$result['success']) {
    throw new RuntimeException("Shortext error {$result['code']}: {$result['message']}");
}
$conversationId = $result['body']['conversation']['id'];
```

```javascript Node.js theme={null}
const response = await fetch("https://shortext.ny-corp.io/api/v2.0/conversations", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.SHORTEXT_API_KEY}`,
    Accept: "application/json",
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    channel: "whatsapp",
    channel_id: "532867649916076",
    contact: {
      identifier: "237699999999",
      profile: { firstname: "Brice", lastname: "Mbarga", preferred_language: "fr" },
    },
    message: {
      type: "template",
      content: {
        template_name: "order_update",
        language: "fr",
        body: { name: "Brice", order: "A-1042" },
      },
    },
  }),
});

const result = await response.json();
if (!result.success) {
  throw new Error(`Shortext error ${result.code}: ${result.message}`);
}
const conversationId = result.body.conversation.id;
```

```python Python theme={null}
import os
import requests

response = requests.post(
    "https://shortext.ny-corp.io/api/v2.0/conversations",
    headers={
        "Authorization": f"Bearer {os.environ['SHORTEXT_API_KEY']}",
        "Accept": "application/json",
    },
    json={
        "channel": "whatsapp",
        "channel_id": "532867649916076",
        "contact": {
            "identifier": "237699999999",
            "profile": {"firstname": "Brice", "lastname": "Mbarga", "preferred_language": "fr"},
        },
        "message": {
            "type": "template",
            "content": {
                "template_name": "order_update",
                "language": "fr",
                "body": {"name": "Brice", "order": "A-1042"},
            },
        },
    },
)

result = response.json()
if not result["success"]:
    raise RuntimeError(f"Shortext error {result['code']}: {result['message']}")
conversation_id = result["body"]["conversation"]["id"]
```

To send a template in an existing conversation, post the same `message` to `POST /conversations/{id}/messages`.

### Media header

For a template with an image, video or document header, give its URL:

```json theme={null}
"header": { "url": "https://cdn.example.com/parcels/A-1042.jpg" }
```

Without `url`, the media stored with the template is used. A document also takes a `filename`.

## Errors

Shortext checks the template before sending it and answers `code` `1002` with a message you can act on:

| Message | Fix |
| - | - |
| `Template order_update not found for this WhatsApp number` | Check the name, and that the template belongs to the WhatsApp Business account of this number. |
| `Template order_update has no en version: fr` | Use one of the languages listed. |
| `Template order_update (fr) is PENDING: only approved templates can be sent` | Wait for Meta's approval. |
| `Missing body variables: order` | Give every variable; `body.missing` lists them. |
| `Template ... has a image header: give message.content.header.url` | Give the media URL. |


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