Prerequisites
- Node.js 20.9+
- A Next.js project using the App Router, or the demo project
- An Anam account and API key (sign up free at lab.anam.ai)
- A Cara 4 avatar from the Anam Lab Avatars page
- An ElevenLabs account with a Conversational AI agent configured via the dashboard
Create the server route
You need one API route. It checks that the request came from your own site, fetches a signed URL from ElevenLabs, then passes it to Anam when creating a session token. API keys and IDs stay server-side. The route reads the avatar ID (which face to render) and the ElevenLabs agent ID from environment variables, so a caller can’t point your keys at a different agent or avatar.Environment variables
NEXT_PUBLIC_ prefix, so Next.js never sends them to the browser.
Protect the route
Every session uses minutes on your Anam account and your ElevenLabs agent. Without a check, any website or script could call the route and start sessions on your keys. These helpers add two checks:- Same origin. The route rejects requests that a browser sent from another site.
- Start ticket. Just before each start, a server action signs a ticket with
SESSION_SECRET, which never leaves the server. The route accepts each ticket once, within two minutes.
API route
environment.elevenLabsAgentSettings field configures ElevenLabs as the session’s speech recognition, language model, and text-to-speech provider. avatarModel and directorNotes enable a warm Cara 4 performance at the start of the session.
The signed URL is short-lived (typically valid for 15 minutes). Anam uses it immediately when the client connects, so there’s no issue with expiry in normal usage. If you’re pre-fetching tokens, create them just before the client needs them.
Step 3: Start the avatar session
Connect the browser, enable your microphone, and have your first conversation with the avatar.

