Installation

Install the SDK in your project using npm:

npm install @anam-ai/js-sdk

DOM Elements

This quickstart example requires a video element to display the persona:

For this example we would include the following in our HTML:

<video id="video-element-id" autoplay playsinline></video>

autoplay is used to start the stream as soon as the page loads. The playsinline attribute prevents the video from being played in fullscreen mode on mobile devices.

Basic Usage

It is important to not expose your API key publicly. Instead, you should:

  1. Exchange your API key for a short-lived session token on the server side
  2. Pass this token to the client
  3. Initialize the Anam SDK with the session token

Step 1: Getting a Session Token

From your server, make a request to get a session token using your API key and persona configuration:

Step 1: Get a session token
// Make this request from your server to keep your API key secure
const response = await fetch("https://api.anam.ai/v1/auth/session-token", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    Authorization: `Bearer ${process.env.ANAM_API_KEY}`,
  },
  body: JSON.stringify({
    personaConfig: {
      name: "Alex",
      avatarId: "30fa96d0-26c4-4e55-94a0-517025942e18",
      voiceId: "6bfbe25a-979d-40f3-a92b-5394170af54b",
      brainType: "ANAM_GPT_4O_MINI_V1",
      systemPrompt: "You are a helpful customer service representative.",
    },
  }),
});
const { sessionToken } = await response.json();

Step 2: Initialize the Client

Once you have a session token, use the createClient method to initialize the Anam client:

Step 2: Initialize the client
import { createClient } from '@anam-ai/js-sdk';

const anamClient = createClient(sessionToken);

// Stream to any video element
await anamClient.streamToVideoElement('video-element-id');

Stopping a Stream

To stop an active session, use the stopStreaming method. This will end the current session and release the audio and video elements.

await anamClient.stopStreaming();

Next Steps