Introduction
About the API
The Chat Widget JavaScript API lets you interact with the Chat Widget embedded on your site. It might come in handy whenever you want to gather some additional data using LiveChat, show or hide your Chat Widget on certain occasions, or amend its behavior in a way that is not provided by the standard settings.
This document focuses on Developers and requires a basic knowledge of JavaScript. However, if you would have any questions, don't hesitate to start a chat with our Support Team or add a new topic on Developers Community.
Getting started
The API is accessed through the LiveChatWidget object. It is being initialized within the LiveChat tracking code, which is used to install our Chat Widget on your site. If you haven't installed the code yet, you can either find it directly in the LiveChat app or copy it from here (remember to replace <LICENSE_NUMBER> with your license number):
The API requires the newest version of the code snippet (≥2.0). If you had installed the code a while ago and the API presented here doesn't work, you should either update the code or use the old version of JS API. You can check the current version of your snippet by calling the scriptTagVersion function:
The LiveChatWidget object comes with four functions:
- on registers a callback function for a specific event
- once similar to on but after single event trigger the callback is removed
- off removes a callback registered by on
- call allows you to invoke methods and setters
- get makes it possible to fetch data
The usage of these functions is based on passing the correct arguments in the following pattern:
For example, if you would like to set your customer's name to John Doe, this is how it would look like:
Asynchronous Initialization
Asynchronous initialization is an optional feature available for Chat Widget via JavaScript API. It allows for far more control over the moment when the Chat Widget should be loaded. It can be especially useful if you would like to make widget initialization happen as a consequence of some user interaction, or your own application business logic event.
This feature is disabled by default, so the Chat Widget loads automatically just after the snippet code is executed. In order to enable it, you need to explicitly set __lc.asyncInit property to true.
From now on the snippet code will be executed but the Chat Widget and its resources will not be loaded. It means that although you will have access to LiveChatWidget object, the widget itself will not be present on your website and the ready callback will not be triggered.
Later in your code you can initialize Chat Widget manually when needed. To do that simply call init function from LiveChatWidget object.
Methods
To change the visibility of the Chat Widget, you can use the following methods:
- Maximize
- Minimize
- Hide
- Destroy
- Hide greeting
Maximize
It maximizes the Chat Widget.
ExampleMinimize
It minimizes the Chat Widget.
ExampleHide
It hides the Chat Widget on the site. You will need to use either 'maximize', or 'minimize' API calls to show it afterwards.
Destroy
It destroys the Chat Widget. It won't be available for further actions until the window is refreshed.
ExampleHide greeting
It hides the currently displayed greeting. In order to hide particular greeting you can listen to new_event or greeting_hidden callbacks to know when greeting with specified id or uniqueId appears and disappears accordingly.
ExampleGetters
You can use getters to fetch the data from the Chat Widget.
Available getters:
- Get state
- Get customer data
- Get chat data
Get state
It returns the Chat Widget state. This includes Widget visibility and license availability.
ExampleResponse
availability | 'online' | 'offline' | Group's availability |
visibility | 'maximized' | 'minimized' | 'hidden' | the Chat Widget visibility |
Get customer data
It returns a filtered list of customer data.
ExampleResponse
id | string | unique customer id |
name | string | customer name, as provided |
string | customer e-mail address, as provided | |
isReturning | boolean | if this customer visited you before |
status | 'queued' | 'chatting' | 'browsing' | 'invited' | Customer's status |
sessionVariables | Record<string, string> | additional free-form session information |
Get chat data
It returns chat data which contains current chat and thread ids.
ExampleResponse
chatId | string | unique chat id |
threadId | string | unique current thread id |
Setters
Various data can be sent over to the LiveChat integration so that your Agents can use it. For more information, you can read these articles in our Help Center: //www.livechat.com/help/custom-variables-configuration/.
Available setters:
- Set session variables
- Update session variables
- Set customer name
- Set customer email
Set session variables
Creates new session variables, or replaces the ones set previously.
Update session variables
It changes the values of your session variables. Please note that the existing variables won't be replaced. The new session variables will be included together with the ones set previously.
ExampleSet customer name
It sets the customer's name.
Set customer email
It sets the customer's email address.
ExampleCallbacks
Callbacks allow you to react to the events triggered by the Chat Widget. You can use them to add custom behaviors when certain events happen. This can be accomplished by subscribing a callback with the API.
Available callbacks:
- On ready
- On availability changed
- On visibility changed
- On customer status changed
- On new event
- On form submitted
- On rating submitted
- On greeting dismissed
- On rich message button clicked
On ready
The Chat Widget has finished loading. If the Chat Widget is already loaded the provided handler function will be called immediately. With this callback, you will receive the Chat Widget state and customer data as if requested by their getters.
Payload
state | WidgetState | the Chat Widget state |
customerData | CustomerData | Customer data from the Chat Widget |
On availability changed
Availability has changed for the current group.
Payload
availability | "online" | "offline" | Availability of a group |
On visibility changed
It is called once the visibility of the Chat Widget has changed. This applies to both user actions like maximizing or minimizing the window as well as hiding or showing the Chat Widget through the use of this API.
Payload
visibility | "maximized" | "minimized" | "hidden" | the Chat Widget visibility |
On customer status changed
It is called once the status of your customer has changed. This can be used to track the following info:
- If and when Customers are being invited to chat
- If Customers are already chatting
- If they are waiting for an agent to become available in the queue
status | 'queued' | 'chatting' | 'invited' |'browsing' | Customer's status |
On new event
It is called for both incoming and outgoing events.
Payload
timestamp | number | Event's send timestamp |
type | 'message' | 'rich_message' | 'file' | Event's type |
author.id | string | Event's author id |
author.type | 'customer' | 'agent' | Event's author type |
If new event is a greeting: | ||
greeting.id | number | Greeting's template id |
greeting.uniqueId | string | Greeting's event uniqueId |
On form submitted
It is called after a form has been submitted in the chat.
Payload:
type | "prechat" | "postchat" | "ticket" | Form's type |
On rating submitted
It is called after the customer has rated the chat, or cancelled the previous rating.
Payload
value | "good" | "bad" | "none" | Customer's rating value |
On greeting displayed
It is called after the greeting has been displayed to the customer.
Payload
id | number | Greeting's template id |
uniqueId | string | Greeting's event uniqueId |
On greeting hidden
It is called after the greeting has been cancelled by the customer or hide_greeting method.
Payload
id | number | Greeting's template id |
uniqueId | string | Greeting's event uniqueId |
On rich message button clicked
It is called after the rich message button has been clicked by the customer.
Payload
eventId | string | Button's owner event's id |
postbackId | string | Clicked button's postback id |
If button was included in greeting event: | ||
greeting.id | number | Greeting's template id |
greeting.uniqueId | string | Greeting's event uniqueId |
Playground
Here's where you can play with the Chat Widget JavaScript API in an interactive environment. Use the buttons and inputs on the left side of the Widget or try to invoke some functions directly in the console.
Examples
Here you can find some example usage of the Chat Widget JavaScript API. They all require the Widget to be installed on the page and window.LiveChatWidget to be defined.
Show the Widget after time
Show the Chat Widget after 30 seconds and keep it open after reloading.
Open the Widget using the button
Show the hidden or minimized Widget after a button has been clicked. You can change the Widget visibility as described in our Help Center
Prefill username and email
It sets the Customers name and email using their login and email.
Initialize Widget asynchronusly on button click
Initialize Chat Widget after clicking the button.