# getchat Custom Channel API v1

A custom channel lets your app, website or system chat with a getchat assistant. You build the chat UI; getchat answers. Each of your users is one conversation in the getchat dashboard. The channel's assistant answers by itself, and your staff can take over at any time.

- **Send:** your server calls `POST /v1/messages`. getchat stores the message and answers `202` right away with the conversation and message ids.
- **Receive:** when the assistant or a staff member answers, getchat POSTs a `message.created` event to the channel's webhook. Replies are **never** in the response of `POST /v1/messages`.
- **Read back:** the conversation and its messages can be read any time through the API.

Base URL: `https://api.getchat.xyz/v1`. JSON in and out, UTF-8. Updated 2026-11-29.

## Quick start

1. In the dashboard click **+** in the channel rail, pick **Kênh tuỳ chỉnh** (custom channel), name it and pick the assistant. Copy the **API key**; it is shown in full only once.
2. In the channel settings, set your webhook URL (HTTPS) and copy the **webhook secret**.
3. Send the first message from your server:

```bash
curl https://api.getchat.xyz/v1/messages \
  -H "Authorization: Bearer $GETCHAT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"externalId": "user-123", "text": "Đơn GC10293 đang ở đâu?", "contact": {"name": "Phạm Ngọc Anh"}}'
```

A few seconds later your webhook gets `message.created` with the assistant's reply.

## Authentication

Send the channel's key on every request: `Authorization: Bearer gck_…`.

- One key per custom channel; it only works for that channel.
- Making a new key in the channel settings stops the old one at once.
- No CORS: call from your server only, never put the key in a browser or mobile app.

## Concepts

| Name | Meaning |
| --- | --- |
| `externalId` | Your user's id, 1–128 chars of letters, digits and `. _ : @ -`. Same channel + same `externalId` = same conversation. |
| Conversation | Created on the first message of an `externalId`; has a `conversationId` (UUID). |
| `role` | `user` (your user), `assistant` (the getchat assistant), `agent` (your staff in the dashboard). |
| Conversation `status` | `assistant` (assistant answers), `agent` (staff took over, assistant paused), `pending` (waiting for staff, e.g. no assistant or out of credit), `closed` (marked done; a new user message reopens it). |

## POST /v1/messages

Adds one user message; creates the conversation when the `externalId` has none.

| Field | Type | Notes |
| --- | --- | --- |
| `externalId` (required) | string | Your user's id. |
| `text` (required) | string | 1–4,000 characters. Text only. |
| `contact` | object | `name`, `email`, `phone`, `avatarUrl`. Shown to staff; sending it again updates it. |
| `metadata` | object | Up to 20 keys, values string (≤500 chars), number, boolean or null. The assistant can read it, e.g. `{"plan": "vip"}`. Sent back in every webhook of the conversation. |
| `clientMessageId` | string | Your message id (≤128 chars). Sending the same id again does not duplicate the message, so retries are safe. |

Response `202 Accepted`:

```json
{ "conversationId": "01a22160-fa2f-73df-bfe9-5af3c3a0c8dd", "messageId": "01a22160-fa59-75ec-95ca-f8299667ed4d", "status": "assistant", "createdAt": "2026-11-28T03:12:00Z" }
```

`status` is the conversation status when the message arrived. With `agent` or `pending` the assistant will not answer; the reply comes when staff answer in the dashboard.

## GET /v1/conversations/{externalId}

```json
{ "conversationId": "…", "externalId": "user-123", "status": "agent", "assignee": { "name": "Linh", "avatarUrl": "https://…" }, "contact": { "name": "Phạm Ngọc Anh" }, "metadata": { "plan": "vip" }, "createdAt": "…", "lastMessageAt": "…" }
```

`404 ERR_CONVERSATION_NOT_FOUND` when the `externalId` has no conversation.

## GET /v1/conversations/{externalId}/messages?after={messageId}&limit=50

- `after` (optional): only messages after this message id; use it to catch up after missed webhooks.
- `limit` (optional): 1–100, default 50.

```json
{ "messages": [ { "id": "…", "role": "user", "text": "Đơn GC10293 đang ở đâu?", "createdAt": "…" }, { "id": "…", "role": "assistant", "text": "…", "sender": { "name": "Hỗ trợ đơn hàng" }, "createdAt": "…" } ], "hasMore": false }
```

Oldest first. Staff internal notes are never returned.

## DELETE /v1/conversations/{externalId}

Deletes the conversation and all its messages for good (e.g. a user asks to delete their data). The next message of the same `externalId` starts a new conversation. Returns `{"deleted": true}`.

## Webhook

