Skip to main content
The Ansa SDK provides a type-safe way to interact with the Ansa API from Node.js, browsers, or any JavaScript runtime. It includes both an API client for server-side integrations and helper functions for controlling the widget.

Installation

Quick Start

API Client

Use AnsaClient for server-side API access:

Widget Control

Use the widget helpers for client-side control:

API Client Reference

Configuration

Create a client:

Chat Methods

chat(agentId, message, options?)

Send a message and receive a complete response.
Options: Response:

chatStream(agentId, message, callbacks, options?)

Stream responses token-by-token for real-time display.
Callbacks:

Agent Methods

getAgents()

List all agents in your organization.

getAgent(agentId)

Get details for a specific agent.
Agent Object:

Conversation Methods

getConversations(agentId)

List all conversations for an agent.

getConversation(conversationId)

Get a conversation with its full message history.
Conversation Object:

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:
Under the hood, the SDK checks window.ansa?.isReady() and either executes immediately or queues the command:
You don’t need to manually wait for the widget—the SDK handles this automatically. But if you need to know when the widget is ready, use window.ansa.onReady() or listen for the ansa:ready event.

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
See Identity for details.

getVisitorId()

Get the anonymous visitor ID.

triggerEvent(eventName, data?)

Fire a custom event for trigger automation.

Error Handling

The SDK throws AnsaError for API errors:
AnsaError Properties:

TypeScript

The SDK is written in TypeScript and exports all types:

Next Steps

Widget API

Full widget API reference

Triggering Forms

Display forms programmatically