TOOL WIKI · VOICE & REALTIME
OpenAI Realtime
Supports realtime speech interactions with conversation state and tool use.
Jump to a section 10 sections
EXAMPLE USE CASE
Ask a fixed policy question through a browser microphone
A spoken support interface needs capture, turn handling, transcription or playback with explicit media formats and a verifiable outcome.
- Input
- WebRTC microphone audio and the demo’s 30-day returns-policy instruction.
- Expected output
- A spoken policy answer plus a connection that Stop can close.
- Success check: Open http://localhost:3000 and start the microphone. Ask: Can I return an item after twenty days?
- Try a failure: Deny microphone permission or stop mid-reply; no audio track should remain active.
Example scope: Actual browser/backend/Realtime integration; the permanent key remains on the backend and no order-changing tools are enabled.
Official reference · OpenAI Realtime
Obtain microphone consent and handle audio devices and interruption.
A conversational voice interface with a direct speech-to-speech session. Issue short-lived client credentials and keep permanent keys server-side.
tool callTo 04 · Business toolscontextTo 05 · State + contextmeasureTo 06 · Quality + latencyUse the speech and reasoning services required by the selected architecture.
Resolve the actual actor and resource before executing actions requested during a call.
Keep confirmed business outcomes separate from transient audio buffers.
Result or stopping pointTest noisy audio, overlapping speech, tool timeouts, and reconnection.
Result or stopping point
A conversational voice interface with a direct speech-to-speech session. Issue short-lived client credentials and keep permanent keys server-side.
When to consider it
- A conversational voice interface with a direct speech-to-speech session.
A design decision to make
- Issue short-lived client credentials and keep permanent keys server-side.
Prerequisites and setup
- Node.js 22+ and an OpenAI API key in the server environment. Requests may incur API usage charges.
- Choose an enabled Realtime model. This local transport lab establishes audio; add authenticated tools separately.
- Use localhost for the microphone permission prompt. Never put the permanent API key in browser JavaScript.
mkdir -p public
npm init -y
npm install express
export OPENAI_API_KEY="YOUR_SERVER_KEY"
export OPENAI_REALTIME_MODEL="YOUR_ENABLED_REALTIME_MODEL"Try a small integration
server.mjs
import express from 'express';
const app = express();
app.use(express.text({type:'application/sdp', limit:'64kb'}));
app.use(express.static('public'));
app.post('/session', async (req,res) => {
if (req.get('origin') !== 'http://localhost:3000') return res.sendStatus(403);
try {
const data = new FormData();
data.set('sdp', req.body);
data.set('session', JSON.stringify({type:'realtime', model:process.env.OPENAI_REALTIME_MODEL, instructions:"You are a concise voice FAQ assistant. Demo policy: returns are accepted within 30 days. Use only this policy; do not invent order statuses or dates. Ask a clarification when the request is outside it."}));
const upstream = await fetch('https://api.openai.com/v1/realtime/calls', {
method:'POST', body:data,
headers:{Authorization:'Bearer '+process.env.OPENAI_API_KEY},
signal:AbortSignal.timeout(15000)
});
if (!upstream.ok) { console.error('Session status',upstream.status); return res.sendStatus(502); }
res.type('application/sdp').set('Cache-Control','no-store').send(await upstream.text());
} catch { res.sendStatus(502); }
});
app.listen(3000,'127.0.0.1',()=>console.log('Open http://localhost:3000'));public/index.html
<!doctype html>
<html lang="en"><meta charset="utf-8"><title>Local voice transport lab</title>
<button id="start">Start microphone</button><button id="stop" disabled>Stop</button>
<p id="status">Disconnected</p><audio id="audio" autoplay controls></audio>
<script type="module">
let pc, stream;
const status=document.querySelector('#status');
const stop=()=>{ stream?.getTracks().forEach(t=>t.stop()); pc?.close();
document.querySelector('#stop').disabled=true; document.querySelector('#start').disabled=false;
status.textContent='Disconnected'; };
document.querySelector('#stop').onclick=stop;
document.querySelector('#start').onclick=async()=>{
document.querySelector('#start').disabled=true;
try {
stream=await navigator.mediaDevices.getUserMedia({audio:true});
pc=new RTCPeerConnection();
pc.ontrack=e=>{document.querySelector('#audio').srcObject=e.streams[0];};
pc.onconnectionstatechange=()=>{status.textContent=pc.connectionState;};
pc.addTrack(stream.getAudioTracks()[0],stream);
const channel=pc.createDataChannel('events');
channel.onmessage=e=>{const event=JSON.parse(e.data); if(event.type==='error')status.textContent='Provider error';};
await pc.setLocalDescription(await pc.createOffer());
const response=await fetch('/session',{method:'POST',headers:{'Content-Type':'application/sdp'},body:pc.localDescription.sdp});
if(!response.ok)throw Error('Session failed');
await pc.setRemoteDescription({type:'answer',sdp:await response.text()});
document.querySelector('#stop').disabled=false;
} catch(error){stop();status.textContent=error.message;}
};
window.addEventListener('pagehide',stop);
</script></html>Run and check the result
mkdir -p public
# Save the HTML file inside public/ before starting.
node server.mjs- Open http://localhost:3000 and start the microphone. Ask: Can I return an item after twenty days?
- The spoken answer uses the supplied 30-day policy. A question about a specific order must not produce an invented status.
- Stop releases media tracks and the peer connection. The browser receives an SDP answer; the permanent API key stays on the backend.
Common failures
- A 502 means the server could not establish the provider session. Inspect the upstream status without logging credentials or raw audio.
- No audio: check the browser permission, autoplay control, microphone device and network WebRTC support.
Production decisions
- Authenticate and rate-limit session creation, allow only approved models, and enforce usage and duration limits.
- Route business tool execution through an authorized server; confirm consequential operations using the exact spoken proposal.
- Handle interruption, reconnection, transcripts, accessibility, recording consent and explicit retention policies.
Keep exploring
Explore the same layer
Compare candidates on your own workload. This entry describes a product’s role; it is not a claim that products in the category are interchangeable.