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.
Tawk_API = Tawk_API || {};
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.
Tawk_API = Tawk_API || {};
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.
Tawk_API = Tawk_API || {};
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.
Tawk_API = Tawk_API || {};
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.
Tawk_API = Tawk_API || {};
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.
Tawk_API = Tawk_API || {};
Tawk_API.onChatHidden = function(){
//place your code here
};
Callback function invoked when the widget is started.
Tawk_API = Tawk_API || {};
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.
Tawk_API = Tawk_API || {};
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.
Tawk_API = Tawk_API || {};
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.
Tawk_API = Tawk_API || {};
Tawk_API.onOfflineSubmit = function(data){
//place your code here
};
Tawk_API = Tawk_API || {};
Tawk_API.visitor = {
name : 'Name',
email : 'email@email.com'
};
Maximizes the chat widget.
Tawk_API.maximize();
//Example
Tawk_API.onLoad = function(){
Tawk_API.maximize();
};
Minimizes the chat widget.
Tawk_API.minimize();
//Example
Tawk_API.onLoad = function(){
Tawk_API.minimize();
};
Minimizes or Maximizes the chat widget based on the current state.
Tawk_API.toggle();
//Example
Tawk_API.onLoad = function(){
Tawk_API.toggle();
};
Opens the chat widget as a pop out.
Tawk_API.popup();
//Example
Tawk_API.onLoad = function(){
Tawk_API.popup();
};
Returns the current widget type whether it’s inline or embed.
Tawk_API.getWindowType();
//Example
Tawk_API.onLoad = function(){
if(Tawk_API.getWindowType() === 'inline'){
// do something if it's inline
}else{
// do something if it's embed
}
};
Shows the chat widget.
Tawk_API.showWidget();
//Example
Tawk_API.onLoad = function(){
Tawk_API.showWidget();
};
Hides the chat widget.
Tawk_API.hideWidget();
//Example
Tawk_API.onLoad = function(){
Tawk_API.hideWidget();
};
Hides or Shows the chat widget based on the current visibility state.
Tawk_API.toggleVisibility();
//Example
Tawk_API.onLoad = function(){
Tawk_API.toggleVisibility();
};
Returns the current page status (online, away or offline).
Tawk_API.getStatus();
//Example
Tawk_API.onLoad = function(){
var pageStatus = Tawk_API.getStatus();
if(pageStatus === 'online'){
// do something for online
}else if(pageStatus === 'away'){
//do something for away
}else{
// do something for offline
}
};
Tawk_API.isChatMaximized();
//Example
Tawk_API.onLoad = function(){
if(Tawk_API.isChatMaximized()){
//do something if it's maximized
}
};
Tawk_API.isChatMinimized();
//Example
Tawk_API.onLoad = function(){
if(Tawk_API.isChatMinimized()){
// do something if it's minimized
}
};
Tawk_API.isChatHidden();
//Example
Tawk_API.onLoad = function(){
if(Tawk_API.isChatHidden()){
// do something if chat widget is hidden
}
};
Tawk_API.isChatOngoing();
//Example
Tawk_API.onLoad = function(){
if(Tawk_API.isChatOngoing()){
//do something if there's ongoing chat
}
};
Tawk_API.isVisitorEngaged();
//Example
Tawk_API.onLoad = function(){
if(Tawk_API.isVisitorEngaged()){
// do something if visitor is engaged in chat
}
};
Ends the current ongoing chat.
Tawk_API.endChat();
//Example
Tawk_API.onLoad = function(){
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:
Tawk_API.setAttributes(attributes, callback);
//Example
Tawk_API.onLoad = function(){
Tawk_API.setAttributes({
'id' : 'A1234',
'store' : 'Midvalley'
}, function(error){});
};
//Example for setting name and email
Tawk_API.onLoad = function(){
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
Tawk_API.addEvent(eventName, metadata, callback);
//Example
Tawk_API.onLoad = function(){
Tawk_API.addEvent('requested-quotation', function(error){});
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
Tawk_API.addTags(tags, callback);
//Example
Tawk_API.onLoad = function(){
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
Tawk_API.removeTags(tags, callback);
//Example
Tawk_API.onLoad = function(){
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.
Tawk_API = Tawk_API || {};
Tawk_API.visitor = {
name : 'Name',
email : 'email@email.com',
hash : '<calculate-hash>'
};
// Here is an example showing how you could do it using PHP
Tawk_API = Tawk_API || {};
Tawk_API.visitor = {
name : '<?php echo $user->name; ?>',
email : '<?php echo $user->email; ?>',
hash : '<?php echo hash_hmac("sha256", $user->email, "<API-KEY>"); ?>'
};
© 2020 tawk.to inc. All Rights Reserved.