Discussions

Ask a Question
Back to All

401 Error on createStartAvatar() Despite Valid Streaming Token

We're encountering a consistent 401 (Unauthorized) error with the Streaming Avatar SDK, specifically at the createStartAvatar() step. Interestingly, our token creation works fine.

Our Setup:

  • Using @heygen/streaming-avatar SDK
  • Next.js 14 application
  • Valid API key confirmed working

Flow & Error Details:

1. Token Creation (✅ Works)

Our /api/heygen/token endpoint successfully gets a token:

const response = await fetch('https://api.heygen.com/v1/streaming.create_token', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-Api-Key': apiKey,
  },
});

2. SDK Initialization (✅ Seems OK)

avatar = new StreamingAvatar({ 
  token,
  onError: (error: any) => {
    console.error('[AvatarSDK] SDK Error:', error);
  }
});

3. Avatar Creation (❌ Fails with 401)

const sessionInfo = await avatar.createStartAvatar({
  avatarName: '73c84e2b886940099c5793b085150f2f',
  quality: AvatarQuality.Low,
});

Error Message:

[AvatarSDK] Failed to start session: APIError: API request failed with status 401
    at new APIError (index.esm.js:328:28)
    at StreamingAvatar.eval (index.esm.js:747:31)

What We've Confirmed:

API key is valid (successfully creates streaming tokens)
Token is being passed to StreamingAvatar constructor
Same error occurs with multiple different API keys
Error is consistent across browser refreshes and new sessions

Questions:

  1. Is there a specific format/transformation needed for the token between creation and createStartAvatar()?
  2. Are there any known issues with token validation in the SDK?
  3. Should we be setting any additional headers or configurations we might be missing?
  4. Is there a way to enable more verbose debugging to see the exact request being made by createStartAvatar()?

Any guidance would be greatly appreciated! Let me know if you need any additional information.

Thanks!