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

# Client Secrets

> POST /v1/ws/client_secrets: mint a short-lived secret that opens a realtime socket from a browser

Mints a short-lived client secret that a browser offers when it opens [`WS /v1/ws`](https://docs.vlm.run/gateway/api-reference/ws-realtime) or [`WS /v1/ws/systemone`](https://docs.vlm.run/gateway/api-reference/ws-systemone). Call it from your server with your API key, and send only `value` to the page. The route is served beside the sockets, so it is not in the OpenAPI schema.

```python Python theme={"theme":{"light":"github-light","dark":"dark-plus"}}
import httpx

response = httpx.post(
    "https://gateway.vlm.run/v1/ws/client_secrets",
    headers={"Authorization": "Bearer <VLMRUN_API_KEY>"},
    json={"expires_after": {"anchor": "created_at", "seconds": 600}},
)
print(response.status_code, response.json())
```

```typescript Node.js theme={"theme":{"light":"github-light","dark":"dark-plus"}}
const response = await fetch("https://gateway.vlm.run/v1/ws/client_secrets", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.VLMRUN_API_KEY}`,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({ expires_after: { anchor: "created_at", seconds: 600 } }),
});
console.log(response.status, await response.json());
```

```bash cURL theme={"theme":{"light":"github-light","dark":"dark-plus"}}
curl -X POST https://gateway.vlm.run/v1/ws/client_secrets \
  -H "Authorization: Bearer $VLMRUN_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"expires_after": {"anchor": "created_at", "seconds": 600}}'
```

The request mints a secret for the caller of the API key that is good for 600 seconds, and returns `200` with the secret:

```json theme={"theme":{"light":"github-light","dark":"dark-plus"}}
{ "object": "realtime.client_secret", "value": "ek_<secret>", "expires_at": 1795000600 }
```

## Request body

The body is optional. With no body, the secret is good for 600 seconds.

### expires\_after

`object`, optional, default: 600 seconds from now.

| Field | Type | Description |
| - | - | - |
| `anchor` | `"created_at"` | The time `seconds` counts from. `created_at` is the only value, and the default. |
| `seconds` | `integer` | The secret's lifetime in seconds, from 1 to 7200. Above 7200 is a `400`. `0` or less is a `422`. |

## Response

| Field | Type | Description |
| - | - | - |
| `object` | `"realtime.client_secret"` | The object type. |
| `value` | `string` | The secret. It starts with `ek_`. |
| `expires_at` | `integer` | When the secret stops opening sockets, in Unix seconds. |

## Open a socket with the secret

Offer the secret as a WebSocket subprotocol, beside `vlmrun.realtime`:

```javascript Browser theme={"theme":{"light":"github-light","dark":"dark-plus"}}
const ws = new WebSocket("wss://gateway.vlm.run/v1/ws", ["vlmrun.realtime", `vlmrun.bearer.${value}`]);
```

The page opens a frames socket as the caller who minted the secret. The server accepts the `vlmrun.realtime` subprotocol and never echoes the secret. A server-side client can also send the secret as `Authorization: Bearer ek_...`.

* **The secret is its caller.** A socket opened with it has the plan, session caps, and billing of the API key that minted it.
* **One secret, many sockets.** A secret opens any number of sockets until `expires_at`, so a page can reconnect on it. The expiry bounds the upgrade only: a socket opened in time runs its full session.
* **Only a client secret goes in the subprotocol.** The `Sec-WebSocket-Protocol` header can be logged by a proxy, so an API key or a JWT in `vlmrun.bearer.*` is a `401`. The one exception is the anonymous key on `/v1/ws`, `vlmrun.bearer.vlmrun`. See [Anonymous sessions](https://docs.vlm.run/gateway/realtime.md#anonymous-sessions).
* **Expired or unknown secrets** are a `401` at the upgrade. Mint another one.

## Errors

| Status | When |
| - | - |
| `400` | `expires_after.seconds` is above 7200. |
| `401` | No API key, the anonymous key `vlmrun`, or a client secret as the key. A client secret mints no other secret. |
| `422` | The body does not validate, for example `seconds` of `0`. |
| `429` | The key is over its request rate limit. A mint counts as a model call. See [Rate Limits](https://docs.vlm.run/gateway/rate-limits.md). |

## Related

> [card] [Webcam from the Browser](https://docs.vlm.run/gateway/guides/live-webcam.md)
>
> A page that mints a secret on your server and streams the camera to the Gateway.

> [card] [WS /v1/ws reference](https://docs.vlm.run/gateway/api-reference/ws-realtime.md)
>
> Every event on the frames socket.


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