| Crates.io | elevenlabs_tts |
| lib.rs | elevenlabs_tts |
| version | 0.2.1 |
| created_at | 2025-08-28 01:41:26.495401+00 |
| updated_at | 2025-09-08 13:44:00.965207+00 |
| description | Type-safe Rust client for ElevenLabs Text-to-Speech API |
| homepage | |
| repository | https://github.com/hamzaelmarjani/elevenlabs_tts |
| max_upload_size | |
| id | 1813456 |
| size | 96,287 |
A type-safe, async Rust client for the ElevenLabs Text-to-Speech API. Generate high-quality speech from text with a simple, ergonomic API.
voices::all_voices::*)models::elevenlabs_models::*)This project is part of a milestone to implement all ElevenLabs APIs in Rust.
Add this to your Cargo.toml:
[dependencies]
elevenlabs_tts = "0.2.1"
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(())
}
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(())
}
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(())
}
# 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
| 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)* |
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),
}
Licensed under either of:
at your option.
Contributions are welcome! Please feel free to:
Before contributing, please ensure your code follows Rust conventions and includes appropriate tests.
If you like this project, consider supporting me on Patreon 💖
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.