ElevenLabs Streaming API Documentation Everything you need to get started with the ElevenLabs Streaming API, with code samples. You're welcome.

in API

September 1, 2024 3 min read
ElevenLabs Streaming API Documentation

Low latency, highest quality text to speech API

clone voiceClone your voice
Free API Playground

Table of Contents

The ElevenLabs Streaming API allows developers to convert text into high-quality speech in real-time, delivering low-latency audio streams for a wide range of applications like voice assistants, chatbots, and voice cloning tools. This guide walks you through how to integrate the API, optimize performance, and handle common challenges.

ElevenLabs Streaming API supports:

  1. Python
  2. Java script
  3. cURL
  4. PHP
  5. Go
  6. Java

Key Features

Authentication

Each request to the ElevenLabs API must include your API key in the header:

xi-api-key: your_api_key
content-type: application/json

Replace your_api_key with your actual API key.

API Endpoints

Streaming Endpoint

POST https://api.elevenlabs.io/v1/text-to-speech/stream
  • Headers:
  • xi-api-key: Your ElevenLabs API key.
  • content-type: application/json
  • Body: Send a JSON payload with text and voice parameters.

Example payload:

{
  "text": "Hello, welcome to the ElevenLabs API",
  "voice_id": "21m00tcm4tlvdq8ikwam",
  "model_id": "turbo_v2",
  "voice_settings": {
    "similarity_boost": 0.75,
    "stability": 0.5
  }
}

Response

The response returns an audio stream that can be played in real time.

Sample Python Code

import requests

def stream_audio(text):
    url = "https://api.elevenlabs.io/v1/text-to-speech/stream"
    headers = {
        "xi-api-key": "your_api_key",
        "content-type": "application/json"
    }
    payload = {
        "text": text,
        "voice_id": "21m00tcm4tlvdq8ikwam",
        "voice_settings": {"similarity_boost": 0.85}
    }

    response = requests.post(url, headers=headers, json=payload, stream=True)
    with open("output.mp3", "wb") as audio_file:
        for chunk in response.iter_content(chunk_size=1024):
            if chunk:
                audio_file.write(chunk)

JavaScript Example (WebSocket)

const socket = new WebSocket('wss://api.elevenlabs.io/v1/text-to-speech/stream');

socket.onopen = () => {
    const payload = {
        text: "Streaming real-time audio with ElevenLabs API.",
        voice_id: "21m00tcm4tlvdq8ikwam",
        model_id: "turbo_v2"
    };
    socket.send(JSON.stringify(payload));
};

socket.onmessage = (event) => {
    // Play or process the audio stream here.
    console.log("Audio received", event.data);
};

Rate Limits and Streaming Latency

  • Rate limits: Ensure you stay within the allowed requests per minute based on your plan.
  • Latency: The streaming_latency parameter helps in optimizing real-time performance.
  • Error handling: Handle common errors like 429 (too many requests) and timeouts with retries.

Available Voices

  • Voice_id: You can choose from a library of voices or clone your own. For example, 21m00tcm4tlvdq8ikwam is the default voice.
  • Voice cloning: Create unique voice models for specialized tasks.

Test Environment

You can test your API setup using ElevenLabs’ GitHub examples. Modify these to suit your needs.

Error Handling

Common errors and troubleshooting tips:

  • 429: Too many requests; consider reducing request frequency.
  • 500: Server error; try again or contact support.

Optimization Tips

  • Use async methods for handling streaming data.
  • Experiment with the voice_settings like similarity_boost to fine-tune voice output.
  • Choose the right output_format (MP3/WAV) based on your use case.

Integrating ElevenLabs’ streaming text-to-speech service offers seamless, low-latency audio generation for any app or service. Whether you’re building an interactive assistant, a chatbot, or content production tools, the flexibility and performance of this API can help you create high-quality audio experiences in real time.

For more detailed examples, visit the ElevenLabs docs.

Recent Posts

Listen & Rate TTS Voices

See Leaderboard

Top AI Apps

Alternatives

Similar articles