Skip to main content

Web Demo Repository

Complete working examples for Vanilla JS, Vue, and React — the fastest way to get started.

Installation

Build Tool Configuration

Required: The SDK uses WASM files that need special build configuration. You must configure your build tool before using the SDK.
Add the official plugin to vite.config.ts:
vite.config.ts
The plugin automatically handles:
  • Development Server: Sets correct MIME type for WASM files
  • Build Time: Copies WASM files to dist/assets/
  • Cloudflare Pages: Generates _headers file
  • Vite Configuration: Configures optimizeDeps, assetsInclude, etc.
vite.config.ts

Authentication

SDK Mode requires an App ID and a Session Token.
Authentication Flow:
The Session Token must be set before calling start(). Never expose your token generation logic in client-side code.

Quick Start

1

Initialize SDK

2

Load Avatar

3

Create View

4

Initialize Audio Context

Critical: initializeAudioContext() must be called inside a user gesture handler (e.g., click, touchstart). This is a browser security requirement — calling it outside a user gesture will fail silently.
5

Connect and Send Audio

6

Cleanup

Core API

AvatarSDK

SDK initialization and global configuration.
setSessionToken() can be called before or after initialize(). If called before, the token is applied automatically during initialization.

AvatarManager

Avatar resource loading and caching. Access via the singleton AvatarManager.shared.

AvatarView

3D rendering view. Automatically creates a Canvas element and an AvatarController.
Transform coordinates:
Container requirement: The container element must have non-zero width and height. The Canvas fills the container and auto-resizes via ResizeObserver.

AvatarController — SDK Mode Methods

These methods are only available when drivingServiceMode is DrivingServiceMode.sdk.
send() behavior:
  • end: false — continues the current conversation round
  • end: true — marks the end of audio input for the current round. The avatar will continue playing remaining animation until finished, then automatically return to idle (notified via onConversationState). Sending new audio after this starts a new round and interrupts any ongoing playback

AvatarController — Common Methods

Available in both SDK Mode and Host Mode.

AvatarController — Event Callbacks

Audio Format

The SDK requires audio in mono PCM16 format: Data size: 1 second at 16 kHz = 16,000 samples × 2 bytes = 32,000 bytes.

Configuration

Configuration Interface

Environment

DrivingServiceMode

AudioFormat

LogLevel

State Management

ConnectionState

Reported via onConnectionState callback (SDK Mode only).

ConversationState

Reported via onConversationState callback.
State transitions are notified immediately when the transition starts, not when the animation completes. For example, playing is reported as soon as the transition from idle begins.

Error Handling

AvatarError

Error Callback

Lifecycle Management

Avatar Switching

Resource Cleanup

dispose() automatically cleans up all resources:
  • WebSocket connections
  • Audio playback data and animation resources
  • Canvas elements and render system
  • Event listeners and callbacks
Always call dispose() when the view is no longer needed. Failing to do so may cause memory leaks.

Fallback Mechanism

If the WebSocket connection fails within 15 seconds, the SDK automatically enters audio-only fallback mode — audio continues playing without animation. This ensures uninterrupted audio playback when the server is unreachable.
  • The fallback mode is interruptible like normal playback
  • onConnectionState reports failed when the connection times out

Browser Compatibility

Common Issues