Rust Voice API
Call the Narakeet text to speech API from Rust to generate audio files. The example on this page uses reqwest — the most widely used Rust HTTP client — with a blocking call that fits CLI tools and batch scripts. No unsafe code, no C bindings, and no runtime to manage beyond the compiled binary.
For endpoint details, authentication, and features common to all languages, see the main Text to Speech API reference.
- Rust Text to Speech Example
- Rust Audio Generation
- Cargo TTS Integration
- Rust Text to Speech API Options
- Rust vs System TTS Engines
Rust Text to Speech Example
The program below sends text to the Narakeet API and writes the audio response to an MP3 file. The text_to_speech function accepts a reqwest::blocking::Client reference, keeping HTTP transport separate from the API logic.
use std::env;
use std::fs::File;
use std::io::copy;
fn text_to_speech(
client: &reqwest::blocking::Client,
api_key: &str,
voice: &str,
text: &str,
output_path: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let url = format!("https://api.narakeet.com/text-to-speech/mp3?voice={}", voice);
let response = client
.post(&url)
.header("Accept", "application/octet-stream")
.header("Content-Type", "text/plain")
.header("x-api-key", api_key)
.body(text.to_string())
.send()?;
if !response.status().is_success() {
let status = response.status();
let body = response.text().unwrap_or_default();
return Err(format!("API error {}: {}", status, body).into());
}
let bytes = response.bytes()?;
let mut file = File::create(output_path)?;
copy(&mut bytes.as_ref(), &mut file)?;
Ok(())
}
fn main() {
let api_key = env::var("NARAKEET_API_KEY").unwrap_or_default();
if api_key.is_empty() {
eprintln!("Please set NARAKEET_API_KEY environment variable");
std::process::exit(1);
}
let client = reqwest::blocking::Client::builder()
.timeout(std::time::Duration::from_secs(30))
.build()
.expect("Failed to create HTTP client");
match text_to_speech(&client, &api_key, "victoria", "Hi there from Rust", "output.mp3") {
Ok(()) => println!("File saved at: output.mp3"),
Err(e) => {
eprintln!("Error: {}", e);
std::process::exit(1);
}
}
}
Add reqwest = { version = "0.12", features = ["blocking"] } to your Cargo.toml dependencies. Set NARAKEET_API_KEY in your environment — create one from the API Keys page — then run with cargo run.
For the complete project with Cargo.toml and lock file, see the Rust streaming API example on GitHub.
Rust Audio Generation
Rust compiles to native code with no garbage collector, making it well suited for audio generation tasks where predictable performance and low resource use matter. Where Rust audio generation fits in practice:
- Command-line utilities that convert documentation or changelog files into spoken audio
- Embedded systems or edge devices that synthesise voice prompts without a heavy runtime
- WebAssembly modules that call the API from a server-side Wasm runtime
- Pipelines that produce audio as part of a Rust-based build or release toolchain
- Serverless functions on platforms that support custom Rust runtimes
For input longer than 1 KB or uncompressed WAV output, switch to the Long Content (Polling) API.
Cargo TTS Integration
The only external crate needed is reqwest. Rust’s standard library does not include an HTTP client, so reqwest fills that gap — it is the most downloaded networking crate on crates.io and supports both blocking and async modes. Adding it to your project is one line in Cargo.toml:
[dependencies]
reqwest = { version = "0.12", features = ["blocking"] }
If your project already uses an async runtime like Tokio, switch from reqwest::blocking::Client to the async reqwest::Client and .await the response. The API call, headers, and error handling stay the same.
Rust Text to Speech API Options
Control voice, speed, volume, and format through the endpoint URL:
- Audio format
- Change the URL path —
/text-to-speech/mp3for compressed audio,/text-to-speech/m4afor better quality at similar size,/text-to-speech/wavfor uncompressed PCM (requires the polling API) - Voice
- Append
?voice=victoriato choose from 900 voices in 100 languages — see all options at Text to Speech Voices - Speed
- Add
&voice-speed=1.2for 20% faster output, or values below1.0to slow the voice down - Volume
- Add
&voice-volume=softor&voice-volume=loudto adjust the output level
For pitch control, pauses, and multi-voice scripts, pass a script header in the request body. Full reference at Configuring Audio Tasks.
Rust vs System TTS Engines
System-level TTS in Rust typically means FFI bindings to platform libraries — speech-dispatcher on Linux, NSSpeechSynthesizer on macOS, SAPI on Windows. Each requires platform-specific build configuration, C header files, and careful unsafe blocks. With the Narakeet API, Rust text to speech is a single HTTPS POST that returns audio bytes. The same binary runs on every platform without conditional compilation or feature flags for speech backends.