# Send a template

`POST /v1/messages`

Templates are **sent by name**: POST /v1/messages with `type: "template"` and `template: { name, language, components }`. The template must be **approved in Meta**; `components` carries its variables. If it is yours and not approved we return `422 TEMPLATE_NOT_APPROVED` **before** calling Meta; if we haven't synced it yet, we let it through.

## Examples

### cURL

```bash
curl -X POST https://api.waiaconnect.com/v1/messages \
  -H "Authorization: Bearer wc_live_YOUR_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{"connectionId":"conn_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","to":"5493511234567","type":"template","template":{"name":"hello_world","language":"es","components":[]}}'
# The template must already be APPROVED in Meta. `components` carries its variables.
```

### Node.js

```javascript
const res = await fetch("https://api.waiaconnect.com/v1/messages", {
  method: "POST",
  headers: {
    "Authorization": "Bearer wc_live_YOUR_API_KEY",
    "Idempotency-Key": crypto.randomUUID(),
    "Content-Type": "application/json"
  },
  // The template must already be APPROVED in Meta; `components` carries its variables.
  body: JSON.stringify({
  "connectionId": "conn_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  "to": "5493511234567",
  "type": "template",
  "template": {
    "name": "hello_world",
    "language": "es",
    "components": []
  }
})
});
console.log(await res.json());
```

### PHP

```php
<?php
$ch = curl_init("https://api.waiaconnect.com/v1/messages");
curl_setopt_array($ch, [
  CURLOPT_POST => true,
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_HTTPHEADER => [
    "Authorization: Bearer wc_live_YOUR_API_KEY",
    "Idempotency-Key: " . bin2hex(random_bytes(16)),
    "Content-Type: application/json",
  ],
  // The template must already be APPROVED in Meta.
  CURLOPT_POSTFIELDS => json_encode(["connectionId" => "conn_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","to" => "5493511234567","type" => "template","template" => ["name" => "hello_world","language" => "es","components" => []]]),
]);
echo curl_exec($ch);
```

### Python

```python
import requests, uuid
res = requests.post("https://api.waiaconnect.com/v1/messages",
  headers={
    "Authorization": "Bearer wc_live_YOUR_API_KEY",
    "Idempotency-Key": str(uuid.uuid4()),
  },
  # The template must already be APPROVED in Meta; "components" carries its variables.
  json={"connectionId":"conn_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","to":"5493511234567","type":"template","template":{"name":"hello_world","language":"es","components":[]}})
print(res.json())
```

## Notes

- **Where the `name` comes from:** `GET /v1/templates` (each row's `name` + `language`), or the Templates section of the panel. It is the name in Meta, not the title you typed on screen, and it only works if that row is `APPROVED`.
- Outside the 24h service window, Meta requires an approved template (error `131047` if you send free text).
- The **24-hour window**: you can only write freely within 24h of the customer's last message. After that you need an approved template. That's why templates exist.
