# Send Your First Message

Discover a channel and send text or template messages.

Connect a channel and create an API key with `channels:view`, `chats:view`, `chats:send_message`, and `templates:view`. Store it in the `QWETTY_API_KEY` environment variable. Replace example IDs with IDs returned by the API.

## 1. Discover a channel and conversation

```bash
curl https://api.qwetty.com/api/v1/channels \
  -H "Authorization: Bearer $QWETTY_API_KEY"

curl 'https://api.qwetty.com/api/v1/chats?limit=5' \
  -H "Authorization: Bearer $QWETTY_API_KEY"
```

Choose a conversation from `data`. The channel's capabilities determine which message types you can send. To open a conversation for an existing contact, use `POST /chats` with `channelId` and `contactId`, plus the appropriate platform identity when needed. This requires `chats:create` and a channel that supports addressing that identity.

## 2. Send a text message

```bash
curl -X POST https://api.qwetty.com/api/v1/chats/CHAT_ID/messages \
  -H "Authorization: Bearer $QWETTY_API_KEY" \
  -H 'Content-Type: application/json' \
  -H 'Idempotency-Key: order-123-confirmation' \
  -d '{"type":"TEXT","content":"Your order is confirmed."}'
```

A successful send returns `201` with the message in `data`. For a retry of this same send, keep the same idempotency key and body. Use a new key only for a new intended message. The integration name appears as the sender in conversation history.

## 3. Send a template

First discover templates available to your chosen channel:

```bash
curl 'https://api.qwetty.com/api/v1/templates?channelId=CHANNEL_ID' \
  -H "Authorization: Bearer $QWETTY_API_KEY"
```

Template sends work on WhatsApp Business API (Cloud API) channels only. Provide an explicit channel, recipient phone number, and template:

```bash
curl -X POST https://api.qwetty.com/api/v1/messages/template \
  -H "Authorization: Bearer $QWETTY_API_KEY" \
  -H 'Content-Type: application/json' \
  -H 'Idempotency-Key: order-123-template' \
  -d '{"channelId":"CHANNEL_ID","phoneNumber":"+6281234567890","templateId":"TEMPLATE_ID"}'
```

If the template has variables, fill them by part with `parameters`. Qwetty checks every slot against the template before anything is sent and builds what WhatsApp expects:

```json
{
  "channelId": "CHANNEL_ID",
  "phoneNumber": "+6281234567890",
  "templateId": "TEMPLATE_ID",
  "parameters": {
    "header": "https://example.com/banner.jpg",
    "body": ["Budi", "INV-7"],
    "buttons": { "1": "INV-7" }
  }
}
```

`header` is the header's one value (its text variable, or the media URL of an image, video or document header), `body` lists the body's values in order, and `buttons` maps a URL button's index to its value. A missing or extra value returns `422 invalid_parameters` with each problem in `details`. To preview the result, `POST /templates/{id}/render` with the same `parameters`.

To see which templates a conversation can use right now, call `GET /chats/{id}/templates`: while the reply window is closed, only approved WhatsApp templates are listed. Local (non-WhatsApp) templates are sent as ordinary text: render them with `POST /templates/{id}/render` (`chatId` fills `{{contact.name}}`, `agentName` fills `{{agent.name}}`) and send the result.

Sending requires `chats:send_message`; discovering and rendering templates requires `templates:view`. Provider messaging windows and template approval requirements apply.

## 4. Read messages and receive events

```bash
curl 'https://api.qwetty.com/api/v1/chats/CHAT_ID/messages?limit=20' \
  -H "Authorization: Bearer $QWETTY_API_KEY"
```

Subscribe to `message.incoming`, `message.outgoing` and `message.updated` using [webhooks](/en/developers/webhooks). Handle [errors and retries](/en/developers/error-handling) before using the integration in production.
