CommerceOS

Widget & API

JavaScript API

Create and control the chat from your own code.

For full control, create the chat yourself instead of using data-public-key. Load the script without the attribute and call CommerceOSChat.createChat, or import the ES module in a bundled app.

Script tag
<script src="https://cdn.selfserve.cosmos.hub.nortq.com/v1/chat.js"></script>
<script>
  const chat = CommerceOSChat.createChat({
    publicKey: "pk_live_your_key",
    apiUrl: "https://chat.selfserve.cosmos.hub.nortq.com",
  });
</script>
ES module
import { createChat } from "https://cdn.selfserve.cosmos.hub.nortq.com/v1/chat.esm.js";

const chat = createChat({
  publicKey: "pk_live_your_key",
  apiUrl: "https://chat.selfserve.cosmos.hub.nortq.com",
  launcher: "hidden",
  onUnreadChange: ({ count, teaser }) => updateBadge(count, teaser),
});

document.querySelector("#help").addEventListener("click", () => chat.toggle());

Options

OptionTypeDescription
publicKeystringRequired. Your key from Setup → Install.
apiUrlstringRequired. https://chat.selfserve.cosmos.hub.nortq.com.
mode"window" | "fullscreen" | "inline"Default window.
targetstringCSS selector to mount into. Default: the page body.
launcher"default" | "hidden"hidden shows no chat button — open it from your own.
entryUrlstringThe page the visitor started on, if not the current one.
trackPageViewsbooleanRecord the pages a visitor views, shown beside their conversation. Default true.
serviceWorkerstringPath of your reply-notification worker. See Visitor notifications.
onUnreadChange({ count, teaser }) => voidCalled when unread replies change. teaser is up to 140 characters of the newest one.
onEvent({ type, data }) => voidCalled for widget events.

Controller

createChat returns a controller:

MethodDescription
open()Open the chat.
close()Close it.
toggle()Open if closed, close if open.
isOpen()Whether it's open.
getUnread(){ count, teaser } — read once on load, then follow onUnreadChange. Unread survives page navigation.
reset()Start a fresh conversation for this visitor.
destroy()Remove the chat from the page.