# Enviar mensaje de WhatsApp

POST `https://api.bydomu.com/v1/conversations/{contactId}/messages`

Envía un mensaje al contacto respetando la ventana de servicio de 24 h de Meta.

- **Ventana abierta:** manda `body` y se entrega como texto libre (la respuesta es el mensaje guardado).
- **Ventana cerrada:** manda `templateId` + `variables` con una plantilla APROBADA (la respuesta trae `waMessageId`, `renderedBody` y `usage`).
- **Ninguna de las dos es posible** (opt-out, sin plantilla, sin ventana): `409 CONFLICT` con el motivo en `error.message`, el modo en `error.reason` (`FREE_FORM`, `TEMPLATE_ONLY`, `HUMAN_AGENT_ONLY`, `BLOCKED`) y, si falta la plantilla, `error.windowExpiresAt`.

La API nunca envía como `human_agent`: esa vía es de una persona real en el buzón. Si la organización no tiene un número de WhatsApp activo, responde `404`.

Permiso que pide

`messages:write`

Encabezado obligatorio

`X-API-Key` · clave secreta

Nivel de acceso

Nivel 3 · con revisión de soporte

## Parámetros de ruta

1 · obligatorios

`contactId` string · **obligatorio**

ID del contacto

## Encabezados

1 · todos opcionales

`Idempotency-Key` string · opcional

Clave única de la operación (8-128 caracteres de `[a-zA-Z0-9._:-]`). Un reintento con la misma clave y el mismo cuerpo devuelve la respuesta original sin repetir la operación. Vive 24 horas. 8–128 chars

## Cuerpo (JSON)

3 · todos opcionales

`body` string · opcional

Texto libre. Solo con la ventana de 24 h abierta. 1–4096 chars

`templateId` string · opcional

Plantilla aprobada, para la ventana cerrada.

`variables` object · opcional

Valores de las variables de la plantilla. por defecto {}

## Respuesta 201

`SentMessage` · 10 campos

**`data` Ventana abierta: el mensaje guardado.**

`id` string

`waMessageId` string

`channel` string WHATSAPP, INSTAGRAM, FACEBOOK, SMS, EMAIL, WEB

`direction` string INBOUND, OUTBOUND

`type` string TEXT, IMAGE, DOCUMENT, AUDIO, VIDEO, LOCATION

`body` string

`timestamp` string

`contactId` string

`agentId` string

**`agent` id · firstName · lastName**

`id` string

`firstName` string

`lastName` string

## Errores posibles

con la forma `{ "error": { "code", "message" } }`

- **400** Datos inválidos: el cuerpo o la query no cumplen el esquema, o `Idempotency-Key` está mal formada.
- **401** UNAUTHORIZED
- **402** PAYMENT_REQUIRED
- **403** FORBIDDEN
- **404** NOT_FOUND
- **409** DUPLICATE
- **429** RATE_LIMITED
- **500** INTERNAL_ERROR
- **502** WHATSAPP_ERROR

cURL:

```bash
curl -X POST "https://api.bydomu.com/v1/conversations/clx1con001/messages" \
  -H "X-API-Key: $BYDOMU_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "body": "Hola María, ¿te queda bien la visita del jueves a las 10?"
}'
```

JavaScript:

```js
import { ByDomu } from "@bydomu/sdk";

const bydomu = new ByDomu({
  apiKey: process.env.BYDOMU_API_KEY,
});

const { data } = await bydomu.conversations.sendMessage("clx1con001", {
  "body": "Hola María, ¿te queda bien la visita del jueves a las 10?"
});
```

Python:

```python
import os, requests

r = requests.post(
    "https://api.bydomu.com/v1/conversations/clx1con001/messages",
    headers={"X-API-Key": os.environ["BYDOMU_API_KEY"]},
    json={
        "body": "Hola María, ¿te queda bien la visita del jueves a las 10?"
    }
)
data = r.json()
```

**Respuesta · 201 · ejemplo**

```json
{
  "data": {
    "id": "clx1msg002",
    "waMessageId": "wamid.HBgMNTczMDAxMjM0NTY3FQIAERgSQzU",
    "channel": "WHATSAPP",
    "direction": "INBOUND",
    "type": "TEXT",
    "body": "Hola María, ¿te queda bien la visita del jueves a las 10?",
    "timestamp": "2026-09-25T10:35:00.000Z",
    "contactId": "clx1con001",
    "agentId": "clx1usr001",
    "agent": {
      "id": "clx1usr001",
      "firstName": "Carlos",
      "lastName": "Rodríguez"
    }
  }
}
```
