Code Examples
Ready-to-use code samples to help you get started quickly.
Basic JavaScript Example
import { RTAVClient } from '@rtav/sdk';
const client = new RTAVClient({
apiKey: process.env.RTAV_API_KEY
});
// Create session
const session = await client.sessions.create({
model: 'gpt-4',
voice: 'alloy',
video: true
});
// Connect
await session.connect();
// Send message
await session.sendMessage('Hello!');
// Handle responses
session.on('audio', (audioChunk) => {
// Play audio
audioContext.decodeAudioData(audioChunk).then(buffer => {
// Play buffer
});
});
session.on('video', (videoChunk) => {
// Display video
videoElement.srcObject = videoChunk;
});
// End session
await session.end();Python Example
from rtav import RTAVClient
import asyncio
client = RTAVClient(api_key='your-api-key')
async def main():
# Create session
session = await client.sessions.create(
model='gpt-4',
voice='alloy',
video=True
)
# Connect
await session.connect()
# Send message
await session.send_message('Hello!')
# Handle responses
async for audio, video in session.stream():
if audio:
# Process audio
pass
if video:
# Process video
pass
# End session
await session.end()
asyncio.run(main())React Example
import { useEffect, useRef, useState } from 'react';
import { RTAVClient } from '@rtav/sdk';
function ChatComponent() {
const [session, setSession] = useState(null);
const videoRef = useRef(null);
const audioRef = useRef(null);
useEffect(() => {
const client = new RTAVClient({
apiKey: process.env.NEXT_PUBLIC_RTAV_API_KEY
});
client.sessions.create({
model: 'gpt-4',
voice: 'alloy',
video: true
}).then(s => {
setSession(s);
s.connect();
s.on('video', (stream) => {
if (videoRef.current) {
videoRef.current.srcObject = stream;
}
});
s.on('audio', (audio) => {
// Handle audio
});
});
return () => {
session?.end();
};
}, []);
const sendMessage = async (text) => {
await session?.sendMessage(text);
};
return (
<div>
<video ref={videoRef} autoPlay />
<button onClick={() => sendMessage('Hello!')}>
Send Message
</button>
</div>
);
}More Examples
Check out our Getting Started guide for more detailed examples and tutorials.