Skip to main content

Quick Reference

App ID is required on every integration path and identifies your Spatius application (it scopes which avatars you can load). Session Token is only required for Direct Mode (DrivingServiceMode.direct), where it authenticates the Motion Server WebSocket; RTC / Platform Integration / Backend Mode paths do not need it. See Credentials for the per-path breakdown.
For the minimum end-to-end integration walkthrough (install, build configuration, init, load, connect), see the Direct Mode guide for Web.

AvatarSDK

Main entry point for SDK initialization and global configuration.

Static properties

Static methods

initialize(appId, configuration)

Initialize the SDK. Must be called before any other operation.

setSessionToken(token)

Set the Session Token used to authenticate the Motion Server WebSocket. Only required in Direct Mode (DrivingServiceMode.direct); AvatarController.start() opens that WebSocket and authenticates with the token.
  • Required for DrivingServiceMode.direct before calling AvatarController.start().
  • Not required for DrivingServiceMode.backend (LiveKit Agents Integration, RTC Adapter path, Backend Mode). Those paths receive audio and motion data through their own transport and do not open a Motion Server WebSocket from the client; AvatarManager.load() fetches avatar metadata over an App-ID-scoped public endpoint.
  • The token must be obtained from your backend (see Session token API).
  • Maximum 24-hour validity.
  • Token must be paired with the App ID used in initialize().
setSessionToken() can be called before or after initialize(). If called before, the token is applied automatically during initialization.

setUserId(userId)

Set a user identifier for logging and telemetry.

setRenderQuality(quality)

Update the global rendering quality tier. The change takes effect on the next rendered frame across active AvatarView instances.

setRenderResolutionCap(enabled, maxHeight?)

Cap the render backing-buffer height while keeping the same CSS size. This is useful on high-DPI displays where rendering above the source avatar asset resolution adds cost without visible benefit.

deviceScore()

Run a short CPU/GPU benchmark and return device scores.

isDeviceSupported()

Check whether the current device can run AvatarKit. In 1.2.0, this runs the same benchmark path used by deviceScore().

cleanup()

Release all SDK resources. Call when the SDK is no longer needed.

AvatarManager

Handles avatar asset loading and caching. Access via the singleton AvatarManager.shared.

Static properties

Instance methods

load(id, onProgress?, useCompressedModel?)

Load an avatar by ID. Downloads and caches the avatar’s assets.
Returns: Promise<Avatar>

cancelLoad(id)

Cancel a pending or running avatar load task.

retrieve(id)

Return a cached avatar instance, if available.

clear(id)

Clear a specific avatar from cache.

clearAll()

Clear all cached avatar resources.

AvatarView

3D rendering view. Automatically creates a Canvas element and an associated AvatarController.

Constructor

Container requirement: the container element must have non-zero width and height. The canvas fills the container and auto-resizes via ResizeObserver.

Render over an Avatar Background

Download the optional 16:9 background from Spatius Studio and store it as an application asset. Set it on the same stage element that you pass to AvatarView; the SDK-created canvas has a transparent background.
For square or portrait display windows, keep this inner stage at 16:9, center it, and clip it with an outer container. See Avatar Background for the shared cropping rules.

Instance properties

Transform coordinates

Instance methods

dispose()

Release all view resources. Call when the view is no longer needed (see Lifecycle management for details).

exportBitmap()

Capture the current rendered avatar frame as a PNG Blob. Returns null if the canvas is not initialized or not currently rendering.

getCameraConfig() / updateCameraConfig(cameraConfig)

Read or update the camera used by the renderer.

pauseRendering() / resumeRendering()

Pause or resume GPU/canvas rendering without stopping audio playback.

isRenderingEnabled()

Check whether the render loop is currently active.

getBoundingRect()

Return the approximate avatar bounds in CSS pixels, or null if the view is not ready. Recalculate after container size or avatarTransform changes.

AvatarController

Handles runtime communication with Motion Server and playback control.

Event callbacks

Instance properties

DrivingServiceMode.direct methods

Available when drivingServiceMode is DrivingServiceMode.direct (the Direct Mode path).

initializeAudioContext()

Initialize the audio context. Must be called inside a user-gesture handler (e.g. a click listener).

start()

Connect to Motion Server.

send(audioData, end)

Send avatar speech audio. Returns the conversationId for the current round. For audio source and timing guidance, see Audio.
send() behavior:
  • end: false — continues the current conversation round.
  • end: true — marks the end of audio input for the current round. The avatar plays the remaining animation, then returns to idle (notified via onConversationState). Sending new audio after this starts a new round and interrupts any ongoing playback.

close()

Close the Motion Server connection.

DrivingServiceMode.backend methods

Available when drivingServiceMode is DrivingServiceMode.backend (the Backend Mode path).

yieldAudioData(audioData, end?)

Provide audio data when your backend owns the Motion Server connection.
Returns: string | null — conversation ID for this audio round, or null if audio playback could not start.

yieldFramesData(motionDataPayloads, conversationId)

Provide motion data payloads when using DrivingServiceMode.backend. The conversationId must match the one returned by the corresponding yieldAudioData() call.
Returns: booleantrue when the final motion data payload for that conversation has been received, otherwise false.

Common methods

Available in both DrivingServiceMode.direct and DrivingServiceMode.backend.

Types and Enums

Configuration

region selects the Spatius deployment region. It defaults to 'us-west'; supported values are 'us-west', 'ap-northeast', and 'cn-beijing'. See Regions for endpoint details and override options.

DrivingServiceMode

RenderQuality

FrameStarvationMode

audioIndependent keeps audio playing while motion data catches up. strictSync pauses audio when motion data runs out and resumes when new motion data arrives; use onPlaybackStall to observe those transitions.

AnimationType

LogLevel

ConnectionState

Reported via onConnectionState. Only emitted when drivingServiceMode is DrivingServiceMode.direct.

ConversationState

Reported via onConversationState.
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.

LoadProgressInfo

Multiply by 100 when rendering as a percentage in your UI.

AudioFormat

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

Error Handling

AvatarError

Error callback

ErrorCode

AvatarError.code is one of the SDK string enum values below.
See Client Error Codes for recovery guidance and Server Error Codes for Console API and Motion Server errors.

Lifecycle Management

Avatar switching

Resource cleanup

dispose() automatically cleans up:
  • WebSocket connections
  • Audio playback data and animation resources
  • Canvas elements and the 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 keeps playback uninterrupted when Motion Server is unreachable.
  • Fallback mode is interruptible like normal playback.
  • onConnectionState reports failed when the connection times out.

Browser Compatibility


Common Issues


Complete Usage Example