How to use setAttributes() with a hash in JavaScript

The instructions below are for desktops and laptops only.

You can use the setAttributes() function in our API to modify customers’ details such as name and email address via your tawk.to chat widget.


Using a hash with the setAttributes() function is a great way to protect customers' data. A hash is a powerful security feature that is an encrypted combination of your secret key and user ID.


In this guide, we’ll show you how to get your secret key, generate a hash, and use it with the setAttributes() function in our API

First, get the secret key

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.

Generate the hash

The hash is generated by a service that accepts a combination of your secret key and user ID and encrypts them into a string of characters that unauthorized parties cannot read.


You can use any encryption service of your choice to generate the 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.

Next, let’s use the hash with the setAttributes() function.

Using setAttributes() with hash

Follow these steps to use setAttributes() with hash:


1. Copy the setAttributes() function from our JavaScript API into your API schema.

Note: If you’re using the phone property with setAttributes(), ensure the phone number follows the international E.164 format (e.g., +1234567890).

2. Copy the hash you generated into the hash field.

3. Ensure the value in the userID field matches the user ID used in the encryption steps above.

Note: It may take up to a few seconds for your customers’ data to be updated.

Error codes

These are the possible error codes you might encounter:

Error codeDescription
400Bad Request Error
This indicates an error in your function’s attributes. Check your code and re-send the request.
401Unauthorized Error
This indicates that
  • The user ID doesn’t match the encrypted hash value, or
  • Secure Mode isn’t enable in your tawk.to dashboard.

Check your code and re-send the request.
500Internal 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.

With these steps, you’ve further strengthened the security of your tawk.to chat widget, giving your customers a safer, more robust experience.

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:

Was this article helpful?

3 out of 5 liked this article

Still need help? Message Us