Ultravox Client SDK for Web

0.5.0 · active · verified Tue Apr 21

The `ultravox-client` library provides a web client SDK for integrating Ultravox's real-time, speech-to-speech AI capabilities into web applications. Written in TypeScript, it offers an event-driven API for managing interactive voice sessions. The current stable version is 0.5.0, indicating it is still in active development with potential for API changes in future minor releases. It enables developers to join AI-powered calls, listen for session status changes, and receive real-time speech transcripts from both users and AI agents. Key differentiators include its focus on low-latency, real-time voice interaction and its comprehensive event model for managing session state and data flow within a web browser environment, abstracting away the underlying WebSocket complexities.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes an `UltravoxSession`, sets up event listeners for session status changes and real-time transcript updates, and attempts to connect to an Ultravox AI call using a provided WebSocket URL. It demonstrates how to monitor the call's state, process incoming speech-to-text data, and manage the session lifecycle within a web application.

import { UltravoxSession } from 'ultravox-client';

// In a real application, replace this with a URL obtained securely from your backend
// which generates it via the Ultravox API. Do NOT hardcode or expose API keys.
const ULTRAVOX_JOIN_URL = 'wss://your-call-join-url'; // Placeholder for demonstration

const session = new UltravoxSession();

session.addEventListener('status', (event) => {
  console.log('Session status changed: ', session.status);
  if (session.status === 'disconnected') {
    console.log('Ultravox session disconnected. Attempting to reconnect if desired.');
  } else if (session.status === 'listening') {
    console.log('Ultravox is now listening for your voice input. Speak freely!');
  } else if (session.status === 'speaking') {
    console.log('Ultravox AI is responding...');
  }
});

session.addEventListener('transcripts', (event) => {
  // `session.transcripts` is an array of all received transcripts.
  // This event fires for both partial and final transcripts.
  const currentTranscripts = session.transcripts;
  console.log('Current transcripts: ', currentTranscripts);

  // Example: Find and log the latest final transcript
  const lastFinalTranscript = currentTranscripts.findLast(t => t.isFinal);
  if (lastFinalTranscript) {
    console.log(`[${lastFinalTranscript.speaker}]: ${lastFinalTranscript.text}`);
  }
});

async function startUltravoxSession() {
  try {
    console.log('Attempting to join Ultravox call at:', ULTRAVOX_JOIN_URL);
    await session.joinCall(ULTRAVOX_JOIN_URL);
    console.log('Successfully joined Ultravox call. Waiting for initial status...');
  } catch (error) {
    console.error("Failed to join call, ensure URL is valid and network is available:", error);
  }
}

startUltravoxSession();

// Example: To leave the call after some user interaction or timeout
// setTimeout(() => {
//   console.log('Leaving Ultravox call after a demonstration period...');
//   session.leaveCall();
// }, 60000); // Automatically leave after 60 seconds

view raw JSON →