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, orrealtime:write;agents:sessionis also accepted by the mint endpoint for gateway-routed sessions, while RESTPOST /v1/agent/sessionsuses ask-fish-key).
- Sufficient API credit on your account (or an active contract / package).
How it works
Step 1: Mint a token on your backend
CallPOST /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)
BrowserWebSocket APIs cannot set custom request headers. Pass ["bearer", token] as the WebSocket subprotocol list instead:
Browser
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
429or5xx, the token is already spent — mint a freshek-fish-token before retrying, because replaying the old token returns401(x-fish-error-code: ephemeral_token_already_used). - No token chaining: An
ek-fish-token cannot callPOST /v1/auth/tokens(403 ephemeral_token_cannot_mint) or management endpoints such asGET /apikey/self. - Origin pinning (browser
http/httpsonly): Whenallowed_originsis provided (up to 20 exacthttp://host[:port]orhttps://host[:port]origins; no wildcards, paths, or custom schemes such ascapacitor://,tauri://,app://, orfile://), the edge gateway requires the request’sOriginheader to match an entry in the list. Native mobile or desktop clients that send noOriginheader receive403(origin_not_allowed) whenallowed_originsis set — omitallowed_originsfor 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 reachedmax_session_duration_seconds. Note thatmax_session_duration_secondscannot extend an endpoint’s own maximum session cap (for example, 3,600 seconds / close4413on/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.
- Close code
- Gateway error codes during redemption:
Related
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.

