Installation
Quick Start
API Client
UseAnsaClient for server-side API access:
Widget Control
Use the widget helpers for client-side control:API Client Reference
Configuration
Chat Methods
chat(agentId, message, options?)
Send a message and receive a complete response.
Response:
chatStream(agentId, message, callbacks, options?)
Stream responses token-by-token for real-time display.
Agent Methods
getAgents()
List all agents in your organization.
getAgent(agentId)
Get details for a specific agent.
Conversation Methods
getConversations(agentId)
List all conversations for an agent.
getConversation(conversationId)
Get a conversation with its full message history.
Widget Functions
These functions control the embedded widget from your application code. They automatically queue commands if the widget isn’t ready yet, using an internal command queue that processes once the widget is initialized.Command Queue Pattern
The SDK uses a command queue to handle cases where you call widget functions before the widget is fully loaded:window.ansa?.isReady() and either executes immediately or queues the command:
embedWidget(config)
Programmatically embed the widget (alternative to the script tag).
openWidget(message?)
Open the chat widget, optionally with a pre-filled message.
closeWidget()
Close the chat widget.
toggleWidget()
Toggle the widget open/closed.
isWidgetOpen() / isWidgetReady()
Check widget state.
showBubbles(messages, duration?)
Show notification bubbles near the widget button.
showForm(form, options?)
Display a form in the widget. See Triggering Forms for details.
identify(identity) / clearIdentity()
Set or clear user identity. Identity is used for:
- Form pre-filling — Fields automatically populate from
userMetadata - HTTP tool authentication — Pass tokens to your APIs via
{{userMetadata.token}}syntax - Personalization — Agent receives user context for personalized responses
getVisitorId()
Get the anonymous visitor ID.
triggerEvent(eventName, data?)
Fire a custom event for trigger automation.
Error Handling
The SDK throwsAnsaError for API errors:
TypeScript
The SDK is written in TypeScript and exports all types:Next Steps
Widget API
Full widget API reference
Triggering Forms
Display forms programmatically