sk_ key in browser code. Instead, your server mints a short-lived ephemeral key (ek_...) and sends it to the page. The browser connects with that key, and it stops working when it expires.
1
Mint a client key on your server
Your server calls The response has the key and its expiry:
POST /v1/realtime/client_secrets with your sk_ key. Set the session config here. The config is applied when the browser connects.2
Connect from the browser
Pass the
ek_ key to the client. With @openai/agents-realtime, call connect with apiKey:The SDK sends the key as a WebSocket subprotocol. If you connect by hand, the subprotocols are
realtime, openai-insecure-api-key.<ek_...> and openai-beta.realtime-v1.3
Refresh before expiry
Keys live from 10 seconds to 2 hours (default 600). A key can be reused until it expires. Mint a new one before the old one expires for long sessions, and once per page load for short ones.
Security notes
The
session config you send to client_secrets is applied when the browser connects. To use different settings, mint a new key.
Errors
Errors use the OpenAI envelope:Related
- Create ephemeral key reference
- WebSocket connection reference