Skip to main content
This section covers everything you need to programmatically integrate Ansa into your applications. Whether you’re building a custom chat experience, syncing data with external systems, or personalizing conversations based on user identity.

Choose Your Integration

Quick Start

The simplest way to add Ansa to your site. Add this script before the closing </body> tag:
<script src="https://widget.ansa.so/embed.js?agentId=YOUR_AGENT_ID"></script>
The ansa object is automatically available on window for programmatic control:
// Open the chat widget
ansa.open();

// Open with a pre-filled message
ansa.open("I need help with my order");

// Close the widget
ansa.close();

// Identify the current user
ansa.identify({
  userId: "user_123",
  userMetadata: {
    name: "Jane Smith",
    email: "[email protected]",
    plan: "pro"
  }
});

Option 2: JavaScript SDK

For server-side integrations or custom chat interfaces, install the SDK:
npm install @ansa/sdk
import { AnsaClient } from "@ansa/sdk";

const ansa = new AnsaClient({
  apiKey: "ansa_sk_...",
});

// Send a message and get a response
const response = await ansa.chat("agent_id", {
  message: "What are your business hours?",
  conversationId: "conv_123", // optional, for multi-turn
});

console.log(response.message);

API Overview

Widget API Methods

The ansa object exposes these methods when using the embed script:
MethodDescription
ansa.open(message?)Open the chat widget, optionally with a pre-filled message
ansa.close()Close the chat widget
ansa.toggle()Toggle the widget open/closed state
ansa.isOpen()Returns true if the widget is currently open
ansa.identify(identity)Set user identity and metadata
ansa.resetUser()Clear user identity
ansa.showForm(form, options)Display a form in the widget
ansa.showBubbles(messages, duration?)Show notification bubbles
ansa.trigger(eventName, data?)Fire a custom event for triggers
ansa.getVisitorId()Get the anonymous visitor ID

SDK Methods

The AnsaClient provides full API access:
MethodDescription
chat(agentId, options)Send a message and receive a response
streamChat(agentId, options, callbacks)Stream responses token-by-token
getAgent(agentId)Retrieve agent configuration
listAgents()List all agents in your organization
getConversation(id)Get conversation history
listConversations(agentId)List conversations for an agent

Architecture

┌─────────────────┐     ┌─────────────────┐     ┌─────────────────┐
│   Your Site     │     │   Ansa Widget   │     │   Ansa API      │
│                 │     │   (iframe)      │     │                 │
│  ┌───────────┐  │     │                 │     │  ┌───────────┐  │
│  │ ansa.open │──┼────▶│   Chat UI       │────▶│  │  /chat    │  │
│  │ ansa.id.. │  │     │                 │     │  │  /agents  │  │
│  └───────────┘  │     │                 │◀────│  │  /tools   │  │
│                 │     │                 │     │  └───────────┘  │
└─────────────────┘     └─────────────────┘     └─────────────────┘


                                                ┌─────────────────┐
                                                │   Your Server   │
                                                │   (webhooks)    │
                                                └─────────────────┘
The widget runs in an iframe for security isolation. Your page communicates with the widget via postMessage, and the widget communicates with the Ansa API. Webhooks notify your server of events in real-time.

Next Steps