Skip to main content

Prerequisites

Sign up for a free Fish Audio account to get started with our API.
  1. Go to fish.audio/auth/signup
  2. Fill in your details to create an account, complete steps to verify your account.
  3. Log in to your account and navigate to the API section
Once you have an account, you’ll need an API key to authenticate your requests.
  1. Log in to your Fish Audio Dashboard
  2. Navigate to the API Keys section
  3. Click “Create New Key” and give it a descriptive name, set a expiration if desired
  4. Copy your key and store it securely
Keep your API key secret! Never commit it to version control or share it publicly.

Understanding TTS Methods

The SDK provides three methods for text-to-speech generation, each optimized for different use cases:
Use convert() for most use cases. Use stream() for memory efficiency when handling large files. Use stream_websocket() when text is generated dynamically in real-time.

Basic Usage

Generate speech from text with a single function call:

Using Voice Models

Specify a voice model for consistent voice characteristics:

Finding Voice Models

Get voice model IDs from the Fish Audio website or programmatically:
Learn more in the Voice Cloning guide.

Emotions and Expressions

Add emotional expressions to make speech more natural:
See the Emotion Reference for all available emotions and Fine-grained Control for advanced usage.

Audio Formats

Choose the output format based on your needs:

Prosody Control

Adjust speech speed and volume for natural-sounding output:
For combined speed and volume control, use TTSConfig with Prosody:

Reusable TTS Configuration

Create a configuration once and reuse it across multiple generations:

Chunk-by-Chunk Streaming

Use stream() for memory-efficient transfer and progressive download. Chunks are network transmission units (not semantic audio segments):
For streaming to files or network without buffering in memory:
Use stream() when you have complete text upfront. For real-time streaming with dynamically generated text (LLMs, live captions), use stream_websocket() instead.

Real-time WebSocket Streaming

For real-time applications where text is generated dynamically, use stream_websocket(). This is perfect for LLM integrations, conversational AI, and live captions:

Basic WebSocket Streaming

Understanding FlushEvent

The FlushEvent forces the TTS engine to immediately generate audio from the accumulated text buffer. This is useful when you want to ensure audio is generated at specific points, even if the buffer hasn’t reached the optimal chunk size.
Without FlushEvent, the engine automatically generates audio when the buffer reaches an optimal size. Use FlushEvent to control exactly when audio should be generated, which can reduce perceived latency in interactive applications.

TextEvent vs Plain Strings

You can yield plain strings (recommended for simplicity) or use TextEvent for explicit control:

LLM Integration Pattern

WebSocket streaming shines when integrating with LLM streaming responses. The TTS engine acts as an accumulator, buffering text until it has enough to generate natural-sounding audio:
The WebSocket connection automatically buffers incoming text and generates audio when it has accumulated enough context for natural-sounding speech. You don’t need to manually batch tokens unless you want to force generation at specific points using FlushEvent.
Learn more in the WebSocket Streaming guide.

Advanced Configuration

Comprehensive TTSConfig with all available parameters:
TTSConfig works the same for both sync and async clients. See TTSConfig API Reference for detailed documentation on each parameter and their defaults.

Error Handling

Handle common TTS errors gracefully:
Common exceptions include RateLimitError, ValidationError, NotFoundError, and FishAudioError.

Best Practices

For long texts, adjust chunk_length in TTSConfig:
If you generate the same speech repeatedly, cache the results:
Implement exponential backoff for rate limits:
Balance speed vs quality based on your use case:

Next Steps

Voice Cloning

Create custom voice models

WebSocket Streaming

Real-time audio streaming

Fine-grained Control

Phoneme-level control and paralanguage

Best Practices

Production tips and optimization