Pass user context to personalize conversations and pre-fill forms
User identity lets you connect chat conversations with your application’s user accounts. When you identify a user, their conversations become linked to their profile, forms can be pre-filled with known data, and the agent can personalize responses.
Forms automatically use metadata to pre-fill fields when the field name exactly matches a key in userMetadata:
Copy
// After identifying with emailansa.identify({ userId: "user_123", userMetadata: { email: "jane@example.com", name: "Jane Smith", company: "Acme Inc" }});// This form will have fields pre-filledansa.showForm({ fields: [ { name: "email", label: "Email", type: "email" }, // → jane@example.com { name: "name", label: "Name", type: "text" }, // → Jane Smith { name: "company", label: "Company", type: "text" } // → Acme Inc ]});
Field names must exactly match the keys in userMetadata. For example, a field named user_email will only be pre-filled if you have userMetadata.user_email, not userMetadata.email.
The agent receives user metadata and can use it in responses:
Copy
User: What's my current plan?Agent: Hi Jane! You're on the Enterprise plan with Acme Inc. You've been with us since January 2024.
For security, sensitive fields are automatically excluded from the agent’s context. Fields containing tokens, passwords, secrets, API keys, or credentials are filtered out. See Personalized Responses for details.
Identity data can be passed to HTTP tools for authenticated API calls. This is useful when your tools need to access user-specific data from your backend.
Copy
// Identify with an auth tokenansa.identify({ userId: "user_123", userMetadata: { authToken: "eyJhbGciOiJIUzI1NiIs...", // User's JWT email: "jane@example.com" }});
The agent receives sanitized user context to personalize its responses. This enables greetings, contextual answers, and tailored recommendations based on user data.
To protect sensitive data, certain fields are automatically excluded from the agent’s context. Fields with keys matching these patterns (case-insensitive) are filtered:
token
secret
password
apikey / api_key
accesstoken / access_token
refreshtoken / refresh_token
auth
credential
private
Copy
// What you pass to identify()ansa.identify({ userId: "user_123", userMetadata: { name: "Jane Smith", email: "jane@example.com", authToken: "eyJhbGciOiJIUzI1NiIs...", // Contains "auth" + "token" apiKey: "sk-1234567890", // Contains "apikey" plan: "enterprise" }});// What the agent sees{ userId: "user_123", userMetadata: { name: "Jane Smith", email: "jane@example.com", plan: "enterprise" }}
Sensitive fields are only filtered from the agent’s LLM context. They are still available for HTTP tool substitution (see section 5) and are sent to your backend.
When a visitor logs in, their anonymous conversations become linked to their user account:
Copy
// Before login: conversations stored under visitorId// User browses, asks questions...// On loginansa.identify({ userId: "user_123", userMetadata: { name: "Jane" }});// After login: previous conversations now linked to user_123// Future conversations also linked to user_123
// Identity is sent with each request// Check Network tab for X-Ansa-User-Id header// Or listen for identity eventswindow.addEventListener("message", (e) => { if (e.data.type === "ansa:identityUpdate") { console.log("Identity updated:", e.data.data); }});