Welcome to the tawk.to JavaScript API documentation.
The API provides a flexible set of methods that can be used in your web projects. To invoke one of the methods below, please be sure to call a method after the embed code on your page.
Use the JavaScript API to manipulate the chat widget displayed on your website.
Popular use-cases:
We are always excited to see how you use the API, so feel free to share your creations with us!
Callback function invoked right after the widget is rendered. This callback is not supported in pop out chat window.
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onLoad = function(){
//place your code here
};
Callback function invoked when the page status changes. The function will receive the changed status which will be either online, away or offline. This callback is not supported in pop out chat window.
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onStatusChange = function(status){
//place your code here
};
Callback function invoked right when Tawk_API is ready to be used and before the widget is rendered. This callback is not supported in pop out chat window.
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onBeforeLoad = function(){
//place your code here
};
Callback function invoked when the widget is maximized. This callback is not supported in pop out chat window.
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onChatMaximized = function(){
//place your code here
};
Callback function invoked when the widget is minimized. This callback is not supported in pop out chat window.
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onChatMinimized = function(){
//place your code here
};
Callback function invoked when the widget is hidden. This callback is not supported in pop out chat window.
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onChatHidden = function(){
//place your code here
};
Callback function invoked when the widget is started.
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onChatStarted = function(){
//place your code here
};
Callback function invoked when the widget is ended. This callback is not supported in pop out chat window.
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onChatEnded = function(){
//place your code here
};
Callback function invoked when the Pre-Chat Form is submitted. The submitted form data is passed to the function. This callback is not supported in pop out chat window.
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onPrechatSubmit = function(data){
//place your code here
};
Callback function invoked when the Offline form is submitted. The submitted form data is passed to the callback. Form data will contain {name : ”, email : ”, message : ”, questions : []}. This callback is not supported in pop out chat window.
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onOfflineSubmit = function(data){
//place your code here
};
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onChatMessageVisitor = function(message){
//place your code here
};
Callback function invoked when message is sent by the agent. The message is passed to the callback. This callback is not supported in pop out chat window.
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onChatMessageAgent = function(message){
//place your code here
};
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onChatMessageSystem = function(message){
//place your code here
};
Callback function invoked when an agent joins the chat. The data is passed to the callback. Will contain {name : ”, position : ”, image : ”, id : ”}. This callback is not supported in pop out chat window.
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onAgentJoinChat = function(data){
//place your code here
};
Callback function invoked when an agent leaves the chat. The data is passed to the callback. Will contain {name : ”, id : ”}. This callback is not supported in pop out chat window.
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onAgentLeaveChat = function(data){
//place your code here
};
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onChatSatisfaction = function(statisfaction){
//place your code here
};
Callback function invoked when the visitor manually changes his name. The visitorName is passed to the callback. This callback is not supported in pop out chat window.
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onVisitorNameChanged = function(visitorName){
//place your code here
};
Callback function invoked when a file is uploaded. The link to the uploaded file is passed to the callback. This callback is not supported in pop out chat window.
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onFileUpload = function(link){
//place your code here
};
Callback function invoked when a tag is updated.
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.onTagsUpdated = function(data){
//place your code here
};
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.visitor = {
name : 'Name',
email : 'email@email.com'
};
Maximizes the chat widget.
window.Tawk_API.maximize();
//Example
window.Tawk_API.onLoad = function(){
window.Tawk_API.maximize();
};
Minimizes the chat widget.
window.Tawk_API.minimize();
//Example
window.Tawk_API.onLoad = function(){
window.Tawk_API.minimize();
};
Minimizes or Maximizes the chat widget based on the current state.
window.Tawk_API.toggle();
//Example
window.Tawk_API.onLoad = function(){
window.Tawk_API.toggle();
};
Opens the chat widget as a pop out.
window.Tawk_API.popup();
//Example
window.Tawk_API.onLoad = function(){
window.Tawk_API.popup();
};
Returns the current widget type whether it’s inline or embed.
window.Tawk_API.getWindowType();
//Example
window.Tawk_API.onLoad = function(){
if(window.Tawk_API.getWindowType() === 'inline'){
// do something if it's inline
}else{
// do something if it's embed
}
};
Shows the chat widget.
window.Tawk_API.showWidget();
//Example
window.Tawk_API.onLoad = function(){
window.Tawk_API.showWidget();
};
Hides the chat widget.
window.Tawk_API.hideWidget();
//Example
window.Tawk_API.onLoad = function(){
window.Tawk_API.hideWidget();
};
Hides or Shows the chat widget based on the current visibility state.
window.Tawk_API.toggleVisibility();
//Example
window.Tawk_API.onLoad = function(){
window.Tawk_API.toggleVisibility();
};
Returns the current page status (online, away or offline).
window.Tawk_API.getStatus();
//Example
window.Tawk_API.onLoad = function(){
var pageStatus = window.Tawk_API.getStatus();
if(pageStatus === 'online'){
// do something for online
}else if(pageStatus === 'away'){
//do something for away
}else{
// do something for offline
}
};
window.Tawk_API.isChatMaximized();
//Example
window.Tawk_API.onLoad = function(){
if(window.Tawk_API.isChatMaximized()){
//do something if it's maximized
}
};
window.Tawk_API.isChatMinimized();
//Example
window.Tawk_API.onLoad = function(){
if(window.Tawk_API.isChatMinimized()){
// do something if it's minimized
}
};
window.Tawk_API.isChatHidden();
//Example
Tawk_API.onLoad = function(){
if(window.Tawk_API.isChatHidden()){
// do something if chat widget is hidden
}
};
window.Tawk_API.isChatOngoing();
//Example
Tawk_API.onLoad = function(){
if(window.Tawk_API.isChatOngoing()){
//do something if there's ongoing chat
}
};
window.Tawk_API.isVisitorEngaged();
//Example
window.Tawk_API.onLoad = function(){
if(window.Tawk_API.isVisitorEngaged()){
// do something if visitor is engaged in chat
}
};
Ends the current ongoing chat.
window.Tawk_API.endChat();
//Example
window.Tawk_API.onLoad = function(){
window.Tawk_API.endChat();
};
Set custom metadata regarding this chat/visitor.
This function takes in two values: attribute and callback.
The attribute value is of the object data type, which is a key value pair.
The key is of the string data type and can contain only alphanumeric characters and ‘-‘ (dash).
You can also use this function to set the visitor name and email. However, you will need to enable the secure mode first and also supply the calculated hash value in this function.
Refer to the secure mode section below on how to do this.
The reason it needs to be in secure mode is to ensure data integrity — to ensure the value sent from the widget to the dashboard is true and has not been tampered with.
The callback, which is a function, will be invoked to notify whether the save failed.
Error messages returned:
window.Tawk_API.setAttributes(attributes, callback);
//Example
window.Tawk_API.onLoad = function(){
window.Tawk_API.setAttributes({
'id' : 'A1234',
'store' : 'Midvalley'
}, function(error){});
};
//Example for setting name and email
window.Tawk_API.onLoad = function(){
window.Tawk_API.setAttributes({
'name' : 'Name',
'email' : 'email@email.com',
'hash' : 'hash value'
}, function(error){});
}
Set a custom event to chat.
This function takes in 3 values: event name, optional metadata and callback.
The event name is of the string data type and can contain only alphanumeric characters and ‘-‘ (dash)
The callback which is a function will be invoked to notify whether the save failed.
INVALID_EVENT_NAME, INVALID_ATTRIBUTES, ATTRIBUTE_LIMIT_EXCEEDED, CONTAINS_INVALID_KEY, CONTAINS_INVALID_VALUE, SESSION_EXPIRED, SERVER_ERROR
window.Tawk_API.addEvent(eventName, metadata, callback);
//Example
window.Tawk_API.onLoad = function(){
window.Tawk_API.addEvent('requested-quotation', function(error){});
window.Tawk_API.addEvent('product-add-to-cart', {
'sku' : 'A0012',
'name' : 'Jeans',
'price' :'50'
}, function(error){});
};
Add tags to the chat.
This function takes in two values; tags and callback.
This is of the array data type.
The content of the tags should be of the string data type.
The total number of tags is 10.
The callback, which is a function, will be invoked to notify whether the save failed.
INVALID_TAGS, TAG_LIMIT_EXCEEDED, VERSION_CONFLICT, SESSION_EXPIRED, SERVER_ERROR
window.Tawk_API.addTags(tags, callback);
//Example
window.Tawk_API.onLoad = function(){
window.Tawk_API.addTags(['hello', 'world'], function(error){});
};
Remove tags from the chat.
This function takes in two values: tags and callback.
This is of the array data type.
The content of the tags should be of the string data type.
The callback, which is a function, will be invoked to notify whether the save failed.
INVALID_TAGS, TAG_LIMIT_EXCEEDED, SESSION_EXPIRED, SERVER_ERROR
window.Tawk_API.removeTags(tags, callback);
//Example
window.Tawk_API.onLoad = function(){
window.Tawk_API.removeTags(['hello', 'world'], function(error){});
};
Secure method is to ensure the data you are sending is actually from you.
To enable secure mode, embed following code on your page.
The hash is server side generated HMAC using SHA256, the user’s email and your site’s API key.
You can get your API key from Admin>Property Settings.
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.visitor = {
name : 'Name',
email : 'email@email.com',
hash : '<calculate-hash>'
};
// Here is an example showing how you could do it using PHP
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.visitor = {
name : '<?php echo $user->name; ?>',
email : '<?php echo $user->email; ?>',
hash : '<?php echo hash_hmac("sha256", $user->email, "<API-KEY>"); ?>'
};
Object used to update the widget styling. Currently only supports zIndex style. Do not place this object in a function, as the values need to be available before the widget script is downloaded. Setting or changing the values after the widget script has been downloaded will not update the widget’s style.
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.customStyle = {
zIndex : Integer|String
};
//Example
window.Tawk_API = window.Tawk_API || {};
window.Tawk_API.customStyle = {
zIndex : 1000
};
window.Tawk_API.customStyle = {
zIndex : '1000'
};
window.Tawk_API.customStyle = {
zIndex : '1000 !important'
};