Skip to main content

Environment Requirements

Before you begin, ensure your development environment meets the following requirements:
  • Node.js Version: 18.0 or higher
  • Browser Version: Chrome 90+, Firefox 90+, Safari 14+, Edge 90+
  • Package Manager: npm / yarn / pnpm

Step 1: Install SDK

Step 2: Initialize SDK

Initialize the SDK in your application:
main.ts
import { AvatarKit, Environment, DrivingServiceMode } from '@spatialwalk/avatarkit'

await AvatarKit.initialize('your-app-id', {
  environment: Environment.cn, // or Environment.intl, Environment.test
  drivingServiceMode: DrivingServiceMode.sdk, // Optional, 'sdk' is default
  // - DrivingServiceMode.sdk: SDK mode - SDK handles WebSocket communication automatically
  // - DrivingServiceMode.host: Host mode - Host app provides audio and animation data
})
Integration Modes:
  • SDK Mode (Default): SDK handles WebSocket communication automatically. Best for real-time audio input scenarios.
  • Host Mode: Host application manages its own network/data fetching. Best for custom data sources or pre-recorded content.
For more details, see Integration Modes.

Step 3: Load Character

Load a character using the shared AvatarManager instance:
loadAvatar.ts
import { AvatarManager } from '@spatialwalk/avatarkit'

const avatarManager = AvatarManager.shared
const avatar = await avatarManager.load('character-id', (progress) => {
  console.log(`Loading: ${progress.progress}%`)
})

Step 4: Create View and Start Rendering

createView.ts
import { AvatarView } from '@spatialwalk/avatarkit'

const container = document.getElementById('avatar-container')
const avatarView = new AvatarView(avatar, container)

// Wait for first frame to render
await avatarView.ready

// Start real-time communication (SDK mode only)
await avatarView.avatarController.start()

Complete Example

Complete Example
import {
  AvatarKit,
  AvatarManager,
  AvatarView,
  Environment,
} from '@spatialwalk/avatarkit'

// 1. Initialize
await AvatarKit.initialize('your-app-id', {
  environment: Environment.cn,
})

// Set sessionToken (if needed, call separately)
AvatarKit.setSessionToken('your-session-token')

// 2. Load character
const avatarManager = AvatarManager.shared
const avatar = await avatarManager.load('character-id', (progress) => {
  console.log(`Loading: ${progress.progress}%`)
})

// 3. Create view
const container = document.getElementById('avatar-container')
const avatarView = new AvatarView(avatar, container)

// Wait for first frame to render
await avatarView.ready

// 4. Set up event listeners
avatarView.avatarController.onConnectionState = (state) => {
  console.log('Connection state:', state)
}

avatarView.avatarController.onConversationState = (state) => {
  console.log('Conversation state:', state)
}

// 5. Start real-time communication
await avatarView.avatarController.start()

// 6. Send audio data (SDK mode)
// ⚠️ Important: Audio must be 16kHz mono PCM16 format (ArrayBuffer)
// For detailed audio format requirements and resampling examples, see [API Reference - Audio Processing](/web-sdk/api-reference#audio-processing)
const audioData = new ArrayBuffer(1024) // Example: 16kHz PCM16 audio data
avatarView.avatarController.send(audioData, false) // Send audio data
// Call when audio ends to mark the end of current conversation round
avatarView.avatarController.send(new ArrayBuffer(0), true)

// 7. Clean up resources (if you no longer need the digital human SDK, you must clean them up)
avatarView.dispose()
AvatarKit.cleanup() // Clean up SDK global resources (only call if you no longer need the entire SDK)

Host Mode Example

Host Mode Example
import {
  AvatarKit,
  AvatarManager,
  AvatarView,
  Environment,
  DrivingServiceMode,
} from '@spatialwalk/avatarkit'

// 1. Initialize SDK with Host mode
await AvatarKit.initialize('your-app-id', {
  environment: Environment.cn,
  drivingServiceMode: DrivingServiceMode.host, // Host mode
})

// 2. Load character
const avatarManager = AvatarManager.shared
const avatar = await avatarManager.load('character-id', (progress) => {
  console.log(`Loading: ${progress.progress}%`)
})

// 3. Create view
const container = document.getElementById('avatar-container')
const avatarView = new AvatarView(avatar, container)
await avatarView.ready

// 4. Host Mode Workflow:
// ⚠️ IMPORTANT: In Host mode, conversationId is CRITICAL for data matching.
//    You MUST send audio data FIRST to get a conversationId,
//    then use that conversationId to send animation data.
//    Animation data with mismatched conversationId will be discarded.

// NOTE: The data variables below are placeholders. Replace them with your actual data.
const audioData = new Uint8Array(1024) // Example streaming audio data
const animationData = [] // Example streaming animation keyframes from your service
// Step 1: Send audio data first to get conversationId (automatically generates if starting new session)
const conversationId = avatarView.avatarController.yieldAudioData(audioData, false)
// Step 2: Use the conversationId to send animation data (REQUIRED - mismatched conversationId will be discarded)
if (conversationId) {
  avatarView.avatarController.yieldFramesData(animationData, conversationId)
}

// 5. Clean up resources
avatarView.avatarController.clear() // Clear all data and resources
avatarView.dispose()
AvatarKit.cleanup()

Step 5: Run Tests

  1. Open your application in a browser
  2. Check the browser console output to confirm SDK initialization succeeded
  3. Observe if the digital human displays correctly
  4. Test audio-driven functionality
It’s recommended to test in modern browsers (Chrome, Edge) for the best WebGPU support

Next Steps

Congratulations! You have successfully integrated SPAvatarKit Web SDK. Next, you can:

Having Issues?

If you encounter problems during integration, please check:
  • FAQ - Common integration questions