Skip to main content
Once the chat widget loads, it exposes a small JavaScript API on window.twSdk. Use it to react to what happens on your page: update context when the visitor signs in, open the chat from your own button, or start over.
Want to render the conversation in your own UI instead of the bubble? Use the Chat SDK.

Prerequisites

Available functions

setVariables

Send context to the visitor’s conversation

openChat

Open the widget from your own button

startNewConversation

Close the current conversation and start fresh
The widget initializes itself from window.twConfig, so there is no init function to call.

Wait for the widget to load

window.twSdk exists only after the widget has loaded and fetched its configuration. The widget fires no ready event, so to run a call that must not be lost, wait until window.twSdk exists:
The helper checks every 100 ms and gives up after 30 seconds, for example when the widget can’t load on this domain. For calls a visitor triggers, such as opening the chat from a button, window.twSdk?. is enough.

Update variables

setVariables sends new context to the visitor’s conversation without a page reload. Use it when data changes after the chat loads: the visitor signs in, adds items to a cart, or opens a different order.
Pass variables, signedVariables, or both. They work like the fields of the same name in window.twConfig. The call returns nothing and sends the update in the background.
object
Strings, numbers, or booleans. Unverified, because the visitor’s browser can change them.
string
A JWT your backend signs. Verified, so values pass the Trusted only check. See Identity verification.

Which values are kept

  • Declared variables only. Typewise drops any name you haven’t declared in Context Variables. The call still succeeds, so a typo fails silently.
  • The open conversation. The update applies to the conversation the visitor has open. With no open conversation (before the first message, or after one closes), Typewise holds the values for the next conversation. A closed conversation is never changed.
  • Signed values win. An unsigned value can’t overwrite a value that arrived signed, so a visitor can’t edit what your backend verified.

Verify the visitor after login

A page often starts anonymous and the visitor signs in later. Fetch a new token from your backend and pass it:

Open the chat

Open the widget from any element on your page:

Start a new conversation

Close the current conversation and start a fresh one: