# Live Chat

The storefront chat: the platform script, endpoints and WebSocket messages for themes and headless storefronts.

Live Chat lets shoppers talk to the store's team from the storefront. It is served on every store domain under `/api/shop/2026-11/chat`. Themes use the platform script and design their own chat block; headless storefronts can use the script or call the endpoints. Live Chat is available while the store has it on (Grow plan and up); otherwise every endpoint except `config` answers `403 LIVE_CHAT_UNAVAILABLE`.

## The script

Load `/api/shop/2026-11/chat/v1/chat.js` once (themes: `shop.live_chat.script_url`). It has no markup or styling and defines `window.TringifyChat`:

- `ready()` → the config (`enabled`, `online`, `welcome_message`, `limits`).
- `start()` → starts or resumes the shopper's conversation. Signed-in customers are recognised; guests solve a small proof-of-work first (handled by the script).
- `send(text, {attachments})` sends a message; `attachments` is a list of what `attach(file)` resolved to.
- `attach(file)` uploads one file and resolves to its attachment: the `attachment` value `POST /chat/attachments` returns (a string). Pass it unchanged in `send`'s `attachments`.
- `setEmail(email, name)`, `typing()`, `history()`, `close()`.
- `requestPerson()` → Talk to a person: the store's team takes over (the store's assistant falls silent) and the chat becomes a case in the store's Inbox. Resolves `{requested, case}`; `case` is `{number, reference}` once the store has it, which also arrives with the `case` event and as `TringifyChat.case`. Asking again returns the same case.
- `on(event, fn)` for `ready`, `history`, `message`, `typing`, `online`, `action`, `case`, `status`, `ended` and `error`; returns an unsubscribe function.
- `history` fires twice: once `ready()` resolves, with the messages this tab already holds from earlier pages (they are kept in the tab, so the chat can be drawn before any connection), and again after the WebSocket's `hello`, with those merged with the conversation's messages from the server. Render the list each time it fires.
- `online`: whether staff are online right now. It is live, never part of a cached page.
- `configure({base})`: a headless site points the script at the store domain.
- `version`: the script's version (1.1.0).

```html
<script src="{{ shop.live_chat.script_url }}" defer></script>
<script>
  document.addEventListener("tringify:chat-loaded", () => {
    TringifyChat.on("message", (m) => render(m));
    TringifyChat.on("online", (online) => showStatus(online));
  });
</script>
```

## Endpoints

| Request | Does |
| --- | --- |
| `GET /chat/config?language=` | `enabled`, `online`, `welcome_message` (the store's text in that storefront language), `script_url`, `socket_url`, `limits`. |
| `POST /chat/identity` | A two-minute `identity` for this visitor: the signed-in customer (the storefront cookie on the store's own pages, or a headless site's `Tringify-Shop-Session` sent with a storefront or server key of the headless storefront that started it; see Customer accounts) or a guest. Refused while Live Chat is off or the visitor is on the store's block list for messages. |
| `POST /chat/challenge` | A proof-of-work `challenge` and `bits`: find a `nonce` so that SHA-256 of `challenge + ":" + nonce` starts with `bits` zero bits (16 today). |
| `POST /chat/start` | Body `{identity, challenge, nonce}` (guests) or `{identity}` (customers, or with a current chat token). Returns `conversation_id`, `token` (12 hours; also set as an HttpOnly cookie for same-origin pages), `socket_url`. Calling it again with a current token resumes the same conversation. |
| `GET /chat/socket?token=` | The WebSocket (below). |
| `GET /chat/messages` | The conversation's messages since it was last saved to the store's Inbox, and `online`. |
| `POST /chat/messages` | Body `{text, client_id, attachments}`: sends without a WebSocket. |
| `POST /chat/email` | Body `{email, name}`: where the store answers when no one is online. |
| `POST /chat/attachments` | Body `{filename, content_type, size}` → `upload_url` (PUT the bytes with that Content-Type within 15 minutes) and `attachment` for `send`. JPEG, PNG, GIF, WebP, PDF and plain text, up to 10 MB, 5 per message. |
| `POST /chat/person` | Body `{language}`: Talk to a person (the same as `requestPerson()`). Returns `{requested, case}`. |

Every request but `config` and `identity` sends the chat token as `Tringify-Chat-Token` (or the cookie). A token opens only its own store's conversation. Cross-origin requests from a headless site need its public storefront key (`Tringify-Shop-Key`) with the site's origin allowed.

## WebSocket messages

Messages are JSON. Send `ping` every 25 seconds; the server answers `pong`.

| Direction | Message |
| --- | --- |
| page → server | `{type:"message", text, client_id, attachments}`, `{type:"typing"}`, `{type:"person", language}` (Talk to a person; answered with `{type:"person", requested, case}`) |
| server → page | `{type:"hello", conversation_id, online, email, messages, limits, case}` on connect; `case` is null, or `{number, reference}` (number null until the store has it) once a person was asked for |
| server → page | `{type:"message", message}`, `{type:"typing", author_kind}`, `{type:"online", online}`, `{type:"error", client_id, error}` |
| server → page | `{type:"action", id, action}` (reserved, below) |
| server → page | `{type:"case", case:{number, reference}}`: the store's Inbox opened the case |

A message is `{id, author_kind, author_name, text, attachments[{filename, content_type, size, url}], created_at, client_id}`. `author_kind` is `shopper`, `staff`, `assistant` or `system`. Text is plain text, up to 2,000 characters; a shopper can send 20 messages a minute. A `system` message records an event (such as the request to talk to a person) in English for the store's team; show it in your own words or not at all.

## Reserved for the store's assistant

A store assistant will be able to answer in Live Chat. Its messages have `author_kind` `assistant`; once a staff member replies, or the shopper asks for a person, the assistant stays silent in that conversation. When it can't help it hands the conversation to the team as a case, with a summary the team sees as an internal note. It may also send `action` messages, which the script carries out only after checking them:

- `navigate`, `open_product`, `apply_filter`: `url` on the store's own origin; the script opens it unless the page's `action` handler calls `preventDefault()`.
- `add_to_cart`: `product_id`, `variant_id`, `quantity`; nothing happens until the shopper confirms: the page shows the offer and calls `action.confirm()`, which calls `TringifyChat.handlers.add_to_cart({product_id, variant_id, quantity, url})` when the theme set one (post them to the cart as `product_id`, `variant_id` and `quantity`), else opens the product.
- Any other action name, and any URL on another origin, is ignored.

Actions are never sent today; build the handlers so the chat block is ready.

## What the store sees

Staff with Inbox access answer from Store Admin or the app while the chat is live. A conversation quiet for 10 minutes is saved in the store's Inbox (channel `live_chat`); later messages from the same shopper join it. When nobody is online, the store answers by email to the address the shopper gave, in the same Inbox conversation.

