Website Embed

JavaScript API: control the chat widget

Control the Assistrio chat widget from your code: open, close, toggle, mount, and unmount it, or open it from any button with a data attribute.

Once the script has loaded, it adds two objects to window: AssistrioChat for the Chat Widget and AssistrioCenterStage for Center Stage. Both have the same methods.

Methods

MethodWhat it does
open()Opens the chat panel.
close()Closes the chat panel (the launcher stays).
toggle()Opens it if closed, closes it if open.
mount(config?)Mounts the widget. Optional config is merged over window.AssistrioChatConfig (or window.AssistrioCenterStageConfig for AssistrioCenterStage). If the widget is already on the page, it reloads with the new config — for example, pass userId and userHash after a user signs in.
unmount()Removes the widget from the page.
isMounted()Returns true if the widget is on the page.
JavaScript
// Open the chat from your own "Talk to us" link
document.querySelector("#talk-to-us")?.addEventListener("click", (event) => {
  event.preventDefault();
  window.AssistrioChat?.open();
});

// Hide the widget on checkout pages in a single-page app
if (location.pathname.startsWith("/checkout")) {
  window.AssistrioChat?.unmount();
} else if (!window.AssistrioChat?.isMounted()) {
  window.AssistrioChat?.mount();
}

The script mounts the widget by itself when the page loads, so you only need mount() after an unmount(), or to change the config.

Data attributes

No JavaScript needed: any element with one of these attributes opens the chat when clicked, including elements added to the page later.

HTML
<button data-assistrio-open>Chat with us</button>           <!-- Chat Widget -->
<button data-assistrio-center-stage>Ask AI</button>         <!-- Center Stage -->

When the API is ready

The install code loads the script with async, so window.AssistrioChat may not exist yet when your own code runs. Use optional chaining (window.AssistrioChat?.open()) for clicks, or wait for the load event before calling it at startup.

Note

The widget doesn't emit events to your page (such as “message sent” or “lead captured”). To react to leads and form answers, use webhooks on your server.

TypeScript

assistrio.d.ts
type AssistrioControls = {
  mount: (config?: Record<string, unknown>) => void;
  unmount: () => void;
  isMounted: () => boolean;
  open: () => void;
  close: () => void;
  toggle: () => void;
};

declare global {
  interface Window {
    AssistrioChat?: AssistrioControls;
    AssistrioCenterStage?: AssistrioControls;
    AssistrioChatConfig?: Record<string, unknown>;
    AssistrioCenterStageConfig?: Record<string, unknown>;
  }
}

export {};

Was this page helpful?

Need help? Contact support — we reply within one to two business days.