Identifying visitors
Link a visitor to a contact in SupportHub. There are three ways to do it, depending on what you know about the user and when.
Why you need it
By default a visitor is anonymous: the backend recognizes them by a random vs_* ID that the widget keeps in a cookie, localStorage, sessionStorage and IndexedDB (see multi-tier storage). If your site already knows the user (they’re signed in to your customer account, CRM or app), identify writes their email, name and phone to the contact, and the operator sees them in the contact card right away. To get one user’s conversations from different devices into one contact, you need a visitor_token (method 3).
Three ways
| Method | When to use |
|---|---|
| data-* attributes | Your server renders the page and already knows the user (Blade / EJS / Twig). Just put the email and name into the template. |
| window.SupportHub.identify() | An SPA where the user signs in after the page loads. Call it from your sign-in handler. |
| visitor_token | One contact across devices: the user writes from a phone and from a laptop. Your backend signs the token with HMAC. |
Method 1: data-* attributes
If your backend renders the page when the user is already known, put the data straight into the <script> tag. The widget reads the attributes on load and sends them to POST /api/webhooks/widget/{workspace_id}/identify, provided at least one of the email, name, phone or Telegram ID is set.
Important: the attributes are read from the <script> element that loads the bundle. If you install the widget with the loader from the “Embed snippet”, set them on the element it creates, the same way the loader already sets data-visitor-token: s.setAttribute('data-email', user.email).
<script
async
src="https://supporthub.cc/widget-bundle?ws=YOUR_WORKSPACE_UUID"
data-email="{{ user.email }}"
data-name="{{ user.full_name }}"
data-phone="{{ user.phone }}"
data-telegram-id="{{ user.telegram_id }}"
></script>Available data-* fields
vs_* ID, not by email: the same email from another browser gives a separate contact. For one contact across devices, use a visitor_token.full_name. It’s written only if the contact has no name yet or has a placeholder one (“Visitor …”, “Guest”).+79161234567). Saved to extra_data.phone if the contact has no phone yet.| Field | Type | Default | Description |
|---|---|---|---|
| data-email | string | — | The visitor’s email. Every identify overwrites the contact’s email, except for a contact BILLmanager has bound an account to: its address is left as it is. Such an address counts as checked only while it is the one BILLmanager itself gave. The backend finds an anonymous visitor by the vs_* ID, not by email: the same email from another browser gives a separate contact. For one contact across devices, use a visitor_token. |
| data-name | string | — | The full name, saved as full_name. It’s written only if the contact has no name yet or has a placeholder one (“Visitor …”, “Guest”). |
| data-phone | string | — | Phone number (E.164 is best: +79161234567). Saved to extra_data.phone if the contact has no phone yet. |
| data-telegram-id | int | — | Numeric Telegram ID. Written to the contact if it has no Telegram ID yet. The backend never uses it to find or merge contacts: a contact from your Telegram bot with the same ID stays separate — see “What happens on the backend” below. |
| data-visitor-token | string (HMAC) | — | An HMAC token from your backend, for cross-device identification (see method 3). |
Method 2: window.SupportHub.identify()
In an SPA, where the user signs in after the page loads, call the JS API. The method is available as soon as the bundle loads, so there’s no need to wait for init(): calls made before initialization finishes are queued and sent once the widget starts. It accepts email, name (or full_name), phone, telegram_id and visitor_token.
// onLoginSuccess callback
window.SupportHub.identify({
email: user.email,
name: user.fullName,
phone: user.phone,
telegram_id: user.telegramId,
});How it behaves
- If the call has at least one field (email, name, phone or Telegram ID), the widget sends it to the backend. The contact is created if it doesn’t exist yet, even if the visitor hasn’t sent a single message.
- If the contact already exists, the email is overwritten on every call, except for a contact with a BILLmanager account bound to it: its address is left as it is, and the request doesn’t fail. The name is written only if there’s none or it’s a placeholder, and the phone and Telegram ID only if they’re still empty. Empty fields don’t overwrite anything.
- Every call with data is a request to the backend (at most 20 per minute per visitor) and a
contact.identifiedevent. If it includes an email and the visitor has an open ticket, it also adds a row to the ticket timeline noting that the client introduced themselves. So call identify when the user signs in or their details change, not on every route change. - Errors aren’t thrown: the promise always resolves. If a request fails, the data stays queued and goes out with the next identify() call on the page. After a reload, the data-* attributes or your identify() call send it again.
Method 3: visitor_token (cross-device)
When the same user comes from a phone and a laptop, the anonymous vs_* ID won’t link them: they’re different browsers and different contacts. To get conversations from both devices into one contact, generate an HMAC-signed token with the user_id on your backend and pass it in data-visitor-token or SupportHub.identify({visitor_token}). The full reference is on the HMAC visitor token page.
Where to get the secret
The project secret (widget_signing_secret) is created on the first request and returned by the public API. You need an API key from SettingsAPI Keys:
GET /api/v1/widget/signing-secret
Authorization: Bearer sk_xxx
200 OK
{
"secret": "a random 43-character string"
}The project comes from the API key. A project admin sees the same secret in SettingsChannelsWeb Widget. The secret doesn’t change on its own (a repeated GET returns the same value) until someone rotates it with the “Rotate secret” button there. Keep it on your backend only: anyone who has it can sign a token for any user_id. How to rotate it if it may have leaked is on the HMAC visitor token page.
How to generate a token (Python)
import hmac, hashlib, base64, json, time
# Get the secret once via GET /api/v1/widget/signing-secret
# and keep it in env. Never expose it to the client.
SECRET = "PASTE_SECRET_HERE"
def visitor_token(user_id: str, ttl: int = 86400) -> str:
payload = {
"user_id": user_id,
"exp": int(time.time()) + ttl,
}
payload_bytes = json.dumps(payload).encode()
payload_b64 = base64.urlsafe_b64encode(payload_bytes).rstrip(b"=").decode()
# IMPORTANT: the HMAC is computed over the raw JSON bytes (not the base64),
# then the base64 part and the hex signature are joined with a dot.
sig = hmac.new(SECRET.encode(), payload_bytes, hashlib.sha256).hexdigest()
return f"{payload_b64}.{sig}"How to generate a token (Node.js)
import crypto from "node:crypto";
const SECRET = process.env.SUPPORTHUB_SIGNING_SECRET;
export function visitorToken(userId, ttl = 86400) {
const payload = { user_id: userId, exp: Math.floor(Date.now() / 1000) + ttl };
const payloadBytes = Buffer.from(JSON.stringify(payload));
const payloadB64 = payloadBytes.toString("base64url");
// HMAC over the raw JSON bytes, NOT the base64 string.
const sig = crypto
.createHmac("sha256", SECRET)
.update(payloadBytes)
.digest("hex");
return `${payloadB64}.${sig}`;
}Endpoint on your side: FastAPI/Python example
import base64, hashlib, hmac, json, time, os
from fastapi import APIRouter, Depends, HTTPException
router = APIRouter()
# Get the secret once via GET /api/v1/widget/signing-secret
# from SupportHub and store it in env / settings.
SUPPORTHUB_SIGNING_SECRET = os.environ["SUPPORTHUB_SIGNING_SECRET"]
@router.get("/token")
async def supporthub_widget_token(current_user = Depends(get_current_user)):
"""Returns an HMAC token <payload_b64>.<sig> for the cross-device widget."""
user_id = str(current_user.id)
if not user_id:
raise HTTPException(401, "Unauthorized")
# Compact JSON: separators=(",", ":") drops the extra spaces.
# The HMAC is computed over the raw JSON bytes (NOT the base64).
payload = json.dumps(
{"user_id": user_id, "exp": int(time.time()) + 3600},
separators=(",", ":"),
)
sig = hmac.new(
SUPPORTHUB_SIGNING_SECRET.encode(),
payload.encode(),
hashlib.sha256,
).hexdigest()
payload_b64 = (
base64.urlsafe_b64encode(payload.encode()).decode().rstrip("=")
)
return {
"token": f"{payload_b64}.{sig}",
"expires_in": 3600,
}One-script install with an automatic token
A single <script> in <body>: it first requests a token from your /token endpoint, then loads the widget with that token. If there’s no token (a guest, a 401, a network error), the widget loads without it, in anonymous mode.
<script>
(function(w, d){
function loadWidget(token){
var s = d.createElement('script');
s.async = 1;
s.src = 'https://supporthub.cc/widget-bundle?ws=YOUR_WORKSPACE_UUID';
if (token) s.setAttribute('data-visitor-token', token);
d.head.appendChild(s);
}
fetch('/api/supporthub/token', { credentials: 'include' })
.then(function(r){ return r.ok ? r.json() : null; })
.then(function(data){ loadWidget(data && data.token); })
.catch(function(){ loadWidget(); });
})(window, document);
</script>Pass it to the widget
<script
async
src="https://supporthub.cc/widget-bundle?ws=YOUR_WORKSPACE_UUID"
data-visitor-token="<%= visitor_token(user.id) %>"
></script>When the token expires: SupportHub.on('token_expired')
If the server refuses the token (its exp has passed or the signature doesn’t match), the widget stops reconnecting with it, shows the visitor “Session expired — refresh the page” and calls the token_expired handler once per token with { reason: "expired" | "invalid" }. Fetch a fresh token there and pass it to identify: the conversation comes back by itself, no page reload. SupportHub.on is there as soon as the bundle has run, like identify, before the widget has started. The onTokenExpired option of init() does the same.
SupportHub.on("token_expired", async function (e) {
// e.reason: "expired" | "invalid"
const r = await fetch("/api/supporthub/token", { credentials: "include" });
if (!r.ok) return;
const { token } = await r.json();
await SupportHub.identify({ visitor_token: token });
});When the user signs out: SupportHub.logout()
When the user signs out of your site, call window.SupportHub.logout(). Without it, a page that doesn’t reload on sign-out (an SPA) keeps showing the previous user’s conversation in the widget, because their token stays in the page’s memory, and the next person at that computer sees it.
// onLogout in your SPA, where you clear your own session
await window.SupportHub.logout();logout():
- Forgets the
visitor_tokenand everything that came fromidentify()and the data-* attributes: the email, name, phone and Telegram ID. - Replaces the anonymous
vs_*ID with a new one in every store (cookie, localStorage, sessionStorage, IndexedDB), so this browser’s earlier anonymous conversation no longer opens in the widget. - Closes the WebSocket, takes the widget back to the home tab and closes the panel.
- Is available as soon as the bundle loads, like
identify(): a call made before initialization finishes works too — the widget applies it as it starts.
The promise always resolves. Nothing is deleted on the server: the conversation stays with the user and opens again when they sign in and the page passes their token. Other open tabs keep the token in memory until they reload. If your site doesn’t reload them, call logout() there too.
If another user signs in on this browser and logout() wasn’t called, identify() with a token for a different user_id starts from a clean anonymous session by itself: the previous user’s contact and conversation never go to the new one.
Signed-in visitors only: data-require-auth
If only people signed in to your site (a client area, say) should use the widget, add data-require-auth to the embed code. Until your site passes a signed visitor token, the widget shows “Sign in first” instead of the chat, help and news. Once the token arrives — through data-visitor-token or SupportHub.identify({ visitor_token }) — the widget opens up by itself, without a page reload. SupportHub.logout() locks it again.
data-auth-url adds a “Sign in” button under the text, linking to your sign-in page: a full http(s) address or a path on the site. Without it, the text only.
<script
src="https://supporthub.cc/widget-bundle?ws=YOUR_WORKSPACE_UUID"
data-require-auth
data-auth-url="/login"
async
></script>Identifying by a token address: data-identify-url
If your site already has an address that gives a signed-in user their visitor token (in a client area, say), the widget can ask it by itself, with no script of yours on the page. Put the address in data-identify-url: as it loads, the widget requests it with the visitor’s cookies (credentials: "include") and the X-SupportHub-Widget: 1 header, and passes the token, email and name it gets to identify().
The response is the JSON { "token": "…", "exp": 1767225600, "email": "…", "name": "…" } when the user is signed in, and { "error": "…" } when they aren’t. The widget reads the body only, not the status code: that’s how the BILLmanager module answers too, always with 200. An error, something that isn’t JSON or an unreachable address leaves the visitor anonymous, with no error shown.
Two errors mean “nobody is signed in here”: { "error": "no_session" } (no live session) and { "error": "not_a_client" } (whoever is signed in isn’t a client: staff, a guest without an account). If this address identified the visitor, on this page or an earlier one in this browser, the widget signs them out as SupportHub.logout() does: the next person at this browser starts from a fresh anonymous session. So a client who signs out of the client area doesn’t leave their session on the sign-in page. A visitor the page identified itself, and an anonymous visitor the address never identified, aren’t affected. Other errors (not_from_cabinet, no_widget_secret, an unreachable address) say nothing about who is signed in and change nothing.
- While the page is open, the widget fetches a new token 5 minutes before
exp, when the visitor comes back to the tab with an expired token, and when the server refuses the token as expired. - While the address hasn’t identified anyone, the widget asks it again when the visitor opens the chat and when they come back to the tab. This catches a sign-in the page wasn’t reloaded after: a SPA’s sign-in form, a sign-in in another tab. Without it the visitor would write as a guest.
- A token the page passes itself (
data-visitor-tokenorSupportHub.identify({ visitor_token })) takes precedence: after it, the address isn’t asked again. - If the address returns another user’s token, the widget starts from a clean anonymous session, as with
identify(). AfterSupportHub.logout(), the address isn’t asked again until the page reloads. - With
data-require-auth, the widget opens up as soon as the token arrives. - The address is a full http(s) URL or a path on the site. It isn’t requested in the builder preview. With manual initialization, use
SupportHub.init({ identifyUrl: "…" }).
This makes your site and the client area on a subdomain one visitor for your support. In the client area the address is a path on its own host. On the landing page it’s the client area’s full address, so a client signed in there is recognized on the landing page too. data-cookie-domain="auto" on both (or once in Widget BuilderAdvancedCookie domainAll subdomains — automatically) shares the anonymous session as well: a conversation started on the landing page before signing in carries on in the client area.
<!-- Client area: my.example.com -->
<script
src="https://supporthub.cc/widget-bundle?ws=YOUR_WORKSPACE_UUID"
data-identify-url="/mancgi/supporthub_widget_token"
data-cookie-domain="auto"
async
></script>
<!-- Landing page: example.com -->
<script
src="https://supporthub.cc/widget-bundle?ws=YOUR_WORKSPACE_UUID"
data-identify-url="https://my.example.com/mancgi/supporthub_widget_token"
data-cookie-domain="auto"
async
></script>location = /mancgi/supporthub_widget_token {
if ($request_method = OPTIONS) {
add_header Access-Control-Allow-Origin "https://example.com" always;
add_header Access-Control-Allow-Credentials "true" always;
add_header Access-Control-Allow-Methods "GET" always;
add_header Access-Control-Allow-Headers "X-SupportHub-Widget" always;
add_header Access-Control-Max-Age "600" always;
add_header Vary "Origin" always;
return 204;
}
add_header Access-Control-Allow-Origin "https://example.com" always;
add_header Access-Control-Allow-Credentials "true" always;
add_header Vary "Origin" always;
# proxy_pass and proxy_set_header as before
}What happens on the backend
The widget sends this request after the page loads (if the data-* attributes carry any data) and on every SupportHub.identify(...) call with data. With a token, &visitor_token=… is added to the URL.
POST /api/webhooks/widget/{workspace_id}/identify?visitor_id=vs_xxx
Content-Type: application/json
{
"email": "user@example.com",
"full_name": "John Smith",
"phone": "+79161234567",
"telegram_id": 123456789
}The backend then:
- Finds the contact: without a token, this
visitor_id’s anonymous contact (Contact.internal_id), and with a validvisitor_token, by the token’suser_idinContact.verified_user_id. A contact already bound to auser_idisn’t found byvisitor_idalone: only that user’s token opens it. - If there’s no contact for the token yet, binds this
visitor_id’s anonymous contact to theuser_id, so a conversation started before sign-in stays with the user. If the token’s user already has a contact (they wrote from another device or signed in before), thisvisitor_id’s anonymous contact is merged into it the way an operator merges contacts: the conversation moves to the user and the anonymous contact is deleted. If the two have different addresses on the same channel (two different emails, say), nothing is merged and they stay separate until an operator decides. A contact already bound to anotheruser_id(someone else signed in on this browser earlier) is never re-bound: the new user gets one of their own. Contacts are never looked up by the email or Telegram ID in the request: those fields aren’t signed, and matching on them would let anyone signed in to your site take over someone else’s contact, say one from email or your Telegram bot, together with its conversations. - If there’s no contact, creates one with the fields passed.
- If the contact exists, updates its fields: the email always (except for a contact with a BILLmanager account bound to it, whose address is left as it is), the name only if there’s none or it’s a placeholder, the phone and Telegram ID only if they’re empty.
- If the visitor has an open widget conversation and the request has an email, adds a row to that ticket’s timeline noting that the client introduced themselves.
- Sends webhook events:
contact.createdonly if this call created the contact, andcontact.identifiedon every successful call. See the webhook docs.
The backend rejects an invalid email (422), and then nothing from the request is saved. An invalid token gets 401, and a request with neither visitor_id nor a token gets 404.
The widget shows widget conversations only. If the same contact also has tickets from Telegram, VK, WhatsApp, email or BillManager, they aren’t in the widget, not in the list and not by a direct link: they can’t be opened, answered, closed or rated, and their events don’t reach the widget. Email replies to messages sent from a widget conversation (the email bridge) stay in that conversation and show there. A mail without such a thread, or a message sent through the API, never goes into a widget conversation — it opens a separate ticket. So a visitor who typed someone else’s email or Telegram ID into their own details doesn’t see the real owner’s conversations, even if they end up on the same contact. Emails, API requests and BillManager tickets don’t find a contact by an email typed into the widget at all: they get a contact of their own.
The exception is a visitor the site verified (visitor_token): they have one conversation across chat channels. If their request is open in Telegram, VK or WhatsApp and they write from the widget, the message continues that request, the widget shows it in full and replies come to the widget. Anonymous visitors aren’t affected: there’s no verified identity, so no one else’s conversation opens to them.
Within the widget itself any visitor has one open conversation: a new one goes into the open one (see Conversation with an operator). Whose it is follows the same rule: for an anonymous visitor their visitor_id and only a contact not yet bound to a user, for a signed-in one the token.
Security
The backend doesn’t verify the email, name and phone from data-* and identify(): any visitor can call identify() in their own browser with any data. So they’re only written to that visitor’s own contact and never used to find anyone else’s. Only the visitor_token is verified, and only its user_id binds a visitor to a contact: generate the token on your backend from the current server session, never give a page another user’s token, and call SupportHub.logout() when the user signs out. A user_id starting with bm: is reserved for the BILLmanager module: that’s how it signs in a client-area customer, and by such an id the assistant and operators see that account’s data. Your site must never issue one.
See also
- HMAC visitor token: the token format, the backend checks, errors and edge cases.
- Widget API reference: the full list of
data-*attributes and the controller’s JS API. - Webhooks: subscribe to
contact.created/contact.identifiedto get these events on your backend.

