Skip to content
API Reference

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

HTTP
POST /v1/auth/browser-token
X-API-Key: YOUR_API_KEY

Call this from your server, never from the browser. The exchange is available on hosted deployments with browser tokens enabled.

Bash
curl -X POST https://api.kugelaudio.com/v1/auth/browser-token \
  -H "X-API-Key: $KUGELAUDIO_API_KEY"
JSON
{
  "token": "eyJhbGciOi...",
  "expires_at": 1795180800,
  "expires_in": 300
}
StatusMeaning
200Token issued.
401The key is missing, invalid, supplied in the query string, or is itself a browser token.
404Browser tokens are not enabled for this deployment.
503The 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:

TypeScript
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:

JavaScript
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

  1. Keep your key on your server only.
  2. Add an endpoint to your backend that calls POST /v1/auth/browser-token and returns the token to your signed-in user.
  3. In the browser, create the client with that token and isToken: true instead of the key.
  4. 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.