Embed the web widget
Paste the ready-made widget on your site, or build your own chat window on the same browser API.
There are two ways to put your agent on your website. The quickest is to paste the Careflow widget's snippet and style it in the dashboard. If you want a chat window that's part of your own design, build it yourself on the widget API, which is what the Careflow widget uses under the hood. Both start the same way, in the dashboard.
The example here is a hotel, Harbour View, whose website is https://www.harbourviewhotel.com.
1. Create the widget in the dashboard
You need the Manager role for this.
- Go to Agents, choose your agent, then open DeployWeb.
- Choose Create web widget.
- On the Install tab, add each website the widget will run on under Allowed website origins, one per line, such as
https://www.harbourviewhotel.com. - Make sure the agent is published. See Publish and go live.
An allowed website is the scheme, host and any port, with no path. https://www.harbourviewhotel.com and https://harbourviewhotel.com are different, so add both if visitors use both. Add http://localhost:3000, or wherever you develop, while you build.
The Install tab also shows the widget's publishable key, inside the snippet. It starts with pk_live_ and is safe to put in your pages. See Add the web widget for styling, the Help tab and the other settings.
2a. Paste the ready-made widget
Copy the snippet from the Install tab and paste it before the closing </body> tag on every page that should show the widget:
<script src="https://app.trycareflow.com/widget.js"
data-key="pk_live_xxx"
data-api="https://api.trycareflow.com" async></script>
That's all. The widget loads its colours, greeting and behaviour from Careflow each time a page loads, so changes in the dashboard reach your site without touching the snippet. On a site that isn't on the allowed list, it shows nothing.
2b. Build your own chat window
If you'd rather write the interface yourself, your page calls the widget API directly from the browser. The flow has three steps: read the settings when the page loads, start a session when the visitor opens the chat, then send messages with the session token.
Read the settings
Call this once per page load. It tells you whether the widget should appear at all, and gives you the brand colour, greeting and agent name you set in the dashboard.
const API = "https://api.trycareflow.com";
const KEY = "pk_live_xxx";
const res = await fetch(`${API}/v1/web/config?key=${encodeURIComponent(KEY)}`);
const body = await res.json();
if (body.code !== "SUCCESS") {
// origin_rejected, channel_disabled or unknown_key: show nothing on this page
} else {
const { config, assistantName } = body.data;
// config.color, config.hero.headline, config.inputPlaceholder, config.persistence, ...
}
Start a session
When the visitor opens your chat, create a session. Send the visitorId you saved last time so they see their earlier conversations. For a new visitor, leave it out and save the one that comes back.
async function startSession() {
const visitorId = localStorage.getItem("careflow_visitor") || undefined;
const res = await fetch(`${API}/v1/web/session`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ publishableKey: KEY, visitorId }),
});
const body = await res.json();
if (body.code !== "SUCCESS") throw new Error(body.message);
localStorage.setItem("careflow_visitor", body.data.visitorId);
return body.data.token;
}
The visitorId gives access to that visitor's past conversations, so keep it in the visitor's own browser and don't swap it for something guessable like their email address. If the dashboard's Remember returning visitors is off, config.persistence is memory: keep the ID in memory only and let each visit start fresh.
A session needs a published agent. If the agent isn't published, this call returns 403 with not_published, and your window should say chat is unavailable.
Send messages
Each message is one request, and the reply comes back in the response. Keep the conversationId from the reply and send it with the next message to stay in the same thread. Send new: true instead when the visitor starts a new chat.
let token = await startSession();
let conversationId;
async function send(text) {
const turn = { message: text, conversationId };
let res = await post("/v1/web/messages", turn, token);
if (res.status === 401) {
token = await startSession();
res = await post("/v1/web/messages", turn, token);
}
const body = await res.json();
if (body.code !== "SUCCESS") throw new Error(body.error || body.message);
conversationId = body.data.conversationId;
return body.data.reply;
}
function post(path, payload, token) {
return fetch(`${API}${path}`, {
method: "POST",
headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
}
A session token lasts 30 minutes from when it was created, so the 401 retry above isn't optional: a visitor who leaves the tab open will need a new one. Replies can contain Markdown such as bold text, lists and links, so render them rather than showing raw text.
Show past conversations
GET /v1/web/conversations lists up to 30 of the visitor's recent conversations, each with its last message, and GET /v1/web/conversations/{id} returns all the messages in one. Both take the session token as Authorization: Bearer. A visitor can only read their own.
const res = await fetch(`${API}/v1/web/conversations`, {
headers: { Authorization: `Bearer ${token}` },
});
const { data } = await res.json();
// data.conversations: [{ id, preview, role, status, updatedAt }]
Add help pages
If you switched on knowledge sources for the Help tab under DeployWebContent, GET /v1/web/help?key=pk_live_xxx lists them with a title and an excerpt, and GET /v1/web/help/{id}?key=pk_live_xxx returns the full text. Pass q to search. These take the publishable key, not a session, so you can show them before the visitor starts chatting.
Add voice
For voice calls, open a WebSocket to wss://api.trycareflow.com/v1/web/voice/stream?token=<session token>, adding &conversationId=... to continue a chat thread by voice. You stream microphone audio up and play the agent's audio back, both as base64-encoded 16-bit PCM at 24,000 samples a second, mono. The agent needs voice turned on. The API Reference lists every message the socket sends and receives.
Limits to plan for
Each widget answers up to 1,000 visitor messages a day, counted from midnight UTC. After that, messages return 429 with cap_exceeded until the next day. Separately, each visitor's browser can make 200 requests a minute. See Rate limits.
Where to go next
- Add the web widget covers styling and the dashboard settings.
- Authentication explains publishable keys, allowed websites and session tokens.
- Errors lists every failure your window should handle.