Skip to main content
Sonzai Docs

Voice

Real-time voice interaction with text-to-speech, speech-to-text, and live duplex streaming.

Text-to-Speech (TTS)

Convert text to spoken audio.

const audio = await client.voice.tts("agent-id", {
  text: "Hello! How can I help you today?",
  voiceName: "aria",
  language: "en",
  outputFormat: "mp3",
});
// audio.data contains the audio bytes

Speech-to-Text (STT)

Transcribe audio to text.

const result = await client.voice.stt("agent-id", {
  audio: base64AudioData,
  audioFormat: "wav",
  language: "en",
});
console.log(result.text);

Live Voice Streaming

Real-time duplex voice conversation. Get a token, then open a bidirectional stream.

// 1. Get a streaming token
const token = await client.voice.getToken("agent-id", {
  voiceName: "aria",
  userId: "user-123",
});

// 2. Connect to live stream
const stream = await client.voice.stream(token);

// Send audio chunks
stream.sendAudio(audioChunk);

// Or send text for the agent to speak
stream.sendText("Tell me about your day");

// Receive events
for await (const event of stream) {
  if (event.type === "audio") {
    playAudio(event.data);
  } else if (event.type === "transcript") {
    console.log(event.text);
  }
}

// End session
stream.endSession();

WebSocket Transport

Live streaming is powered by WebSocket and supports real-time duplex audio. The client sends microphone audio chunks upstream while simultaneously receiving synthesized speech and transcripts downstream, enabling natural conversational flow.

Browse Voice Catalog

List available voices.

const voices = await client.voices.list({
  language: "en",
  gender: "female",
});

for (const voice of voices.items) {
  console.log(voice.name, voice.language, voice.gender);
}