Get your first AI persona running in under 5 minutes
The Anam API provides a simple interface for implementing digital AI personas within your web applications. This guide will walk you through the process of setting up a minimal example of an interactive AI persona. By the end, you’ll have a working persona that can have real-time conversations in your web browser.
Create a new file called index.html and add this basic structure:
index.html
Copy
Ask AI
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Hello Cara</title></head><body> <div style="text-align: center; padding: 20px;"> <h1>Chat with Cara</h1> <p>Your AI persona will appear below and start automatically</p> <video id="persona-video" autoplay playsinline style="max-width: 100%; border-radius: 8px;"></video> <div id="status" style="margin-top: 15px; font-size: 14px; color: #666;">Loading...</div> </div></body></html>
2
Add the JavaScript
Now add the script that will automatically start your persona. Add this <script> tag just before the closing </body> tag:
Copy
Ask AI
<script type="module"> import { createClient } from "https://esm.sh/@anam-ai/js-sdk@latest"; // Replace with your actual API key const API_KEY = "your-api-key-here"; const videoElement = document.getElementById("persona-video"); const statusElement = document.getElementById("status"); async function createSessionToken() { const response = await fetch("https://api.anam.ai/v1/auth/session-token", { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${API_KEY}`, }, body: JSON.stringify({ personaConfig: { name: "Cara", avatarId: "30fa96d0-26c4-4e55-94a0-517025942e18", voiceId: "6bfbe25a-979d-40f3-a92b-5394170af54b", brainType: "ANAM_GPT_4O_MINI_V1", systemPrompt: "You are Cara, a helpful and friendly AI assistant. Keep responses conversational and concise.", }, }), }); const data = await response.json(); return data.sessionToken; } async function startChat() { try { statusElement.textContent = "Creating session..."; const sessionToken = await createSessionToken(); statusElement.textContent = "Connecting..."; const anamClient = createClient(sessionToken); await anamClient.streamToVideoElement("persona-video"); statusElement.textContent = "Connected! Start speaking to Cara"; } catch (error) { console.error("Failed to start chat:", error); statusElement.textContent = "Failed to connect. Check your API key."; } } // Auto-start when page loads startChat();</script>
3
Add your API key
Replace your-api-key-here with your actual Anam API key in the script.
4
Complete file example
Your final index.html file should look like this:
index.html
Copy
Ask AI
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Hello Cara</title></head><body> <div style="text-align: center; padding: 20px;"> <h1>Chat with Cara</h1> <p>Your AI persona will appear below and start automatically</p> <video id="persona-video" autoplay playsinline style="max-width: 100%; border-radius: 8px;"></video> <div id="status" style="margin-top: 15px; font-size: 14px; color: #666;">Loading...</div> </div> <script type="module"> import { createClient } from "https://esm.sh/@anam-ai/js-sdk@latest"; // Replace with your actual API key const API_KEY = "your-api-key-here"; const videoElement = document.getElementById("persona-video"); const statusElement = document.getElementById("status"); async function createSessionToken() { const response = await fetch("https://api.anam.ai/v1/auth/session-token", { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${API_KEY}`, }, body: JSON.stringify({ personaConfig: { name: "Cara", avatarId: "30fa96d0-26c4-4e55-94a0-517025942e18", voiceId: "6bfbe25a-979d-40f3-a92b-5394170af54b", brainType: "ANAM_GPT_4O_MINI_V1", systemPrompt: "You are Cara, a helpful and friendly AI assistant. Keep responses conversational and concise.", }, }), }); const data = await response.json(); return data.sessionToken; } async function startChat() { try { statusElement.textContent = "Creating session..."; const sessionToken = await createSessionToken(); statusElement.textContent = "Connecting..."; const anamClient = createClient(sessionToken); await anamClient.streamToVideoElement("persona-video"); statusElement.textContent = "Connected! Start speaking to Cara"; } catch (error) { console.error("Failed to start chat:", error); statusElement.textContent = "Failed to connect. Check your API key."; } } // Auto-start when page loads startChat(); </script></body></html>
5
Start a local server
To serve your html file, you’ll need a local web server. You can use one of the following methods depending on which development tools you have installed.
Copy
Ask AI
npx http-server -p 8000
6
Open and test
Navigate to http://localhost:8000 in your browser
Allow microphone access when prompted
Cara will appear automatically and you can start talking!
When you’re done, close the browser tab to end the session.