JavaScript
Core SDK for browser and Node.js
Installation
npm install @ai-chans/sdk-js
Quick Start
import { ChansClient } from "@ai-chans/sdk-js"
const client = new ChansClient({ agentToken: "agt_xxx", // From dashboard apiUrl: "https://api.chans.ai"})
// Listen for eventsclient.on("transcript", (text) => console.log("User:", text))client.on("response", (text) => console.log("Agent:", text))
// Connect (requests microphone permission)await client.connect({ userId: "user-123" })
// Later: disconnectawait client.disconnect()
ChansClient
Constructor Options
| Option | Type | Required | Description |
|---|---|---|---|
agentToken | string | Yes | Agent token from dashboard (agt_xxx) |
apiUrl | string | No | API URL (default: https://api.chans.ai) |
manualAudioHandling | boolean | No | Handle audio playback manually (for Node.js) |
Methods
connect(options?)
Connects to the voice agent. Requests microphone permission in browser.
await client.connect({ userId: "user-123" // Optional: for conversation segmentation})
disconnect()
Disconnects from the voice agent.
await client.disconnect()
getState()
Returns current connection state.
const state = client.getState() // "idle" | "connecting" | "ready" | ...
isConnected()
Returns whether the client is connected.
if (client.isConnected()) { // ...}
Events
Subscribe with client.on(event, callback). Returns an unsubscribe function.
| Event | Callback | Description |
|---|---|---|
stateChange | (state: ChansState) => void | Connection state changed |
transcript | (text: string) => void | User speech transcribed (interim) |
userTurnComplete | (text: string) => void | User finished speaking (final) |
response | (text: string) => void | Agent response text |
connected | () => void | Connected to room |
disconnected | () => void | Disconnected from room |
agentConnected | (agent: AgentInfo) => void | Agent joined |
agentDisconnected | () => void | Agent left |
error | (error: Error) => void | Error occurred |
audioTrack | (track: RemoteTrack) => void | Agent audio track available |
const unsubscribe = client.on("transcript", (text) => { console.log("User said:", text)})
// Later: stop listeningunsubscribe()
States
type ChansState = | "idle" // Not connected | "connecting" // Connecting to room | "waiting" // Waiting for agent | "ready" // Ready for conversation | "processing" // Processing user speech | "speaking" // Agent is speaking | "error" // Error occurred
Node.js Usage
For Node.js, enable manual audio handling:
const client = new ChansClient({ agentToken: "agt_xxx", manualAudioHandling: true})
client.on("audioTrack", (track) => { // Handle audio track manually})
Next
- React - React hooks and components
- Webhook API - Handle agent responses