# Send messages

> `phoneNumberId` is the Whitelabel WhatsApp Cloud API id from [Phone numbers](/whatsapp/concepts/phone-numbers.md). Meta’s id is `waPhoneNumberId`.

Scope for send: `messages:send`. Scope for template create: `templates:write`.

## 24-hour window

An inbound message sets `conversation.windowExpiresAt` to 24 hours after the server records it.

| `type` | Window closed | Window open |
| --- | --- | --- |
| `template` | Accepted by this API. Meta must still accept the template. | Accepted |
| Anything else (`text`, `image`, …) | `400` `VALIDATION_ERROR`. Message: `Customer service window is closed. Send an approved template message to re-open the conversation.` `details` is omitted. | Accepted |

The check is the conversation for that phone and recipient. A template does not open the window. The recipient’s reply does.

```http
GET /api/v1/conversations/window?phoneNumberId=PHONE_ID&to=5215512345678
Authorization: Bearer $VITRAL_WHATSAPP_API_KEY
```

Scope `conversations:read`.

```json
{
  "data": {
    "conversationId": null,
    "contactId": null,
    "waId": "5215512345678",
    "windowOpen": false,
    "windowExpiresAt": null,
    "lastInboundCustomerMsgAt": null
  }
}
```

`windowOpen: false` means a non-template send is rejected. `conversationId` is null when this recipient has no conversation on that phone. A phone on another tenant is `403`. An unknown phone id is `404`.

## Human takeover

A reply from the WhatsApp Business app pauses automatic replies for that conversation. Whitelabel WhatsApp Cloud API stores the pause and sends [conversation.human_takeover](/whatsapp/webhooks/events.md#conversationhuman_takeover). It does not reject `POST /messages` while the pause is active. Your bot has to skip its own sends.

`GET /api/v1/conversations` and `GET /api/v1/conversations/:id` (scope `conversations:read`) add these fields. List responses include them on every row.

| Field | Meaning |
| --- | --- |
| `lastBusinessAppReplyAt` | Meta timestamp of the last app reply that started or extended the pause. A late echo, a revoke, or a number with the pause disabled does not move it. |
| `humanActiveUntil` | When the pause ends. Null after resume. |
| `botPaused` | True when `humanActiveUntil` is still in the future. |
| `botResumedAt` | When resume was last called. A later app reply starts a new pause and leaves this set. |

```http
POST /api/v1/conversations/CONVERSATION_ID/bot-resume
Authorization: Bearer $VITRAL_WHATSAPP_API_KEY
```

Scope `conversations:read` for now. Clears a pause that is still in the future: sets `humanActiveUntil` to null and `botResumedAt` to now, and sends [conversation.bot_resumed](/whatsapp/webhooks/events.md#conversationbot_resumed) once (`contactId` and `waId` included). Calling it again, or calling it after the pause has expired, returns the same row and does not send the event again. Another tenant's id is `404`.

The length is the phone's `botPauseMinutesOnEcho`. Null uses the deployment default, 30 minutes. `0` turns the pause off for that number. Set it with `PATCH /api/v1/phone-numbers/:id` (scope `phone_numbers:write`):

```json
{ "botPauseMinutesOnEcho": 120 }
```

Send `null` to use the deployment default again. Values outside 0–1440 are `400`.

An echo that reached Whitelabel WhatsApp Cloud API more than 10 minutes after its Meta timestamp does not pause. Time in the processing queue does not count. A deleted message (`revoke`) does not pause. An edit does. Imported history does not pause. The pause is computed from the echo's Meta timestamp. A retry still pauses when the message was stored and the pause was not. After `conversation.human_takeover` was queued, another delivery of that message does not extend the pause or send the event again. An older echo does not shorten a pause a newer echo already set. An echo at or before `botResumedAt` does not pause.

## Send a template

```http
POST /api/v1/messages
Authorization: Bearer $VITRAL_WHATSAPP_API_KEY
Content-Type: application/json

{
  "phoneNumberId": "PHONE_ID",
  "to": "5215512345678",
  "type": "template",
  "template": {
    "name": "recordatorio_cita",
    "language": { "code": "es" },
    "components": [
      {
        "type": "body",
        "parameters": [
          { "type": "text", "text": "Ana" },
          { "type": "text", "text": "2 de octubre" },
          { "type": "text", "text": "10:30" }
        ]
      }
    ]
  }
}
```

`to` is stored as digits when it is a phone. A leading `+` is removed. Spaces, dashes, and parentheses are stripped before that. Any other string is a business-scoped user id.

`201` is the stored message. Fields you branch on:

```json
{
  "data": {
    "id": "ck_message",
    "phoneNumberId": "PHONE_ID",
    "direction": "OUTBOUND",
    "type": "TEMPLATE",
    "status": "PENDING"
  }
}
```

The row also has `body`, `rawRequest`, timestamps, and billing fields. `wamId` is filled after Meta accepts the send. The queue job id is `msg.{messageId}` with 3 attempts.

If a local template row exists for that name and language on another WABA and not on this phone’s WABA, the API returns `400` `VALIDATION_ERROR` (`Template is not on this phone number's WhatsApp account`) and does not send. If no local row exists, the API still enqueues and Meta accepts or rejects. The same template name on two WABAs does not block a send to the WABA that has it.

`POST /api/v1/messages/batch` takes `phoneNumberId` and `messages` (1–100). Each item is a send body without `phoneNumberId`. The server loops send and returns `201` `{ "data": [ ... ] }`. A failure stops the loop. Earlier messages stay queued.

## Idempotency

Send `Idempotency-Key` (1–200 printable ASCII characters, no spaces) to make a retry safe:

```http
POST /api/v1/messages
Idempotency-Key: reminder-ck_appt_42
```

- Same key, same body: the first message is returned (`201`, header `Idempotent-Replayed: true`) and nothing is sent again. Two concurrent requests with one key produce one message.
- Same key, different body or phone: `409` `IDEMPOTENCY_KEY_REUSED`, with `details.messageId`.
- On `/messages/batch` the key applies per item as `<key>:<index>`. Retrying a batch that stopped halfway with the same key replays the items already queued and sends only the rest.
- Keys are unique per tenant and do not expire.
- Without the header nothing changes: a retried `201` creates another message.

## Reply inside the window

```http
POST /api/v1/messages
Authorization: Bearer $VITRAL_WHATSAPP_API_KEY
Content-Type: application/json

{
  "phoneNumberId": "PHONE_ID",
  "to": "5215512345678",
  "type": "text",
  "text": { "body": "Su cita quedó confirmada para el 2 de octubre a las 10:30." }
}
```

The stored `type` is `TEXT`.

## Mark read and reactions

`POST /api/v1/messages/:id/mark-read` returns the message (`200`). `POST /api/v1/messages/:id/react` with `{ "emoji": "👍" }` returns `201`. Both require `messages:send` and a stored `wamId`. Without a `wamId` the error is `400` `VALIDATION_ERROR` (`Message has no WAM ID (not yet sent)`). Both call Graph in the request, unlike send.

## Status

`PENDING` → `SENT` → `DELIVERED` → `READ`, or `FAILED`.

`GET /api/v1/messages/:id` (`messages:read`) or the `message.status` webhook. On failure, `errorCode` is `META_<number>`, `PHONE_DISCONNECTED`, `SEND_FAILED`, or `HTTP_<status>`. See [Errors](/whatsapp/api/errors.md).

List: `GET /api/v1/messages?phoneNumberId=&direction=OUTBOUND&status=FAILED&page=1&limit=50`. `direction` is `INBOUND` or `OUTBOUND`. `limit` default 50, max 100.
