Setting a visitor’s name and email in the Pre-Chat Form using JavaScript API

[Desktop]

The tawk.to Javascript API makes it easy to deliver exceptional customer service by auto-populating a customer's name and email address in the Details tab, Pre-Chat Form, and Offline Form during a chat.

By default, all visitors are assigned unique IDs that start with the letter V and a number sequence. 

If you would like to replace the default ID and identify your customers in the chat, here are three ways you can set the name and email through the JavaScript API.

Note: To use the JavaScript API the visitor details must already be available through your system. This will not work for unknown visitors to your website.

JavaScript API provides a flexible set of methods for controlling the widget’s behavior and appearance on your website. 

1. Secure JavaScript API Disabled

With Secure JavaScript API Disabled and the user’s name and email available on widget load, use the following script:

2. Secure JavaScript API Enabled

With Secure JavaScript API Enabled and the user’s name and email available on widget load, use the following script:

Note: The hash value can be computed using the following PHP example:

3. Single-page app (website) or AJAX login

If you have a single-page app (website) or AJAX login, use the setAttribute function. For this, you will need to enable Secure JavaScript API and add the calculated hash value. Here’s the script you can use:

Note: The hash value can be computed using the following PHP example:

In addition to setting a visitor’s name and email, the tawk.to JavaScript API allows you to automate processes at the beginning or end of a chat.

You can learn more about enabling JavaScript API here: JavaScript API.


In the event that a pre-existing contact is added a second time through an automation, you can merge the two contacts into one profile.


Learn how to merge two or more contacts here: How to merge contacts


Need more help? Reach out to our support team or schedule a call

Was this article helpful?

99 out of 163 liked this article

Still need help? Message Us