Identify your customers from your website
If your website already has its customers signed in, the Web Chat recognizes them on its own, without asking for an email or a code.
It's for websites with their own sign-in: a store, an app, a members area. Your server already knows who the customer is and tells the chat with a signature. The chat signs them in on its own, and the customer sees their usual conversation on any device where they have your website open.
The signature is a JWT token with HS256, signed with that Web Chat's secret key. You create the key in Developers, in the 'Web Chat' tab, and it's shown only once: store it on your website's server, never in the page's code.
On every page, your server signs a token that expires in a few minutes and puts it in the chat code, in data-user. If the customer isn't signed in on your website, data-user is empty: that way, when they sign out of your website, they also leave the chat.
The contact is created when the customer writes for the first time, not when they open your website: each visit doesn't fill up your contact list. It arrives with their name, email, phone and your custom fields, and their customer number goes in the webchat_usuario_id field.
It works even if visitor sign-in is 'Off'. Anyone your website doesn't identify carries on as always: anonymous, or signing in with their email or WhatsApp if you have it on.
What you can do
- Recognize your customers in the chat without asking them for anything.
- Let your customer continue the same conversation on their phone and their computer.
- Send their name, email, phone and your custom fields to their contact record.
- Look up your customer in your system from a flow, with webchat_usuario_id.
- Change the key without cutting anyone off: the previous one stays valid for 24 more hours.
- Test a token in Developers and see exactly what's wrong.
What the token carries
| Field | What it is |
|---|---|
| sub | Required. Your customer's identifier on your website, up to 128 characters. Send it as text: a number with more than 15 digits arrives rounded and is rejected. |
| iat and exp | Required. When it was signed and when it expires: at most one hour between them. We recommend 5 minutes. |
| aud | Optional. This Web Chat's key (wc_…): that way the token isn't valid for another chat. |
| name | Optional. Their name. It doesn't overwrite one someone on your team changed by hand. |
| Optional. Their email. It's added if the contact didn't have one. | |
| email_verified | Optional. true only if your website verified that email: the first time that customer signs in, it's joined with the conversation that email already had, unless it belongs to another customer of your website. |
| phone_number | Optional. Their phone with the country code. It's added if the contact didn't have one. |
Sign on your server (Node.js)
// npm install jsonwebtoken
import jwt from "jsonwebtoken";
const user = customer
? jwt.sign(
{ sub: String(customer.id), name: customer.name, email: customer.email, email_verified: Boolean(customer.emailVerified) },
process.env.WEBCHAT_SECRET,
{ algorithm: "HS256", expiresIn: "5m", audience: "wc_tu_clave" },
)
: "";Put it in the chat code
<script src="https://platform.chatlevel.ai/api/webchat/widget" data-key="wc_tu_clave" data-user="<%= user %>" async></script>Not signed in on your website
<script src="https://platform.chatlevel.ai/api/webchat/widget" data-key="wc_tu_clave" data-user="" async></script>Single-page apps
window.postMessage({ type: "webchat:user", token: user }, location.origin);
window.postMessage({ type: "webchat:logout" }, location.origin);
window.addEventListener("webchat:user-needed", async (e) => {
const user = await fetchChatToken();
window.postMessage({ type: "webchat:user", token: user, key: e.detail.key }, location.origin);
});With the iframe
<iframe src="https://platform.chatlevel.ai/webchat/marco?key=wc_tu_clave#user=<%= user %>" title="Chat"></iframe>
iframe.contentWindow.postMessage({ type: "webchat:logout" }, "https://platform.chatlevel.ai");
window.addEventListener("message", async (e) => {
if (e.origin !== "https://platform.chatlevel.ai" || e.data?.type !== "webchat:user-needed") return;
iframe.contentWindow.postMessage({ type: "webchat:user", token: await fetchChatToken() }, "https://platform.chatlevel.ai");
});An example
An online store with its customer area
- Ana shops at the store and signs in with her account.
- On every page, the store's server signs who she is: her customer number, her name and her email.
- Ana opens the chat on her phone and asks about her order: the chat doesn't ask her for anything, it already knows who she is.
- That night she opens it on her computer and continues the same conversation.
- In your inbox, her contact record has her customer number, and your flow uses it to look up the order in the store.
Recommendations
- Sign a new token on every page, valid for 5 minutes. Don't cache pages with a session: a cached page with one customer's token would be served to others.
- On pages cached for everyone, don't add data-user. Leave it empty only on pages generated for each visit, like the one after signing out: otherwise every visit to a public page would sign your customer out of the chat.
- On single-page websites, listen for webchat:user-needed: the chat asks for it when it starts without data-user and when the session expires. In the embedded frame it arrives as a message, and changing the #user= in its address is enough. With two chats on the same page, send key in the message.
- Send email_verified only if your website really verified the email, for example with a link or a code.
- Set aud to your Web Chat's key: that way a token doesn't work in another chat.
- Before going live, test what your website generates in 'Test a token', in Developers.
- Change the key if it was ever shared: the previous one stays valid for 24 hours.
Watch out for this
- Whoever has the secret key can sign in as any of your customers in that chat. Keep it only on your server: never in the page's code or in an app.
- If your website sends email_verified without having verified it, someone could get into another person's conversation with their email.
- If a page with the token is served to many people because of a cache, the chat notices and doesn't let anyone in with that token.
- The chat session lasts at most 12 hours without a new signature from your website. On a shared computer, leave data-user empty when signing out.
- Removing the key signs out everyone who got in that way, right away. People who signed in with their email or WhatsApp stay signed in.
- Disconnecting the Web Chat also removes its key. If you connect it again, create a new one and put it on your website.
