How to generate a hash for JavaScript API
The instructions below are for desktops and laptops only.
Using a hash with your JavaScript API calls is a great way to secure your customers’ data on your tawk.to chat widget.
A hash is a powerful security feature that encrypts your secret key and user ID into a string of characters that unauthorized parties cannot read.
These functions in our JavaScript API accept the hash as an attribute:
In this guide, we’ll show you how to get your secret key and generate the hash.
Follow these steps to get your secret key:
1. Log in to your tawk.to account.
2. On your dashboard, select the property you need the key for. Hover over the property name to ensure you’ve selected the correct one.
3. Click the gear icon on the top menu to go to the Administration panel.
4. On the Administration menu, select Overview.
5. Scroll down to JavaScript API.
6. Enable Secure Mode.
7. Click the following icon to copy the Key:
You’ll need this key for the next section where we generate the hash.
You can use any encryption service of your choice to generate a hash. In this example, we show you how to do this using Crypto.js. The relevant libraries are available at:
https://cryptojs.gitbook.io/docs
https://www.npmjs.com/package/crypto-js
Follow these steps to generate the hash:
1. Copy the following code into the <head> section of your website’s HTML:
2. Copy the following function into your JavaScript file. This function handles the generation of the hash value.
3. Replace secretKey with the key copied from your tawk.to dashboard.
If you’re using the hash with the login() function, please note:
After logging in, it may take up to a few seconds for your customer’s conversations to appear.
Error code | Description |
400 | Bad Request Error This indicates an error in your function’s attributes. Check your code and re-send the request. |
401 | Unauthorized Error This indicates that
Check your code and re-send the request. |
500 | Internal Server Error This indicates our servers are currently busy. Try re-sending the request. |
These error codes will appear in the developer’s console of your web browser.
That’s it! Now your customers’ data sent via API calls are encrypted, protecting them from unauthorized access.
Should you encounter any challenges or have further questions, please reach out to us.
If you have feedback about this article, or if you need more help:
Click the green live chat icon
Schedule a call with us
Visit our community