Skip to main content
Never embed a long-lived sk-fish- API key in browser JavaScript, mobile apps, or desktop binaries. Instead, mint a short-lived, one-time (single-use) ek-fish- ephemeral token on your backend and hand it to your client so the client can connect directly to Fish Audio with low latency and zero audio proxying.

Prerequisites

  • A Scoped API key created on API Keys with:
    • tokens:mint, and
    • At least one ephemeral-capable inference scope you want to delegate (tts:write, stt:write, voice_design:write, or realtime:write; agents:session is also accepted by the mint endpoint for gateway-routed sessions, while REST POST /v1/agent/sessions uses a sk-fish- key).
  • Sufficient API credit on your account (or an active contract / package).
Classic API keys cannot mint ephemeral tokens (403 classic_key_cannot_mint). Passing any non-ek-fish- credential in ?token=, or passing a long-lived sk-fish- key in Sec-WebSocket-Protocol, is rejected with 400 (x-fish-error-code: long_lived_key_in_query_forbidden).

How it works

Step 1: Mint a token on your backend

Call POST /v1/auth/tokens from your trusted server right before the client starts a session. Restrict scopes, ttl_seconds, max_session_duration_seconds, and (for browser clients) allowed_origins as tightly as your use case allows.

Step 2: Connect directly from the client

Browser WebSocket (Realtime STT and Live TTS)

Browser WebSocket APIs cannot set custom request headers. Pass ["bearer", token] as the WebSocket subprotocol list instead:
Browser
During the WebSocket handshake, the edge gateway extracts the ek-fish- token from Sec-WebSocket-Protocol, strips it before forwarding upstream, and echoes only Sec-WebSocket-Protocol: bearer in the 101 Switching Protocols response per RFC 6455.
Model selection on browser WebSockets: Because browser WebSocket connections cannot send custom headers like model, direct browser connections use each WebSocket endpoint’s default model (transcribe-stream-1 on /v1/asr/realtime, s2.1-pro on /v1/tts/live). Native mobile/desktop clients and server relays can send the model header on WebSocket upgrades, and browser HTTP fetch requests can send model directly via CORS.

Browser HTTP fetch (TTS, STT, Voice Design)

Inference endpoints served through the edge gateway handle browser CORS preflights (OPTIONS), set Vary: Origin, and expose Fish Audio diagnostic headers (x-fish-trace-id, x-fish-error-code, x-fish-error-source, x-fish-free-model, x-fish-required-scope, ratelimit-limit-concurrency, ratelimit-current-concurrency, retry-after). Send the ephemeral token in the standard Authorization: Bearer header (you can also set the model header normally):
Browser
Each ek-fish- token is single-use: mint a fresh token per request or per WebSocket connection. If a client cannot set headers or subprotocols, you can also pass ?token=ek-fish-... in the query string — the edge gateway scrubs token from the URI before logging or forwarding upstream.

Security controls and runtime behavior

  • Atomic single-use redemption (mint a fresh token on retries): A token is consumed atomically on its first redemption, before concurrency checks and upstream forwarding. If that request fails later with 429 or 5xx, the token is already spent — mint a fresh ek-fish- token before retrying, because replaying the old token returns 401 (x-fish-error-code: ephemeral_token_already_used).
  • No token chaining: An ek-fish- token cannot call POST /v1/auth/tokens (403 ephemeral_token_cannot_mint) or management endpoints such as GET /apikey/self.
  • Origin pinning (browser http/https only): When allowed_origins is provided (up to 20 exact http://host[:port] or https://host[:port] origins; no wildcards, paths, or custom schemes such as capacitor://, tauri://, app://, or file://), the edge gateway requires the request’s Origin header to match an entry in the list. Native mobile or desktop clients that send no Origin header receive 403 (origin_not_allowed) when allowed_origins is set — omit allowed_origins for non-browser clients.
  • Live parent key re-check: At redemption time, Fish Audio re-verifies the parent scoped API key. Revoking the parent key, removing a required scope, or hitting its USD spend limit immediately blocks all unredeemed ek-fish- tokens minted by that key.
  • WebSocket session guardrails:
    • Close code 4008 (session_duration_exceeded): the connection reached max_session_duration_seconds. Note that max_session_duration_seconds cannot extend an endpoint’s own maximum session cap (for example, 3,600 seconds / close 4413 on /v1/asr/realtime) — whichever limit is lower fires first.
    • Close code 4002 (api_key_spend_limit_exceeded): the parent API key reached its USD spend limit mid-session.
  • Gateway error codes during redemption:
Expired or already-used tokens are automatically removed after expires_at, after which lookups return invalid_ephemeral_token. Handle all three 401 ephemeral codes (invalid_ephemeral_token, ephemeral_token_already_used, ephemeral_token_expired) the same way: request a fresh ek-fish- token from your backend.

POST /v1/auth/tokens

Full request, response, rate limit, and error reference.

API Key Types, Scopes, and Spend Limits

Configure Scoped keys, all 12 scopes, and per-key USD spend caps.

WebSocket Realtime Speech to Text

Connect from browsers or servers to wss://api.fish.audio/v1/asr/realtime.

WebSocket TTS Streaming

Stream real-time text to speech over WebSocket.