Skip to content

Chat widget JavaScript API reference

The install code defines window.Click2Reply. Until the widget has loaded, that global is a stub that queues on, once, off and call; once it loads, the queued calls run in order and every method below is available. So call("<name>", ...) is the form that works at any time, and the direct methods are for code that runs after the ready event.

// Safe anywhere, even before the widget has loaded.
Click2Reply.call("set_customer_email", "ana@example.com");
// The direct form, once the widget is ready.
Click2Reply.on("ready", () => Click2Reply.maximize());

window.__c2r is read once, when the widget loads:

FieldTypeWhat it does
licensenumberYour license number. Required.
groupstringThe group new chats from this page go to, by its id.
customer{ name?, email? }The customer’s name and email, prefilled.
paramsobjectSession variables, { "plan": "pro" }: agents see them next to the chat.
identity_tokenstringA token your server signed for the signed-in user; see identity tokens.
async_initbooleanLoad nothing until the page calls Click2Reply.init().
Methodcall nameWhat it does
maximize()maximizeOpen the chat window.
minimize()minimizeClose the window back to the chat button.
hide()hideHide the widget, chat button included.
destroy()destroyRemove the widget from the page for this page view.
setCustomerName(name)set_customer_nameSet the customer’s name, up to 120 characters.
setCustomerEmail(email)set_customer_emailSet the customer’s email.
setSessionVariables(vars)set_session_variablesReplace the session variables.
updateSessionVariables(vars)update_session_variablesAdd to or change the session variables, keeping the rest.
setCart({ value, currency, items })set_cartThe cart agents see: value in the currency’s main unit, currency an ISO 4217 code, items a count.
track(event)trackReport a sale or a goal; see tracking sales and goals.
setSounds(on)set_soundsTurn the widget’s sounds on or off.
setCookieConsent(granted)set_cookie_consentWith the organization’s cookie-consent setting on, the widget forgets the visitor between pages unless the page calls this with true, on every page load once the visitor has consented. false withdraws consent and removes what the widget stored.
get(what)-Read "state", "customer_data" or "chat_data"; only after ready.
on(event, fn), once(event, fn), off(event, fn)-Listen to the events below.
init()-Load the widget when the config says async_init.

Session variables are up to 50 pairs, names up to 64 characters and values up to 512; longer ones are cut.

Click2Reply.get("state");
// { availability: "online" | "offline", visibility: "maximized" | "minimized" | "hidden" }
Click2Reply.get("customer_data");
// { id, name, email, isReturning, status: "browsing" | "invited" | "queued" | "chatting", sessionVariables }
Click2Reply.get("chat_data");
// { chatId, threadId } while a chat is open, otherwise null

track credits a sale or a goal to the chat that came before it, within 7 days:

Click2Reply.call("track", { type: "order_created", order_id: "A-1001", total_cents: 4990, currency: "USD" });
Click2Reply.call("track", { type: "product_added", product: { id: "sku-42", name: "Mug", quantity: 1, price_cents: 1290 }, currency: "USD" });
Click2Reply.call("track", { type: "goal", name: "signup" });

Amounts are integer cents. product_removed takes the same fields as product_added, and order_created takes an optional products list. A server can report the same events with POST /v1/track.

EventIts argument
ready{ state, customerData }, as get returns them.
availability_changed{ availability: "online" | "offline" }
visibility_changed{ visibility: "maximized" | "minimized" | "hidden" }
customer_status_changed{ status: "browsing" | "invited" | "queued" | "chatting" }
new_event{ type, author: { id, type: "agent" } } when an agent’s message or file arrives.
greeting_displayed, greeting_hidden{ id, uniqueId } of the greeting.
rich_message_button_clicked{ postbackId } of the button.
form_submitted{ type: "prechat" | "postchat" | "ask_for_email" | "custom" }
rating_submitted"good", "bad", or "none" when a rating is taken back.
Click2Reply.on("new_event", (e) => {
if (document.hidden) document.title = "New message";
});
Click2Reply.on("availability_changed", ({ availability }) => {
document.querySelector("#chat-button").hidden = availability === "offline";
});

customer names the visitor; an identity token proves who they are, so a signed-in user sees their chat history on every device. Sign a JWT on your server for the signed-in user and put it in window.__c2r.identity_token:

  • Algorithm: ES256 or RS256. Add the public key, as a JWK, in the agent app or with POST /v1/customer-token/keys. Click2Reply never holds a key that can sign.
  • Claims: sub is your user id (1 to 128 characters), aud is your license number as a string, exp is at most 24 hours ahead. nbf is honored when present.
  • Header: kid may name the key; without it every key of that algorithm is tried.

Never sign the token in the browser: anyone could then claim to be anyone.