const pc = new RTCPeerConnection();
const answer = await fetch(REALTIME_URL, {
method: 'POST',
headers: { Authorization: 'Bearer ' + API_KEY },
body: offer.sdp,
});const { token } = await fetch('/api/realtime-token').then((r) => r.json());
const answer = await fetch(REALTIME_URL, {
method: 'POST',
headers: { Authorization: 'Bearer ' + token },
body: offer.sdp,
});The browser gets a short-lived token from your server, so the main API key never ships in frontend code.
WebRTC is usually better from the browser, as it handles audio, jitter and echo cancellation. WebSocket is simpler for server-side connections.
Yes, most realtime APIs accept and return text as well as audio, but a regular streaming HTTP API is usually simpler for text-only chat.
Sessions often have a maximum duration and drop on network changes. Handle the close event, reconnect with a new token and restore the conversation state.