Skip to main content

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​

MethodWhat 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​

MethodReturns
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​

MethodWhat 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​

KindExample
Bearer tokenAgentWidget.setAuth({ kind: "bearer", token: "VISITOR_TOKEN" })
BasicAgentWidget.setAuth({ kind: "basic", username: "USER", password: "PASSWORD" })
API key in a headerAgentWidget.setAuth({ kind: "api_key_header", headerName: "x-api-key", value: "VISITOR_KEY" })
API key in the queryAgentWidget.setAuth({ kind: "api_key_query", paramName: "api_key", value: "VISITOR_KEY" })
Custom headersAgentWidget.setAuth({ kind: "custom_headers", headers: { "x-tenant": "T1", "x-user": "U1" } })
CookiesAgentWidget.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
EventWhen
initThe handshake and appearance are loaded (handler gets { appearance })
readyThe chat panel is ready
open, closeThe panel opens or closes
destroydestroy() finished
request_identityThe agent asks who the visitor is; answer with identify(...)
require_loginThe agent needs the visitor to sign in on your site (for example before a booking); show your login, then call identify(...) and setAuth(...)
requestThe agent sends a request to your page (for example a tool that runs in the browser)
any other nameCustom 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));