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.
Prerequisites
- The chat widget is installed on your page. See Installation.
- Every variable you send is declared in Context Variables.
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
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:
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.
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:Related
- Identity verification: sign variables so they arrive verified
- Context Variables: declare the variables Typewise keeps
- Chat configuration: install and configure the widget