tts/
tts.rs

1// import the dependencies
2use elevenlabs_api::{
3    tts::{TtsApi, TtsBody},
4    *,
5};
6
7fn main() {
8    // Load API key from environment ELEVENLABS_API_KEY.
9    // You can also hadcode through `Auth::new(<your_api_key>)`, but it is not recommended.
10    let auth = Auth::from_env().unwrap();
11    let elevenlabs = Elevenlabs::new(auth, "https://api.elevenlabs.io/v1/");
12
13    // Create the tts body.
14    let tts_body = TtsBody {
15        model_id: None,
16        text: "Hello world".to_string(),
17        voice_settings: None,
18    };
19
20    // Generate the speech for the text by using the voice with id yoZ06aMxZJJ28mfd3POQ.
21    let tts_result = elevenlabs.tts(&tts_body, "yoZ06aMxZJJ28mfd3POQ");
22    let bytes = tts_result.unwrap();
23
24    // Do what you need with the bytes.
25    // The server responds with "audio/mpeg" so we can save as mp3.
26    std::fs::write("tts.mp3", bytes).unwrap();
27}