How to use this guide
This guide takes an additive approach, covering a range of Anam features and implementation patterns. Each section builds upon the previous ones, allowing you to add functionality progressively.We provide detailed, complete code examples to make integration
with AI coding assistants straightforward. Use the buttons in the top-right corner of
this page to open the content in your preferred LLM or copy the entire guide
for easy reference.
What You’ll Build
By the end of this guide, you’ll have a persona application featuring:- Modern UI/UX with loading states, connection status, and responsive design
- Chat History Panel showing conversation transcripts in real-time
- Advanced Event Handling for connection management and user feedback
- Talk Commands for programmatic persona control
- Audio Management with mute/unmute functionality
- Production Security with proper authentication and rate limiting
- Error Handling & Retry Logic for better user experience
Prerequisites
- Node.js (version 18 or higher) and npm installed
- Understanding of modern JavaScript/TypeScript
- An Anam API key (get one here)
- Basic knowledge of Express.js and modern web development
- A microphone and speakers for voice interaction
Basic App
If you’ve already completed the Basic Application guide, you can skip to Listening to events. The setup below is similar but included here for completeness.
1
Create project directory
2
Initialize Node.js project
bash npm init -y This creates a package.json file for managing dependencies.3
Create public directory
bash mkdir public The
public folder will contain your HTML and JavaScript files that are served to the browser.4
Install dependencies
bash npm install express dotenv We’re installing Express for the server and dotenv for environment variable management. The Anam SDK will be loaded directly from a CDN in the browser.
5
Configure environment variables
Create a
.env file in your project root to store your API key securely:.env
Step 1: Set up your server
Create a basic Express server to handle session token generation:server.js
Step 2: Set up your HTML
Create a simple HTML page with a video element and basic controls:public/index.html
Step 3: Initialize the Anam client
Create the client-side JavaScript to control your persona:public/script.js
Step 4: Test your basic setup
- Start your server:
- Open http://localhost:8000 in your browser
- Click “Start Chat” to begin your conversation!
You should see Cara appear in the video element and be ready to chat through
voice interaction.
Listening to events
Anam personas communicate through an event system that allows your application to respond to connection changes, conversation updates, and user interactions. Let’s enhance our basic app with event handling to create a more responsive experience.Understanding the Event System
The Anam SDK uses an event-driven architecture where you can listen for specific events and react accordingly. Here’s the process for listening to events:1
Import the event types
Import the specific event types you want to listen to:
2
Define your event listener function
Create a function to handle the event:
3
Add your event listener to the client
Register your listener function with the client:
Adding a loading state
Loading states provide important user feedback during connection establishment. Let’s add a simple loading indicator to our HTML:Step 1: Update the UI
public/index.html
Step 2: Adding the event listener
Now let’s update our JavaScript to handle the loading state by adding the following to ourscript.js file:
public/script.js
Adding a chat history panel
A common use case for event listeners is to update the UI based on the transcription of the conversation. Let’s look at how we can implement this using Anam events.Understanding Message Events
Anam provides two key events for tracking conversation transcriptions:
Let’s start with the
MESSAGE_HISTORY_UPDATED event to build a basic chat history.
Step 1: Add chat history UI
First, update your HTML to include a simple chat panel:public/index.html
Step 2: Listen for updates
Now let’s add the event listener to handle complete conversation updates:public/script.js
The
MESSAGE_HISTORY_UPDATED event provides an array of message objects with
role (“user” or “assistant”) and content properties. This gives you the
complete conversation transcript each time someone finishes speaking.Step 3: Add real-time transcription
Now let’s enhance the experience by showing live transcription as the persona speaks using theMESSAGE_STREAM_EVENT_RECEIVED event:
Your app now displays both complete conversation history and real-time
transcription as the persona speaks!
Sending commands
Beyond voice interaction, you can programmatically send messages to your persona using the talk command. This is useful for creating interactive experiences, automated workflows, or custom UI controls.Using the talk command
The talk command allows you to send text messages that the persona will speak directly. This can be used to instruct the persona to say something in response to a user action other than voice input, such as a button click or when certain UI elements are shown on screen. You can send a talk command by calling thetalk method on the Anam client during a session.
Step 1: Update the UI
First, update your HTML to include a text input and send buttonpublic/index.html
Step 2: Add the code
Now let’s add the code to ourscript.js file to send the command to the persona
public/script.js
Advanced Stream Management
For applications that require more control over audio and video streams, you have two main approaches for customizing the streaming behavior:- Custom Input Streams: Pass your own audio input to
streamToVideoElement - Output Stream Capture: Use
stream()to capture and process persona output
Custom Input Streams
ThestreamToVideoElement() method accepts an optional audio input stream, allowing you to process or record user audio before sending it to the persona:
- Record user audio input
- Apply audio filters or effects to user input
- Monitor user audio levels
- Handle custom microphone setups
Output Stream Capture
For capturing and processing the persona’s output (video and audio), use thestream() method which returns the raw output streams:
- Recording persona video and audio output
- Custom video rendering and effects
- Audio analysis and processing
- Streaming to multiple destinations
What You’ve Built
You now have a working Anam application with these core patterns:- WebRTC Streaming: Real-time video connection for voice conversations with an AI persona
- Event Handling: Responding to session state changes and conversation updates using the SDK’s event system
- Live Transcription: Real-time display of what the persona is saying via
MESSAGE_STREAM_EVENT_RECEIVED - Conversation History: Complete transcript tracking via
MESSAGE_HISTORY_UPDATED - Talk Commands: Programmatic text input to trigger persona responses
- Session Management: Secure server-side token generation with proper API key protection
- Stream Recording: Manual stream management for custom audio processing workflows
What’s Next
Ready to take your persona application further? Here are your next steps: Explore Advanced Features: Continue reading our guides to learn about audio control, custom personas, and production deployment best practices. Integrate Your Own AI: Want to use your own language model instead of Anam’s default brain? Check out our Custom LLM Integration guide to learn how to connect your persona to custom AI models and APIs.Advanced Customization
Persona Configuration
Modify thegetDefaultPersonaConfig() method to customize:
- Avatar appearance and voice selection
- System prompts and personality traits
- Custom brain types and LLM integration
UI Theming
Update the CSS variables inapp.css to match your brand:
- Color schemes and gradients
- Typography and spacing
- Animation timing and effects
Event Integration
Extend the event handlers to integrate with your existing systems:- Analytics and user behavior tracking
- Customer service platforms
- CRM and database systems
Troubleshooting
Connection Issues
Connection Issues
Symptoms: Persona fails to connect or frequently disconnectsSolutions:
- Verify your API key is correctly configured
- Check network connectivity and firewall settings
- Ensure WebRTC is supported in your browser
- Review server logs for authentication errors
Audio Problems
Audio Problems
Symptoms: No audio input/output or poor qualitySolutions:
- Grant microphone permissions in browser settings
- Check audio device configuration
- Ensure HTTPS is enabled (required for microphone access)
- Test with different browsers or devices
Performance Issues
Performance Issues
Symptoms: Slow loading or laggy interactionsSolutions:
- Optimize network bandwidth and connection quality
- Reduce video quality if needed
- Implement connection pooling and caching
- Monitor server resource usage
UI Responsiveness
UI Responsiveness
Symptoms: Interface doesn’t work on mobile or different screen sizesSolutions:
- Test on various devices and screen sizes
- Verify CSS media queries are working
- Check touch event handling
- Validate accessibility compliance

