WebSocket tokens and authentication
Keep long-lived API keys on your server. Browsers receive temporary credentials. ASR compatibility tickets and generic ephemeral tokens use different fields and authentication flows.
Service endpoints
Service URL: https://api.fourhz.ai
| Method / Protocol | Path | Content-Type / Frames | Auth |
|---|---|---|---|
| GET | /v1/token | query | Authorization: <API Key> |
| POST | /auth/v1/ephemeral-tokens | application/json | Authorization: <API Key> |
ASR single-use token
curl -X GET 'https://api.fourhz.ai/v1/token?expires_in_seconds=60' \
-H "Authorization: $FOURHZ_API_KEY"# pip install requests
import os
import requests
from contextlib import ExitStack
url = "https://api.fourhz.ai/v1/token?expires_in_seconds=60"
headers = {"Authorization": os.environ["FOURHZ_API_KEY"]}
with ExitStack() as stack:
response = requests.request("GET", url, headers=headers, timeout=300)
response.raise_for_status()
print(response.json() if response.content else {"status": response.status_code})// Node.js 22+
import {readFile} from 'node:fs/promises';
const url = "https://api.fourhz.ai/v1/token?expires_in_seconds=60";
const headers = {"Authorization": process.env.FOURHZ_API_KEY};
const response = await fetch(url, {method: "GET", headers, signal: AbortSignal.timeout(300000)});
if (!response.ok) throw new Error(await response.text());
console.log(response.status === 204 ? {status: 204} : await response.json());{"token":"temporary-token"}Run on your server with the raw key in Authorization, without Bearer. The token is single-use; mint another for every reconnect. expires_in_seconds: 1–600, default 60. Connection-ticket expiry is distinct from recording duration.
const params = new URLSearchParams({token, sample_rate:"16000", encoding:"pcm_s16le", speech_model:"fourhz-spectrum"});
const ws = new WebSocket("wss://api.fourhz.ai/v1/asr/realtime?" + params);Portal exchange for this Playground
Sign in to Portal and POST /api/playground/ephemeral-token using your login session and API key ID, with resource asr.clean_stream. Pass access_token in the ASR WebSocket token query parameter.
Response fields
| Field | Type | Meaning and availability |
|---|---|---|
token | string | Single-use ASR ticket from GET /v1/token; pass as the token query parameter. |
access_token | string | Temporary ASR credential returned by the Playground token exchange. |
resource | string | Authorized resource; not interchangeable across resources. |
token_type | string | Generic credential type; does not change raw-key endpoint authentication. |
expires_in | integer | Generic credential TTL in seconds, used for connection authentication. |
expires_at | string | Generic credential expiration in RFC3339. |
scope / protocol | string | Resource scope and protocol returned by the server. |
max_session_seconds | integer? | WebSocket session duration limit, separate from the credential TTL. |
Expiry, reconnects and credential boundaries
Do not interchange credentials across accounts, keys or resources. Mint a new ticket after connection failure or disconnection. A disconnect does not prove that audio was unprocessed or unbilled; do not automatically replay the full recording. Do not persist tokens or full credential-bearing URLs in logs or analytics.