getchat POSTs JSON to the channel's HTTPS webhook URL.

| Event | When |
| --- | --- |
| `message.created` | The assistant or staff sent a message to the user. Show it in your chat UI. |
| `conversation.typing` | The assistant or staff is typing; has `typing: {"by": "assistant", "state": "start"}`. Optional. |
| `conversation.status_changed` | Status changed, e.g. staff took over (`agent`), handed back (`assistant`) or closed (`closed`). |
| `webhook.test` | "Gửi sự kiện thử" in the channel settings. Only `id`, `type`, `createdAt`, `channelId`; any `2xx` passes. |

```json
{
  "id": "c1c4b5a2-8732-4b9c-ac9d-0eea82245a42",
  "type": "message.created",
  "createdAt": "2026-11-28T03:12:04Z",
  "channelId": "01a22071-1ccb-7552-abbb-54c260b5eb1a",
  "conversation": { "conversationId": "…", "externalId": "user-123", "status": "assistant", "metadata": { "plan": "vip" } },
  "message": { "id": "…", "role": "assistant", "text": "…", "sender": { "name": "Hỗ trợ đơn hàng", "avatarUrl": "https://…" }, "replyTo": "<user message id>", "createdAt": "…" }
}
```

### Signature

Headers: `X-Getchat-Signature: t=<unix seconds>,v1=<hex>` and `X-Getchat-Event-Id: <event id>`.

1. Read `t` and `v1` from the header.
2. HMAC-SHA256 of `{t}.{raw body}` with the webhook secret, as hex.
3. Compare with `v1` in constant time; reject when `t` is more than 5 minutes off.

```js
import crypto from "node:crypto";
app.post("/getchat/webhook", express.raw({ type: "application/json" }), (req, res) => {
  const { t, v1 } = Object.fromEntries((req.get("X-Getchat-Signature") || "").split(",").map((p) => p.split("=")));
  const expected = crypto.createHmac("sha256", process.env.GETCHAT_WEBHOOK_SECRET).update(`${t}.${req.body}`).digest("hex");
  const ok = v1 && v1.length === expected.length && crypto.timingSafeEqual(Buffer.from(v1), Buffer.from(expected)) && Math.abs(Date.now() / 1000 - Number(t)) < 300;
  if (!ok) return res.sendStatus(401);
  res.sendStatus(200); // answer first, then handle
  handleEvent(JSON.parse(req.body));
});
```

### Delivery and retries

- Answer `2xx` within **10 seconds**; do heavy work afterwards.
- On an error, a timeout or a non-2xx answer getchat retries after 1 min, 5 min, 30 min, 2 h, 6 h and 24 h (7 tries in all).
- An event can arrive **more than once**: skip ids you already handled. Order is not guaranteed: sort by `createdAt`.
- After 3 days of failures the webhook is paused and the account owner is emailed. Messages are still stored; read them with `GET …/messages?after=…`.
- Without a webhook you can poll `GET …/messages?after=…`, at most once every 3 seconds per conversation.

## Takeover and the assistant

- Staff taking over sends `conversation.status_changed` with `status: "agent"`; replies then come with `role: "agent"`. Handing back sets `assistant`.
- Changing the channel's assistant does not affect the API; old conversations stay.
- Each assistant reply uses the workspace's credit. Out of credit, the assistant pauses and the conversation becomes `pending` until staff answer.

## Limits

| Limit | Value |
| --- | --- |
| All requests of one channel | 600 / minute |
| Sending for one `externalId` | 30 messages / minute |
| Reading one `externalId` | 60 requests / minute |
| Message length | 4,000 characters |

Over a limit: `429` with a `Retry-After` header (seconds).

## Errors

Errors are JSON: `{ "error": "ERR_API_KEY_INVALID", "message": "API key is missing or invalid." }`. Every response has an `X-Request-Id` header.

| HTTP | error | When |
| --- | --- | --- |
| 400 | `ERR_INVALID_INPUT` | Body is not JSON, a required field is missing, bad `externalId`, empty or too long `text` |
| 401 | `ERR_API_KEY_INVALID` | Missing, wrong or replaced key |
| 404 | `ERR_CONVERSATION_NOT_FOUND` | No conversation for this `externalId` |
| 409 | `ERR_DUPLICATE_MESSAGE` | Same `clientMessageId` with different text |
| 429 | `ERR_RATE_LIMIT` | Over a limit |
| 5xx | `ERR_INTERNAL` | getchat error; retry with the same `clientMessageId` |

## Support

dev@getchat.xyz — include the `X-Request-Id` of the failed request. Human-readable guide (Vietnamese): https://getchat.xyz/docs/api
