# Chat SDK (/sdk)

The SDK connects your own chat interface to Talqo. It uses the same embed token, agent configuration, and server-side limits as the [pre-built widget](/embed-chat-integration).

## Install

```sh
npm install @talqo/sdk
```

The package includes browser ESM JavaScript and TypeScript declarations. Use it from a browser application with a bundler. Install the SDK version matching your Talqo server release.

## Connect your interface

Create an embed in the dashboard and copy its public token. Use your Talqo origin as `apiUrl`; do not include `/api`.

```ts
import { createChatClient } from "@talqo/sdk"

const chat = createChatClient({
	apiUrl: "https://talqo.example.com",
	embedToken: "YOUR_EMBED_TOKEN",
})

const unsubscribe = chat.subscribe(() => {
	const state = chat.getSnapshot()
	// Render state.messages and state.activity in your interface.
	console.log(state)
})

await chat.initialize()
await chat.sendMessage("What delivery options do you offer?")

// When your interface unmounts:
unsubscribe()
chat.dispose()
```

`initialize()` loads the embed configuration and restores the conversation. Subscribe before initializing so your interface observes loading and recovery states. `sendMessage()` streams updates through the subscription and resolves when sending completes. Catch rejected operations and render `getSnapshot().error` when present. Server-reported error codes are documented in the [API problem types](/problems) catalog.

## State and controls

`getSnapshot()` returns messages, configuration, activity, available actions, notices, active tool calls, and any error. Use `isAvailable` to hide an unavailable chat and `actions` to enable `send`, `stop`, `retry`, and `new-chat` controls. The SDK owns lifecycle and recovery decisions. `acceptedMessageId` identifies the latest accepted user message, so a UI can clear its submitted draft without inspecting transcript changes. Message text belongs to the message's `text` field. Render it as text or sanitized Markdown, never as unchecked HTML.

Use `cancelResponse()` to stop a reply, `retryLastMessage()` to retry after a failed response, and `startNewChat()` to clear the current conversation and begin another. `error.newChatAvailable` includes SDK recovery defaults when the server omits guidance. An explicit server value, including `false`, takes precedence.

The SDK stores conversation credentials in browser local storage and falls back to memory if storage is unavailable. A `storage-unavailable` error or notice means a reload can lose the conversation. The SDK owns streaming, cancellation, and reconnect recovery; custom interfaces should render its state rather than implement a second transport.

The embed token is public. Provider API keys and operator credentials must stay on the Talqo server.
