I obtain an ephemeral key from my server, which is set up as follows:
// server context above ...
const realtimeBaseUrl = 'https://api.openai.com/v1/realtime'
const response = await fetch(`${realtimeBaseUrl}/transcription_sessions`, {
method: "POST",
headers: {
Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
"Content-Type": "application/json",
},
}
return response.client_secret.value
// server context below ...
I’m using WebRTC for real-time transcription and received a 400 error when sending the SDP offer, see below.
// client context above ...
const ephemeralToken = await getEphemeralToken();
// Establish peer connection
const pc = new RTCPeerConnection();
// Add local audio track for mic input
const stream = await navigator.mediaDevices.getUserMedia({ audio: true });
pc.addTrack(stream.getTracks()[0]);
// Data channel for sending and receiving messages
const dataChannel = pc.createDataChannel("oai-events");
dataChannel.onmessage = handleDataChannelMessage;
// Create offer & set local description
const offer = await pc.createOffer();
await pc.setLocalDescription(offer);
// Send SDP offer to OpenAI Realtime
const baseUrl = "https://api.openai.com/v1/realtime";
const model = "gpt-4o-mini-transcribe";
const url = `${baseUrl}?model=${model}`;
const sdpResponse = await fetch(url, {
method: "POST",
body: offer.sdp,
headers: {
Authorization: `Bearer ${ephemeralToken}`,
"Content-Type": "application/sdp",
},
});
// Set remote description
const answerSdp = await sdpResponse.text();
console.log("SDP Response:", answerSdp);
await pc.setRemoteDescription({ type: "answer", sdp: answerSdp });
// client context below ...
error 400 (Bad Request) via POST to https://api.openai.com/v1/realtime?model=gpt-4o-mini-transcribe
SDP Response: {
"error": {
"message": "",
"type": "",
"code": "",
"param": ""
}
}
Interestingly, I tested switch the server’s ephemeral token endpoint from /transcription_sessions to /sessions, the WebRTC connection could be established.