Skip to main content

Crate typecast_rust

Crate typecast_rust 

Source
Expand description

Typecast Rust SDK

Official Rust SDK for the Typecast Text-to-Speech API.

§Quick Start

use typecast_rust::{TypecastClient, TTSRequest, TTSModel, ClientConfig};

#[tokio::main]
async fn main() -> typecast_rust::Result<()> {
    // Create a client from environment variables
    let client = TypecastClient::from_env()?;

    // Or create with explicit API key
    // let client = TypecastClient::with_api_key("your-api-key")?;

    // Create a TTS request
    let request = TTSRequest::new(
        "tc_60e5426de8b95f1d3000d7b5",
        "Hello, world!",
        TTSModel::SsfmV30,
    );

    // Generate speech
    let response = client.text_to_speech(&request).await?;
     
    // Save audio to file
    std::fs::write("output.wav", &response.audio_data)?;
    println!("Audio saved! Duration: {} seconds", response.duration);

    Ok(())
}

§Features

  • Text-to-Speech: Convert text to natural-sounding speech
  • Multiple Models: Support for ssfm-v21 and ssfm-v30 models
  • Emotion Control: Preset emotions and smart context-aware inference
  • Voice Discovery: List and filter available voices
  • Audio Customization: Control volume, pitch, tempo, and format

§Configuration

The SDK can be configured using environment variables:

  • TYPECAST_API_KEY: Your Typecast API key (required)
  • TYPECAST_API_HOST: Custom API host (optional, defaults to https://api.typecast.ai)

§Error Handling

All operations return a Result<T, TypecastError>. The error type provides detailed information about what went wrong:

use typecast_rust::{TypecastClient, TTSRequest, TTSModel, TypecastError};

let client = TypecastClient::from_env()?;
let request = TTSRequest::new("invalid_voice", "Hello", TTSModel::SsfmV30);

match client.text_to_speech(&request).await {
    Ok(response) => println!("Success!"),
    Err(TypecastError::NotFound { detail }) => {
        println!("Voice not found: {}", detail);
    }
    Err(TypecastError::Unauthorized { .. }) => {
        println!("Invalid API key");
    }
    Err(e) => println!("Other error: {}", e),
}

Re-exports§

pub use client::ClientConfig;
pub use client::TypecastClient;
pub use client::DEFAULT_BASE_URL;
pub use client::DEFAULT_TIMEOUT_SECS;
pub use errors::Result;
pub use errors::TypecastError;
pub use models::Age;
pub use models::AudioFormat;
pub use models::EmotionPreset;
pub use models::ErrorResponse;
pub use models::Gender;
pub use models::ModelInfo;
pub use models::Output;
pub use models::PresetPrompt;
pub use models::Prompt;
pub use models::SmartPrompt;
pub use models::TTSModel;
pub use models::TTSPrompt;
pub use models::TTSRequest;
pub use models::TTSResponse;
pub use models::UseCase;
pub use models::VoiceV2;
pub use models::VoicesV2Filter;

Modules§

client
Typecast API client
errors
Error types for the Typecast SDK
models
Data models for the Typecast API