Identify Logged-in Users
If your site has authentication, call identify() after login to attach trusted profile data to the current visitor. The AI then knows who it's talking to and can pull data from your CRM, order history, or account.
Vanilla JS
widget.identify({
externalUserId: "user_42",
name: "Alex Example",
email: "[email protected]",
phone: "+491701234567",
company: "Example GmbH",
});externalUserId is the only field you really need — that's the join key to your own database. Everything else helps the AI personalize.
React / Next.js (script-tag-style via event)
The <BitPalmAgent /> component doesn't expose a ref by design. Instead, dispatch a browser event from anywhere in your app:
"use client";
import { useEffect } from "react";
export function IdentifyOnLogin({ user }: { user: User | null }) {
useEffect(() => {
if (!user) return;
window.dispatchEvent(
new CustomEvent("bitpalm-identify", {
detail: {
slug: "your-agent-slug",
externalUserId: user.id,
name: user.name,
email: user.email,
},
})
);
}, [user]);
return null;
}The widget listens for bitpalm-identify on window and forwards the data.
Script-tag integrations
Same event pattern as React above. Call from anywhere in your page after login:
window.dispatchEvent(
new CustomEvent("bitpalm-identify", {
detail: {
slug: "your-agent-slug",
externalUserId: "user_42",
name: "Alex Example",
email: "[email protected]",
},
})
);Verified users (signed token)
identify() and context() come from the browser, so anyone could send other values. The agent treats them as hints, not as proof of who it is talking to.
When the agent runs in a members area and has to know for sure who is logged in, for example to talk about someone's own contract or tickets, your server signs a short token about the user:
- In the dashboard, open your agent, then Settings → Access → Logged-in users and create a signing key. Keep it on your server, it never belongs in the browser.
- When someone is logged in, your server creates a JWT (HS256) with that key.
- The page passes the token on as context. The agent checks it on every message.
// Your server, e.g. in the route that renders the members area
import jwt from "jsonwebtoken";
const userToken = jwt.sign(
{
sub: user.id, // required, your own user id
name: user.name, // optional
email: user.email, // optional
attributes: { role: "landlord", units: ["A-1203"] }, // optional, up to 2 KB
aud: "your-agent-slug", // optional, binds the token to this agent
},
process.env.BITPALM_IDENTITY_KEY,
{ algorithm: "HS256", expiresIn: "1h" }, // at most 24 hours
);// The page, with the script tag or the <BitPalmAgent /> component
window.dispatchEvent(
new CustomEvent("bitpalm-context", {
detail: { slug: "your-agent-slug", userToken },
})
);
// On logout, empty every token you sent
window.dispatchEvent(
new CustomEvent("bitpalm-context", {
detail: { slug: "your-agent-slug", userToken: "", sessionToken: "" },
})
);
// With createWidget() from the npm package
widget.context({ userToken });
widget.resetContext(); // on logout, clears all contextNew context is merged with what the widget already has, so with the event a token you don't overwrite stays until the page reloads. An empty token counts as no token.
Sign a new token before the old one runs out, for example on every page load, and send it the same way.
With your own chat UI, send it as userToken in the body of POST /api/v1/chat.
What the token carries is shown to the agent as a verified user, including the attributes. A token with a wrong signature, without sub or past its expiry counts as no token. The token itself never reaches the model.
Under Only for logged-in users you can switch the agent to answer nobody without a valid token. Visitors without one see a note to log in.
Quick links per role and greeting by name
The widget checks the token as soon as it arrives and uses two things from it. Nothing else from the token reaches the browser.
- Role:
attributes.role(a text) orattributes.roles(a list). In the dashboard under Quick links, set a group to Only certain roles, for exampletenant. The widget shows the first group whose pages and roles match. Groups without roles are for everyone, and visitors without a valid token only see those. Put groups for roles above the groups for everyone. - First name:
attributes.first_name, otherwise the first word ofname. Write{first_name}in the greeting, for exampleHi {first_name}, I'm Sara.Without a valid token the placeholder goes away with the space or comma in front of it:Hi, I'm Sara.
Both update right away when you send a token later, a new one, or clear it on logout.
const userToken = jwt.sign(
{ sub: user.id, name: user.name, attributes: { role: "tenant", first_name: user.firstName } },
process.env.BITPALM_IDENTITY_KEY,
{ algorithm: "HS256", expiresIn: "1h" },
);Quick links per role decide what is shown, they are not a lock. What the agent tells someone still depends on the token it checks with every message.
Your code can also set the quick links itself, see setQuickLinks().
Calling your API for the logged-in user
Your agent can call your own API endpoints. Set an endpoint's access to session token and send a token of your API along in the context:
window.dispatchEvent(
new CustomEvent("bitpalm-context", {
detail: { slug: "your-agent-slug", sessionToken },
})
);It goes to that endpoint as Authorization: Bearer <sessionToken> and never reaches the model. Keep it short-lived and limited, and let your API check it and return only that user's data. On logout, send it empty together with the user token, as shown above.
Files in the chat
In your agent's chat style settings, switch on files and choose what visitors can send: photos, PDF, Word, Excel, text. Also set the largest file, the photo quality and, if you want, after how many days files get deleted. The agent sees photos. Other files it can't read, it only passes them on.
Switch on Send the chat's files along on a POST or PUT endpoint, and the server adds them to the JSON body:
{
"description": "...",
"attachments": [
{
"id": "b1f3a2c0-...",
"name": "Contract.pdf",
"mime_type": "application/pdf",
"size_bytes": 482133,
"url": "https://..."
}
]
}urlworks for 10 minutes. Fetch it before you answer, only from our storage host and without following redirects. A call with files has 30 seconds.- The agent picks which files belong to a call, judged by name and conversation. Only those come, the others keep waiting for a request they fit. When it picks none,
attachmentsis left out. At most 10 come per call. - The agent picks with a field called
attach_files, so with files switched on,attachmentsandattach_filescan't be names of your own parameters.attach_filesnever reaches your API. - A file counts as delivered after a 2xx answer and comes to the same endpoint only once. After an error or a timeout it comes again, with the same
id, so ignore ids you already have. - The same file sent twice in one chat before it went out comes once. Sent again after it went out, it counts as sent on purpose and comes again with a new
id. Two different files can have the samename, so tell them apart byid. - Calls with files carry an
Idempotency-Keyheader. A repeat of the same call has the same key, so you can spot it when the first try already opened your ticket. - Don't send the links back in your answer. We remove them before the agent sees it.
- We check that a file really is what it claims to be, but we don't scan for viruses.
Calling identify multiple times
Safe and additive. Later calls merge with earlier ones — pass only the fields that have new values:
widget.identify({ externalUserId: "user_42", email: "[email protected]" });
// later, after they update their phone in your settings:
widget.identify({ phone: "+491701234567" });The widget keeps the original externalUserId and adds the phone.
See also
- Form auto-identify — capture data without writing code
- Custom Context — pass non-identity business data (cart, listing, plan)