> ## Documentation Index
> Fetch the complete documentation index at: https://aircaps.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Client-side streaming

> Create a short-lived token on your backend, then stream audio directly from a browser without exposing your API key.

Your backend creates a client token using your API key. Your browser uses that token to open an AirCaps WebSocket and send audio directly to AirCaps. Your backend does not need to relay the audio.

This works with **AirCaps Live 1** (`aircaps-live-1`) and **A5S Streaming v1** (`a5s-streaming-v1`, early access), subject to your account's model access.

## 1. Create a token on your backend

Call [`POST /v1/realtime/token`](https://aircaps.ai/docs/api-reference/realtime-token) with your server-side API key:

```bash theme={null}
curl --fail-with-body https://api.aircaps.com/v1/realtime/token \
  -H "Authorization: Bearer $AIRCAPS_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"expires_in": 60}'
```

```json Response: 201 Created theme={null}
{
  "object": "realtime_token",
  "token": "aircaps_rt_...",
  "expires_at": "2026-11-24T23:01:00Z",
  "expires_in": 60
}
```

`expires_in` accepts **60–3,600 seconds** and defaults to **3,600** when omitted. Use 60 seconds when the browser will connect immediately. The response reports the remaining lifetime, which may be slightly shorter than requested.

Expose this call through an authenticated route in your application, such as `POST /api/aircaps-token`. Check that the signed-in user may start a stream, return the token to that user, and set `Cache-Control: no-store` on your response. Keep the AirCaps API key on your backend.

```javascript Backend: inside your authenticated route theme={null}
const response = await fetch("https://api.aircaps.com/v1/realtime/token", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.AIRCAPS_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ expires_in: 60 }),
});

if (!response.ok) {
  throw new Error(`AirCaps token request failed: ${response.status}`);
}

const { token, expires_at, expires_in } = await response.json();
// Return these fields as JSON to your authenticated user, with Cache-Control: no-store.
```

## 2. Connect from the browser

`/api/aircaps-token` below is the route you implement in your own application, not an AirCaps endpoint.

```javascript Browser theme={null}
const tokenResponse = await fetch("/api/aircaps-token", {
  method: "POST",
  credentials: "same-origin",
});
if (!tokenResponse.ok) throw new Error("Could not create a streaming token");
const { token } = await tokenResponse.json();

const url = new URL("wss://api.aircaps.com/v1/realtime");
url.searchParams.set("token", token);
const ws = new WebSocket(url);
let ready = false;

ws.addEventListener("open", () => {
  ws.send(JSON.stringify({
    type: "start",
    model: "aircaps-live-1",
    encoding: "pcm_s16le",
    sample_rate: 16000,
    channels: 1,
  }));
});

ws.addEventListener("message", ({ data }) => {
  const message = JSON.parse(data);
  if (message.type === "ready") ready = true;
  if (message.type === "transcript") console.log(message.text);
  if (message.type === "error") console.error(message.code, message.message);
  if (message.type === "finished") ready = false;
});
ws.addEventListener("close", () => { ready = false; });

function sendAudio(pcmFrame) {
  if (!ready || ws.readyState !== WebSocket.OPEN) {
    throw new Error("Wait for ready before sending audio");
  }
  ws.send(pcmFrame);
}
```

No `Authorization` header or `access_token` field is needed on this WebSocket. A valid client token allows connections from your application's browser origin.

After `ready`, send raw PCM in real time: 16 kHz, mono, signed 16-bit little-endian, normally in 20 ms frames (640 bytes). For Live 1, send `{"type":"finalize"}` at each turn's end, or configure [end-of-turn detection](https://aircaps.ai/docs/guides/live-1.md#end-of-turn-detection). To end the session, send `{"type":"finish"}` and wait for `finished` before closing.

For A5S Streaming v1, set `model` to `"a5s-streaming-v1"` and add the desired `language` and `diarization` options to `start`. See the [streaming guide](https://aircaps.ai/docs/guides/streaming.md) and [WebSocket protocol](https://aircaps.ai/docs/api-reference/realtime.md).

## WebSocket address

Use **`wss://api.aircaps.com/v1/realtime`** with the `token` query parameter, as shown above. This address accepts client tokens for both **AirCaps Live 1** and **A5S Streaming v1**, subject to your account's model access. Select the model in your `start` message; no endpoint lookup is needed for this flow.

[`GET /v1/realtime/endpoint`](https://aircaps.ai/docs/api-reference/realtime-endpoint) is available for server-side clients connecting with an API key. Not every direct address it returns accepts client tokens yet, so use the address above for token-authenticated clients.

## Expiry, permissions, and reconnecting

* **Streaming only.** Client tokens work on the `/v1/realtime` WebSocket. REST endpoints reject them with `401 unauthorized`, including token creation and endpoint lookup.
* **Same account access.** Model access, billing, and session limits are inherited from the API key that created the token. The token is not restricted to a particular model or application user.
* **Expiry controls new connections.** Connect before `expires_at`. Expiry does not close an already-authenticated stream; its normal session limit still applies, up to one hour. Obtain a fresh token when reconnecting after expiry.
* **Tokens are reusable until expiry.** They are not single-use. Create a fresh, short-lived token for each intended session, and keep it out of logs and persistent browser storage.
* **Revocation.** Revoking the parent API key prevents new token-authenticated sessions within the authentication cache interval, up to 60 seconds. Sessions already open continue until they end.

Missing, malformed, expired, or revoked tokens receive `unauthorized` and WebSocket close code `4401`. Do not retry indefinitely with the same rejected token; obtain a fresh one from your backend.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.