# Inviare un messaggio di testo

`POST /v1/messages`

POST /v1/messages con `type: "text"`. Restituisce **202** (accettato, NON inviato): entra in coda e viene smaltito a ritmo controllato. L'header `Idempotency-Key` (UUID) è obbligatorio — riprovare con la stessa key restituisce lo stesso record (200) invece di duplicarlo. Identifica il numero mittente con `connectionId` (conn_…) o `from` (phoneNumberId).

## Esempi

### cURL

```bash
curl -X POST https://api.waiaconnect.com/v1/messages \
  -H "Authorization: Bearer wc_live_YOUR_API_KEY" \
  -H "Idempotency-Key: REPLACE-WITH-A-UNIQUE-ID" \
  -H "Content-Type: application/json" \
  -d '{"connectionId":"conn_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","to":"5493511234567","type":"text","text":{"body":"Hello from WAIA Connect"}}'
```

### 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"
  },
  body: JSON.stringify({
  "connectionId": "conn_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
  "to": "5493511234567",
  "type": "text",
  "text": {
    "body": "Hello from WAIA Connect"
  }
})
});
console.log(await res.json()); // { id: "msg_…", status: "queued" }
```

### 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",
  ],
  CURLOPT_POSTFIELDS => json_encode(["connectionId" => "conn_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","to" => "5493511234567","type" => "text","text" => ["body" => "Hello from WAIA Connect"]]),
]);
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()),
  },
  json={"connectionId":"conn_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX","to":"5493511234567","type":"text","text":{"body":"Hello from WAIA Connect"}})
print(res.json())  # {"id": "msg_…", "status": "queued"}
```

## Note

- **Da dove vengono questi due:** il `conn_…` è il campo `id` di `GET /v1/connections` (e puoi copiarlo dalla scheda del numero, nel pannello); il `phoneNumberId` è il campo `phoneNumberId` della stessa risposta. Uno dei due è obbligatorio, anche se hai un solo numero (se non ne mandi nessuno: `CONNECTION_REQUIRED`).
- `to` va in formato E.164 (solo cifre, 8–15). Nell'esempio c'è il tuo numero, così puoi provare scrivendo a te stesso.
- **Anteprima di un link:** aggiungi `preview_url: true` dentro `text` e WhatsApp prova a mostrare la scheda del **primo** link del testo. La costruisce **Meta** con i metadati di quella pagina: se non li ha, il messaggio parte comunque senza anteprima — non è un errore. È facoltativo; senza il campo, tutto funziona come sempre.
- _Risposta: `{ id: "msg_…", status: "queued" }` (oppure `held` con un `reason`). Lo stato si segue con GET /v1/messages/:id._
