---
name: voxego-assistant-app
description: Connect an app that draws its own chat screen to a Voxego assistant through the app's server: the /chat route, history sent every time, one visitor id per person, bracketed lines for what the app did, lookup forms in the app's screen, and voice with /talk and /listen. Use when someone builds their own chat screen for a Voxego assistant instead of the chat bubble.
---

# Connect an app's own chat screen to a Voxego assistant

Use this when an app (a web app, a PWA, a mobile app, a shop's own storefront) draws its own chat instead of using the chat bubble. The app's server talks to the assistant; the screen talks only to the app's server. Most of the trouble in such builds comes from four things this skill gets right from the start: sending the history every time, one visitor id per person, telling the assistant what the app did itself, and lookup forms in the app's own screen. Stop at the points marked **Ask**, and never confirm a preview on your own.

## 0. Check the assistant

Call `org_whoami`, then `assistants_get`: its status, what it keeps (`keep`), its lookups, its `voice` settings and `visitor_daily` (replies one visitor gets a day). If there is no assistant yet, set one up first with the "Set up a Voxego assistant" steps, as far as testing it. `assistants_embed` gives this assistant's own calls in its second half; read it before writing code.

## 1. Learn the app

**Ask**, only what you can't see in the code:

- Where the server code lives (the framework, the route folder).
- How the app knows a person: a signed-in user's id, or nothing (then the app makes an anonymous id per device).
- What the app's own screen does that the assistant doesn't: starting a return, booking a time, adding to the cart, applying a code.
- Whether visitors look up orders, send photos, or talk by voice.

## 2. The key

`assistants_keys_create` makes the key (the person says yes first; the secret is shown once). It goes in the app server's environment as `VOXEGO_ASSISTANT_KEY`. A call with it from a web page is refused (403 `origin_not_allowed`), so the screen never calls Voxego directly.

## 3. One server route

Add one route to the app, such as `POST /api/assistant`. The screen sends it the message and the conversation id; the route adds what the screen shouldn't hold, calls the assistant and hands back the reply:

```
POST https://voxego.dev/api/v1/assistants/{id}/chat
Authorization: Bearer $VOXEGO_ASSISTANT_KEY

{ "message": "…", "conversation": "c_… from the last reply", "history": [ … ], "visitor": "…", "lang": "pt-PT" }
```

`lang` is the page's language, so a short first message is answered in it. Keep `conversation` from the first reply and send it back with every message of that conversation.

## 4. Send the history every time

The assistant remembers nothing between messages unless it keeps conversations. So:

- Keep each conversation's turns (on the server, or in the screen's state and sent up with each message).
- Send them as `history` with every message: oldest first, each `{ "role": "user" | "assistant", "text": "…" }`, up to the last 40, the new message not included.
- After each reply, add the visitor's turn and the reply's text to it.
- A reply with a `warning` saying no history came means it was forgotten: it will greet again and ask for things already given.
- Send history even when the assistant keeps conversations. It ignores it then, but if keeping stops later (a set number of hours ends, or someone changes it) history is what it remembers with.

## 5. One visitor id per person

`visitor` is the id the app knows that one person by, up to 200 characters: the signed-in user's id, or a random id the app stores per device (a cookie or local storage). Send a hash of it if you prefer; never an email.

- Never one fixed value for everyone (the app's name, the server's id, "web"). Each visitor gets `visitor_daily` replies a day, so a shared id makes every shopper share that allowance, and once it's used up everyone gets 429 `visitor_limit_reached` until tomorrow.
- Lookups and voice require it: each visitor gets 5 lookups an hour (3 misses in a row pause them for an hour) and 15 minutes of voice chat a day.

## 6. Tell it what the app did

When the app's own screen does something (a return card completed, a booking made), add one assistant turn to the history saying so, in brackets as the whole turn:

```
{ "role": "assistant", "text": "(Return started for 1 item from ORD-48213. Refund of €129.00 to the original card within 5 working days of arrival.)" }
```

