Discussions

Ask a Question
Back to All

Streaming Avatar Session Creation in Unity

Hi Everyone, I want to integrate Heygen Streaming API in unity. To do that I am writing my own c# code by referring to the JS code here: https://github.com/HeyGen-Official/StreamingAvatar.

Whenever I am trying to create a session in c#, I am getting 401: Unauthorized. I tried with my API key elsewhere(web), it's working. I assume that it has something to do with the headers. Here is my sample code:


CreateSessionRequest requestBody = new CreateSessionRequest{  
    quality = "low",  
    avatar_id = avatarId,  
    voice = new VoiceRequest{  
      voice_id = voiceId  
    }  
};  
string json = JsonUtility.ToJson(requestBody);
string url = "https://api.heygen.com/v1/streaming.new";

var request = new UnityWebRequest(url, "POST");
byte[] bodyRaw = new System.Text.UTF8Encoding().GetBytes(json);
request.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
request.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
request.SetRequestHeader("accept", "application/json");
request.SetRequestHeader("content-type", "application/json");
request.SetRequestHeader("x-api-key", apiKey);
yield return request.SendWebRequest();