ansa object is automatically exposed on the window object. This API lets you programmatically control the widget — open/close the chat, show forms, identify users, and trigger automations.
Basic Setup
Add the embed script to your page:ansa object is available immediately after the script loads:
API Reference
ansa.open(message?)
Opens the chat widget. Optionally pre-fill the input with a message.
| Parameter | Type | Description |
|---|---|---|
message | string? | Optional message to pre-fill in the input |
ansa.close()
Closes the chat widget.
ansa.toggle()
Toggles the widget between open and closed states.
ansa.isOpen()
Returns whether the widget is currently open.
boolean
ansa.isReady()
Returns whether the widget has finished initializing.
boolean
ansa.showBubbles(messages, duration?)
Shows notification bubble(s) near the widget button. Great for proactive engagement.
| Parameter | Type | Description |
|---|---|---|
messages | string | string[] | Message(s) to display |
duration | number? | Seconds before auto-hide (optional) |
ansa.hideBubbles()
Hides any visible notification bubbles.
ansa.showForm(form, options?)
Displays a form in the widget. Can reference a server-side form by name or provide a client-side schema.
| Parameter | Type | Description |
|---|---|---|
form | string | FormSchema | Tool name (server) or form schema (client) |
options.onSubmit | function? | Called with form data on submit |
options.onCancel | function? | Called when form is cancelled |
options.sendToAgent | boolean? | Also send data to agent as message |
ansa.trigger(eventName, data?)
Fires a custom event that can trigger automations.
| Parameter | Type | Description |
|---|---|---|
eventName | string | Name of the custom event |
data | object? | Optional event payload |
ansa.identify(identity)
Sets the current user’s identity. This personalizes the chat experience and pre-fills forms.
| Property | Type | Description |
|---|---|---|
userId | string? | Your internal user ID |
userMetadata | object? | Additional user properties |
ansa.resetUser()
Clears the current user identity (e.g., on logout).
ansa.getVisitorId()
Returns the anonymous visitor ID (persisted in localStorage).
string — Unique visitor identifier
ansa.registerFormSchema(schemas)
Registers client-side form schema providers. Forms are generated dynamically based on context.
| Parameter | Type | Description |
|---|---|---|
schemas | object | Map of form name → schema provider function |
args: Arguments passed from the agent tool calluser: User context ({ visitorId, userId?, userMetadata?, conversationId? })
ansa.hasFormSchema(name)
Checks if a form schema provider is registered.
boolean
ansa.registerFormHandler(name, handler) Legacy
Registers a form submission handler for server-side forms.
ansa.unregisterFormHandler(name) Legacy
Removes a registered form handler.
ansa.hasFormHandler(name) Legacy
Checks if a form handler is registered.
Configuration
Script Parameters
Configure the widget via URL parameters:| Parameter | Type | Description |
|---|---|---|
agentId | string | Your agent ID (required) |
theme | "light" | "dark" | Color theme |
primaryColor | string | Primary accent color (URL-encoded hex) |
chatBubbleColor | string | Chat bubble button color |
displayName | string | Agent display name |
profilePicture | string | Agent avatar URL |
initialMessage | string | First message from agent |
messagePlaceholder | string | Input placeholder text |
suggestedMessages | string | Comma-separated suggestions |
welcomeBubbles | string | Comma-separated bubble messages |