Website widget: JavaScript SDK
When the embed code loads, it creates a global object window.AgentWidget. Your page uses it to
open and close the chat, send messages, pass the signed-in user and page context to the agent, and listen to events.
Plan: every plan. Credits: 60 per AI answer sent through the widget.
Wait until the widget is ready before calling commands:
function withWidget(fn) {
if (window.AgentWidget) return fn(window.AgentWidget);
window.addEventListener("AgentWidget:init", () => fn(window.AgentWidget), { once: true });
}
Open, close, remove
| Method | What it does |
|---|---|
AgentWidget.open() | Opens the chat panel |
AgentWidget.close() | Closes the chat panel |
AgentWidget.toggle() | Opens if closed, closes if open |
AgentWidget.destroy() | Removes the bubble, the panel and all listeners. Reload the page to bring it back. |
State
| Method | Returns |
|---|---|
AgentWidget.isOpen() | true when the panel is open |
AgentWidget.isReady() | true when the handshake is done and the chat can be used |
AgentWidget.getState() | { open, ready, initialized, destroyed, appearance } |
Send data to the agent
| Method | What it does |
|---|---|
AgentWidget.sendMessage(text, meta?) | Sends text as if the visitor typed it. meta is optional extra data, for example where the click came from. |
AgentWidget.identify(user) | Tells the agent who the visitor is, for personal answers (id, email, name and any other fields). |
AgentWidget.setContext(context) | Gives the agent page context (current page, product, cart, language). |
AgentWidget.setAuth(auth) | Gives the widget the visitor's login for your own APIs, so agent tools can call them for this visitor. |
AgentWidget.clearAuth() | Forgets that login (call it when the visitor signs out). |
AgentWidget.emitEvent(name, payload?) | Sends a custom event from your page to the agent runtime, for example a payment result. |
AgentWidget.resetSession() | Starts a new conversation. |
AgentWidget.setWidth(px) / AgentWidget.resetWidth() | Changes the desktop panel width (mobile stays full screen). |
withWidget((w) => {
w.identify({ id: "CUSTOMER_REF", email: "customer1@example.com", name: "Customer 1" });
w.setContext({ page: "pricing", plan_selected: "business", locale: "en" });
document.querySelector("#help-with-invoice").addEventListener("click", () => {
w.open();
w.sendMessage("I need help with my invoice.", { source: "billing_banner" });
});
});
setAuth shapes
| Kind | Example |
|---|---|
| Bearer token | AgentWidget.setAuth({ kind: "bearer", token: "VISITOR_TOKEN" }) |
| Basic | AgentWidget.setAuth({ kind: "basic", username: "USER", password: "PASSWORD" }) |
| API key in a header | AgentWidget.setAuth({ kind: "api_key_header", headerName: "x-api-key", value: "VISITOR_KEY" }) |
| API key in the query | AgentWidget.setAuth({ kind: "api_key_query", paramName: "api_key", value: "VISITOR_KEY" }) |
| Custom headers | AgentWidget.setAuth({ kind: "custom_headers", headers: { "x-tenant": "T1", "x-user": "U1" } }) |
| Cookies | AgentWidget.setAuth({ kind: "cookie" }) (your server must set cookies the widget can send) |
Only pass the visitor's own credentials for your website. Never pass your LUMA API key.
Listen to events
const off = AgentWidget.on("open", () => analytics.track("chat_opened"));
AgentWidget.once("ready", () => console.log("chat ready"));
off(); // stop listening
| Event | When |
|---|---|
init | The handshake and appearance are loaded (handler gets { appearance }) |
ready | The chat panel is ready |
open, close | The panel opens or closes |
destroy | destroy() finished |
request_identity | The agent asks who the visitor is; answer with identify(...) |
require_login | The agent needs the visitor to sign in on your site (for example before a booking); show your login, then call identify(...) and setAuth(...) |
request | The agent sends a request to your page (for example a tool that runs in the browser) |
| any other name | Custom events sent by the agent runtime |
Every event is also fired on window as a DOM event named AgentWidget:<event>:
window.addEventListener("AgentWidget:close", (e) => console.log(e.detail));