Skip to main content

Prerequisites

Sign up for a free Fish Audio account to get started with our API.
  1. Sign up on fish.audio
  2. Fill in your details to create an account, complete steps to verify your account.
  3. Log in to your account and open Get Started
Once you have an account, you’ll need an API key to authenticate your requests.
  1. Log in and open Get Started
  2. Click “Get your API key”: the key is created in one click and shown only once
  3. Copy your key and store it securely
Keep your API key secret! Never commit it to version control or share it publicly.
Install the Python or TypeScript SDK:
client.asr.stream_websocket() requires fish-audio-sdk 1.5.0 or later, and client.speechToText.convertRealtime() requires fish-audio 0.2.0 or later (Node.js only). In browsers or other languages, connect to the WebSocket directly.
You also need a 16-bit PCM mono WAV file, at 8000, 12000, 16000, 22050, 24000, 32000, 44100 or 48000 Hz. To convert a recording, run ffmpeg -i input.mp3 -ac 1 -ar 16000 -c:a pcm_s16le speech.wav.

Recipe

client.asr.stream_websocket() in Python and client.speechToText.convertRealtime() in TypeScript take an iterator of audio chunks and return the events the server sends. The script streams the file in 40 ms chunks at real-time speed, as a live microphone would, and prints every event until the session ends. The SDK opens the session, sends each chunk as it is yielded, and ends the session when the iterator is exhausted.
Run it with your API key in FISH_API_KEY:
Node.js 22.18 or later runs TypeScript files directly; on an earlier version, compile the script with tsc first. The script uses import: inside a project whose package.json sets "type": "commonjs", name it transcribe_file.mts. For a 15-second customer-support clip, the output looks like this (... marks drafts left out):
The clip ends mid-sentence, so the last turn is committed by audio_done. Drafts can hold partial words, such as deli-; the committed text is final.

Read the events

Each event is an object with a type and the fields of the server message as attributes; a field the server left out is None in Python and undefined in TypeScript. The exceptions are on committed: a missing user_turn is True in both SDKs, and Python also fills a missing reason with "eot" and degraded with False. The options have the same names in both SDKs: drafts=True in Python is drafts: true in TypeScript.
  • speech_start opens a turn when voice activity begins. In the default auto mode, noise and other voices open turns too.
  • draft is the whole turn so far, and replaces the previous draft. Drafts come only with drafts=True (results.drafts: true in the protocol); use them for live captions.
  • user_turn_taken says the server judged that the user is taking their turn, rather than noise or someone else nearby.
  • eager_end_of_turn comes with eager=True (turn_detection.eager: true) when the turn has probably ended. Its text equals the final transcript unless turn_resumed follows, so an agent can start its reply early.
  • committed is the final transcript of the turn. user_turn: false marks a non-user turn, such as background noise or a short acknowledgement while your agent is speaking. Speaker tags such as <|speaker:0|> are stable within the session; strip them and trim the text, or pass diarize=False (results.diarize: false). Text can also contain sound tags such as [laughter]; strip them too, or turn them off with results.tag_audio_events: false. language_code is missing when the text is too short to tell, as for “Uh-huh.” above.
  • session_done is the last event. The SDK sends audio_done when your iterator is exhausted; the server answers with session_done and closes the connection with code 1000.
The SDKs skip message types they do not know; if you read the protocol yourself, ignore message types and fields you do not know, because new ones can be added. Every message is described in the WebSocket reference.
The script streams the file at real-time speed so that the events arrive as they would for live audio. To transcribe a recording you already have, use POST /v1/asr instead; it is simpler and returns the whole transcript at once.

Choose what you receive

The keyword arguments of stream_websocket(), and the fields of the first argument of convertRealtime(), set the session’s options: By default, each transcript starts with a speaker tag, as in the output above. For plain text, turn diarize off:
The stream that both calls return also sends messages during the session: finalize() commits the current turn (push-to-talk), and agent_speaking(), add_context() and configure() are for voice agents; in TypeScript they are agentSpeaking(), addContext() and configure(). To stop before the audio ends in Python, use the stream in a with block (async with with AsyncFishAudio), or call close() (await stream.aclose()). Leaving the loop is not enough while you still hold the stream in a variable: the session stays open and the SDK keeps sending your audio. In TypeScript, leave the loop or call close(). If the server has stopped reading, the Python SDK does not wait for it when closing: after half a second it cuts the connection. Read the events from one place at a time. In Python with AsyncFishAudio, a second read started while another is waiting raises RuntimeError; a read cancelled by a timeout, such as asyncio.wait_for(), loses no event, so read again. An error that ends the session raises ASRRealtimeError (from fishaudio), with the server’s error_type and the WebSocket close_code. close_code can be None when the server’s error message ended the session before the connection closed. A refused connection raises APIError, such as AuthenticationError for an invalid API key. Since fish-audio-sdk 1.10.0, a lost connection raises WebSocketConnectionError (a subclass of WebSocketError), and every one of these errors has e.trace_id to quote to support. A recoverable error does not raise: it arrives as an error event, and the session goes on. In TypeScript, the stream throws RealtimeSTTError (from fish-audio), with errorType and closeCode, and a refused connection throws an API error such as UnauthorizedError.

Without the SDK

In other languages, or to control the connection yourself, speak the WebSocket protocol directly: connect to wss://api.fish.audio/v1/asr/realtime, send a config message, wait for session_started, then send audio messages and finish with audio_done. Every message is a MessagePack map in a binary frame. Install a WebSocket client and a MessagePack library:
These scripts do the same as the recipe above, and also print the close code when the session ends (closed: 1000 done):
The audio goes in data as the raw PCM bytes, which must travel as MessagePack bin, not as base64 text: msgpack for Python packs bytes as bin by default (do not set use_bin_type=False), and @msgpack/msgpack packs a Buffer or Uint8Array as bin. The server closes a session after 30 seconds without any client message, but a WebSocket ping counts as one, and Python’s websockets sends a ping every 20 seconds by default, so the session stays open even if your audio stops; see Capture audio well for a live source that can stall. Run them with your API key in FISH_API_KEY:

Capture from a browser microphone

Never put your long-lived sk-fish- API key in a web page. For browser microphone capture, either mint a single-use ek-fish- token on your backend to connect directly via new WebSocket("wss://api.fish.audio/v1/asr/realtime", ["bearer", token]) (see Ephemeral Tokens for Browsers and Client Apps; uses the default transcribe-stream-1 because browsers cannot set the model header), or run a small relay on your server (below) that adds the Authorization and model headers and passes MessagePack frames through unchanged. The page then speaks the same protocol as the scripts above.
Install aiohttp, put both files in one folder, run python relay.py, and open http://localhost:8080.
  • The page captures audio at the device’s own sample rate and sends that rate in config; the server converts it, so the page does not resample. The rate must be one of the supported rates.
  • An AudioWorklet converts the samples to 16-bit PCM and posts 40 ms per message.
  • Stop sends audio_done, so the server commits the last turn before it closes the connection.
  • The relay forwards the API’s close code to the page. In production, add your own user authentication, per-user session limits and input checks to the relay.