Browser tokens
Exchange a project API key for a short-lived token that a browser can use for text-to-speech, speech-to-text and speech enhancement WebSockets.
On this page
A browser cannot send headers when it opens a WebSocket, and a project API key in the URL is visible to everyone using the page. Instead, your server holds the key and hands the browser a browser token: a signed credential that works like the key, for a few minutes.
What a token can do
- It acts as the key that issued it: same organization, same project, same permissions as the key had when the token was issued. A key limited to text-to-speech gives a token limited to text-to-speech.
- It lasts up to 5 minutes by default. Deployments can configure a lifetime of
up to 15 minutes. A token expires sooner when the issuing key expires sooner.
After expiry, connections with it are refused with
401. A connection that is already open keeps running; only new connections need a new token. - It works on the text-to-speech, speech-to-text and speech enhancement WebSockets and on their REST endpoints.
- It cannot be exchanged for another token, so a leaked token cannot renew itself.
- Revoking the key stops new tokens at once; tokens already issued keep working until their own expiry.
Exchange a key for a token
Call this from your server, never from the browser. The exchange is available on hosted deployments with browser tokens enabled.
curl -X POST https://api.kugelaudio.com/v1/auth/browser-token \
-H "X-API-Key: $KUGELAUDIO_API_KEY"{
"token": "eyJhbGciOi...",
"expires_at": 1795180800,
"expires_in": 300
}| Status | Meaning |
|---|---|
200 | Token issued. |
401 | The key is missing, invalid, supplied in the query string, or is itself a browser token. |
404 | Browser tokens are not enabled for this deployment. |
503 | The key lookup is temporarily unavailable. Retry after the interval in Retry-After. |
Use the token in the browser
Pass the token as token in the WebSocket URL. With the JavaScript SDK, set
isToken:
import { KugelAudio } from 'kugelaudio';
// `token` comes from your server's call to POST /v1/auth/browser-token.
const client = new KugelAudio({ apiKey: token, isToken: true });Without the SDK:
const ws = new WebSocket(`wss://api.kugelaudio.com/ws/tts?token=${token}`);Fetch a fresh token shortly before each connection. A token that has expired
answers 401 with the message Browser token has expired.
Migrating from a key in the browser
- Keep your key on your server only.
- Add an endpoint to your backend that calls
POST /v1/auth/browser-tokenand returns thetokento your signed-in user. - In the browser, create the client with that token and
isToken: trueinstead of the key. - Rotate any key that was shipped to a browser.
Clients that still send a key in the URL (?api_key=) keep working while you
migrate, but the key is visible in browser history and proxy logs. Move them to
tokens.