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.
| Property | Type | Description |
|---|---|---|
conversationId | string | Continue an existing conversation |
metadata | Record<string, unknown> | Visitor metadata passed to the agent |
| Property | Type | Description |
|---|---|---|
message | string | The agent’s response text |
conversationId | string | Conversation ID for follow-ups |
messageId | string | Unique message identifier |
toolCalls | ToolCall[] | Tools executed during response |
chatStream(agentId, message, callbacks, options?)
Stream responses token-by-token for real-time display.
| Callback | Parameters | Description |
|---|---|---|
onToken | (token: string) | Called for each streamed token |
onToolCall | (toolCall: ToolCall) | Called when a tool is executed |
onMessage | (response: ChatResponse) | Called with complete response |
onError | (error: Error) | Called on stream error |
onComplete | () | Called when stream ends |
Agent Methods
getAgents()
List all agents in your organization.
getAgent(agentId)
Get details for a specific agent.
| Property | Type | Description |
|---|---|---|
id | string | Unique agent identifier |
name | string | Agent display name |
systemPrompt | string | System prompt/instructions |
model | string | AI model (e.g., “gpt-4o”) |
temperature | number | Response randomness (0-1) |
createdAt | string | ISO timestamp |
updatedAt | string | ISO timestamp |
Conversation Methods
getConversations(agentId)
List all conversations for an agent.
getConversation(conversationId)
Get a conversation with its full message history.
| Property | Type | Description |
|---|---|---|
id | string | Conversation identifier |
agentId | string | Associated agent ID |
messages | Message[] | Array of messages |
createdAt | string | ISO timestamp |
updatedAt | string | ISO timestamp |
Widget Functions
These functions control the embedded widget from your application code. They automatically queue commands if the widget isn’t ready yet.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:
| Property | Type | Description |
|---|---|---|
message | string | Human-readable error message |
statusCode | number | HTTP status code |
code | string? | Machine-readable error code |