The assistant takes a bracketed turn as a fact from the app, amounts and dates included, so it can answer "when do I get my refund?" next. Put in only what it may repeat to the visitor: no emails, addresses or card details. If the assistant keeps conversations it ignores history, so send the same line as `context` on the next message instead, such as `{ "done": "Return started for 1 item from ORD-48213" }` (up to 200 characters a value). `context` also tells it what the visitor is looking at, such as `{ "product": "SKU-1" }`.

## 7. Lookup forms in the app's screen

A reply with `lookup` asks for live details, such as an order. The screen shows a form, never asks in the chat:

- One field per entry of `lookup.fields` (the business's own labels, each `{ key, label, type }`) or, without them, per entry of `lookup.asks` (such as `["order", "email"]`). `lookup.prefill` is an order number the visitor already typed: show it filled in. `lookup.name` is the form's title.
- The route sends it to `POST https://voxego.dev/api/v1/assistants/{id}/lookup` with `{ "lookup": lookup.id, "order", "email" or "postcode" (or "fields"), "visitor", "conversation", "lang": lookup.lang }`.
- Show the answer as an assistant turn and add it to the history.

## 8. Show the reply

Replies are Markdown; render it. Each reply has `language`, `follow_up`, `answered` and `turned_away`, for styling or logs. If photos are on, send `image` as a data: URL of a JPEG, PNG or WebP shrunk in the browser to about 1024 px, up to about 1.5 MB.

## 9. Voice (only if it's on)

`GET https://voxego.dev/api/v1/assistants/{id}` with the key answers `voice: { chat, read_aloud, voice }`; show only what is on. The same key and `visitor` go on every voice call.

- **Voice chat:** the screen records one turn (stop on a pause, at most 30 seconds); the route sends `POST …/talk` with `{ audio, visitor, conversation, history, context, lang }`. Show `heard` as the visitor's turn and `reply` as the assistant's, play `audio` (`mime`), and add both to the history. `reply: null` means nothing was heard and nothing was charged: record again.
- **While a turn is on its way** (over about 1.5 seconds), play a short line: `GET …/line?lang=pt&n=0..3` (free; cache it for a day).
- **Read aloud:** a Listen button under a reply from the last hour sends `POST …/listen` with `{ text, visitor, lang }` and plays `audio`.
- 429 `voice_limit_reached`: show "Voice limit reached." and keep writing. 429 `conversation_long`: start a new conversation.

## 10. Errors the route handles

| Status | Code | What the app does |
|---|---|---|
| 401 | | The key is wrong or revoked; tell the operator |
| 403 | `origin_not_allowed` | The call came from a browser; move it to the server |
| 404 | | The key belongs to another assistant |
| 409 | `assistant_off` | It's off; while building send `"test": true` |
| 422 | `visitor_invalid` | `visitor` missing or too long |
| 429 | `visitor_limit_reached` | This visitor's replies for today are used; if everyone gets it, the visitor id is shared (step 5) |
| 429 | `monthly_spend_reached` | The assistant's monthly cap is reached; tell the operator |
| 413 | `body_too_large` | Shrink the photo |

## 11. Test before going live

Send `"test": true` on /chat, /lookup, /talk and /listen while building: free (50 test replies a day per assistant), nothing kept or charged, and it works while the assistant is off. Run, through the app itself:

1. Two messages where the second only makes sense after the first ("do you have it in blue?" then "and in size 42?"). No `warning`, and the second answer uses the first.
2. Two different visitors, to see each has their own id.
3. An action in the app, then "when do I get my refund?": the answer uses the bracketed line.
4. A lookup through the form, with a real order the person gives you.
5. One voice turn and one Listen, if voice is on.

Show the person what each did. Then, if it isn't live, `assistants_set_status` `live` (asks them first).

## 12. Hand over

Tell the person: where the route lives, where the key is kept, how the visitor id is made, which app actions send a bracketed line, and what each error shows the visitor.

## Always

- The key stays on the server; never in the screen, a page or a chat.
- A key's secret is shown once: give it to the person and don't repeat it.
- Never confirm a preview without the person's yes.
- Don't tell the person what a law requires, and don't plan their time for them.
