Fish Audio Python SDK

1.3.0 · active · verified Thu Apr 16

The `fish-audio-sdk` is the official Python client library for the Fish Audio API, currently at version 1.3.0. While the `fish-audio-sdk` PyPI package is still actively maintained, from version 1.0.0 onwards, it installs the newer, client-based `fishaudio` module, which is the recommended and more robust API for interacting with Fish Audio services. Releases are frequent, often several times a month, reflecting continuous development and feature additions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Fish Audio client, synthesize text into speech, and save the resulting audio to a file. It is essential to set your `FISH_API_KEY` environment variable or pass it directly to the `FishAudio` client during initialization. The example uses the synchronous `convert` method for simplicity.

import os
from fishaudio import FishAudio
from fishaudio.utils import play, save

# Ensure your API key is set as an environment variable or pass it directly
# export FISH_API_KEY="YOUR_API_KEY_HERE"
api_key = os.environ.get('FISH_API_KEY', 'YOUR_API_KEY_HERE') # Replace with actual key or ensure env var is set

if api_key == 'YOUR_API_KEY_HERE':
    print("Warning: FISH_API_KEY environment variable not set. Using placeholder.")
    print("Please set FISH_API_KEY or provide it directly to FishAudio(api_key='...')")
    exit(1)

client = FishAudio(api_key=api_key)

text_to_synthesize = "Hello, this is a test from the Fish Audio Python SDK. Hope you enjoy it!"

try:
    # Generate speech (returns bytes)
    audio = client.tts.convert(text=text_to_synthesize)

    # Save the audio to a file
    output_filename = "output_audio.mp3"
    save(audio, output_filename)
    print(f"Audio saved to {output_filename}")

    # Optionally, play the audio (requires `utils` extra and platform support)
    # play(audio)
    # print("Audio played.")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →