elevenlabs_tts

Crates.ioelevenlabs_tts
lib.rselevenlabs_tts
version0.2.1
created_at2025-08-28 01:41:26.495401+00
updated_at2025-09-08 13:44:00.965207+00
descriptionType-safe Rust client for ElevenLabs Text-to-Speech API
homepage
repositoryhttps://github.com/hamzaelmarjani/elevenlabs_tts
max_upload_size
id1813456
size96,287
Hamza El Marjani (hamzaelmarjani)

documentation

README

elevenlabs_tts

Crates.io Docs.rs License

A type-safe, async Rust client for the ElevenLabs Text-to-Speech API. Generate high-quality speech from text with a simple, ergonomic API.

Features

  • Type-safe & Async: Built with Rust's type system and async/await support
  • Builder Pattern: Intuitive, chainable API for configuring TTS requests
  • Predefined Voices: Access to static voice definitions (voices::all_voices::*)
  • Model Support: Full support for ElevenLabs models (models::elevenlabs_models::*)
  • Customizable: Voice settings, Elevanlabs TTS APIs, custom base URLs, and enterprise support
  • Tokio Ready: Works seamlessly with the Tokio runtime

Check-out Also:

This project is part of a milestone to implement all ElevenLabs APIs in Rust.

Installation

Add this to your Cargo.toml:

[dependencies]
elevenlabs_tts = "0.2.1"

Quick Start

use elevenlabs_tts::ElevenLabsTTSClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = ElevenLabsTTSClient::new("your-api-key");

    let audio = client
        .text_to_speech("Hello, world!")
        .voice(&voices::all_voices::ARNOLD) // Arnold Voice
        .model(models::elevanlabs_models::ELEVEN_MULTILINGUAL_V2) // Eleven Multilingual v2
        .execute()
        .await?;

    std::fs::write("output.mp3", audio)?;
    Ok(())
}

Examples

Basic Usage

use elevenlabs_tts::{ElevenLabsTTSClient, models, voices};
use std::env;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let api_key = env::var("ELEVENLABS_API_KEY")
        .expect("Please set ELEVENLABS_API_KEY environment variable");

    let client = ElevenLabsTTSClient::new(api_key);

    let prompt = "Happiness often hides in ordinary moments, waiting for you to pause, smile, and simply enjoy being present.";

    let audio = client
        .text_to_speech(prompt)
        .voice(&voices::all_voices::ARNOLD) // Arnold Voice
        .model(models::elevanlabs_models::ELEVEN_MULTILINGUAL_V2) // Eleven Multilingual v2
        .execute()
        .await?;

    std::fs::create_dir_all("outputs")?;
    std::fs::write("outputs/output.mp3", audio)?;
    println!("Audio saved to outputs/output.mp3");
    Ok(())
}

Advanced Configuration

use elevenlabs_tts::{ElevenLabsTTSClient, VoiceSettings, models, voices};
use std::env;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let api_key = env::var("ELEVENLABS_API_KEY")?;
    let client = ElevenLabsTTSClient::new(api_key);

    let voice_settings = VoiceSettings::default()
        .style(0.3)
        .speaker_boost(false)
        .speed(1.05);

    let prompt = "Life feels lighter when you slow down, take a deep breath, and notice the small details around you.";

    let audio = client
        .text_to_speech(prompt)
        .voice_settings(voice_settings.clone())
        .voice(&voices::all_voices::IVANA)
        .model(models::elevanlabs_models::ELEVEN_FLASH_V2_5)
        .language_code("fr")
        .output_format("mp3_44100_192")
        .seed(4000)
        .execute()
        .await?;


    std::fs::create_dir_all("outputs")?;
    std::fs::write("outputs/advanced_output.mp3", audio)?;
    println!("Advanced audio saved to outputs/advanced_output.mp3");
    Ok(())
}

Running Examples

# Set your API key
export ELEVENLABS_API_KEY=your_api_key_here

# Run the basic example
cargo run --example basic_tts

# Run the advanced example
cargo run --example advanced_tts

API Overview

Method Description
ElevenLabsTTSClient::new(String) Create client instance (required)*
.text_to_speech(String) Build a TTS request (required)*
.voice(String) Use a static voice (optional)
.voice_id(String) Use custom voice ID (optional)
.model(String) Select model (optional)
.voice_settings(VoiceSettings) Fine-tune voice params (optional)
.output_format(String) Audio format (e.g. mp3_44100) (optional)
.language_code(String) Force language pronounce/accent only (no translation) (optional)
.seed(u32) Deterministic sampling (optional)
.previous_text(String) Improve continuity (before) (optional)
.next_text(String) Improve continuity (after) (optional)
.previous_request_ids(Vec<String>) Continuity previous requests (optional)
.next_request_ids(Vec<String>) Continuity next requests (optional)
.apply_text_normalization(String) Normalize text (auto/on/off) (optional)
.apply_language_text_normalization(bool) Lang-specific normalization (optional)
.execute() Run request → audio (required)*

Error Handling

The crate uses standard Rust error handling patterns. All async methods return Result types:

match client.text_to_speech("Hello").execute().await {
    Ok(audio) => println!("Generated {} bytes of audio", audio.len()),
    Err(e) => eprintln!("TTS generation failed: {}", e),
}

Requirements

  • Rust 1.70+ (for async/await support)
  • Tokio runtime
  • Valid ElevenLabs API key

License

Licensed under either of:

at your option.

Contributing

Contributions are welcome! Please feel free to:

  • Open issues for bugs or feature requests
  • Submit pull requests with improvements
  • Improve documentation or examples
  • Add tests or benchmarks

Before contributing, please ensure your code follows Rust conventions and includes appropriate tests.

Support

If you like this project, consider supporting me on Patreon 💖

Patreon

Changelog

See CHANGELOG.md for a detailed history of changes.


Note: This crate is not officially affiliated with ElevenLabs. Please refer to the ElevenLabs API documentation for the most up-to-date API information.

Commit count: 19

cargo fmt