Realtime API - OpenAI API
Realtime API - OpenAI API
The OpenAI Realtime API enables low-latency, multimodal interactions including speech-to-
speech conversational experiences and real-time transcription.
This API works with natively multimodal models such as GPT-4o and GPT-4o mini, offering
capabilities such as real-time text and audio processing, function calling, and speech
generation, and with the latest transcription models GPT-4o Transcribe and GPT-4o mini
Transcribe.
Using WebRTC, which is ideal for client-side applications (for example, a web app)
Using WebSockets, which is great for server-to-server applications (from your backend or
if you're building a voice agent over phone for example)
Start by exploring examples and partner integrations below, or learn how to connect to the
Realtime API using the most relevant method for your use case below.
Example applications
Check out one of the example applications below to see the Realtime API in action.
Realtime Console
To get started quickly, download and configure the Realtime console demo. See events flowing
back and forth, and inspect their contents. Learn how to execute custom logic with function
calling.
https://platform.openai.com/docs/guides/realtime#connect-with-webrtc 1/9
18/4/25, 19:17 Realtime API - OpenAI API
A demonstration of handoffs between Realtime API voice agents with reasoning model validation.
Partner integrations
Check out these partner integrations, which use the Realtime API in frontend applications and
telephony use cases.
Use cases
The most common use case for the Realtime API is to build a real-time, speech-to-speech,
conversational experience. This is great for building voice agents and other voice-enabled
applications.
The Realtime API can also be used independently for transcription and turn detection use
cases. A client can stream audio in and have Realtime API produce streaming transcripts
when speech is detected.
Both use-cases benefit from built-in voice activity detection (VAD) to automatically detect
when a user is done speaking. This can be helpful to seamlessly handle conversation turns, or
to analyze transcriptions one phrase at a time.
Realtime Speech-to-Speech
Learn to use the Realtime API for streaming speech-to-speech conversations.
Realtime Transcription
https://platform.openai.com/docs/guides/realtime#connect-with-webrtc 2/9
18/4/25, 19:17 Realtime API - OpenAI API
Depending on your use case (conversation or transcription), you should initialize a session in
different ways. Use the switcher below to see the details for each case.
Overview
In scenarios where you would like to connect to a Realtime model from an insecure client over
the network (like a web browser), we recommend using the WebRTC connection method.
WebRTC is better equipped to handle variable connection states, and provides a number of
convenient APIs for capturing user audio inputs and playing remote audio streams from the
model.
Connecting to the Realtime API from the browser should be done with an ephemeral API key,
generated via the OpenAI REST API. The process for initializing a WebRTC connection is as
follows (assuming a web browser client):
https://platform.openai.com/docs/guides/realtime#connect-with-webrtc 3/9
18/4/25, 19:17 Realtime API - OpenAI API
While it is technically possible to use a standard API key to authenticate client-side WebRTC
sessions, this is a dangerous and insecure practice because it leaks your secret key. Standard API
keys grant access to your full OpenAI API account, and should only be used in secure server-side
environments. We recommend ephemeral keys in client-side applications whenever possible.
Connection details
URL https://api.openai.com/v1/realtime
Query model
Parameters
Substitute EPHEMERAL_KEY with an ephemeral API token - see below for details on how
to generate one.
The following example shows how to initialize a WebRTC session (including the data channel
to send and receive Realtime API events). It assumes you have already fetched an ephemeral
API token (example server code for this can be found in the next section).
https://platform.openai.com/docs/guides/realtime#connect-with-webrtc 4/9
18/4/25, 19:17 Realtime API - OpenAI API
22 const dc = pc.createDataChannel("oai-events");
23 dc.addEventListener("message", (e) => {
24 // Realtime server events appear here!
25 console.log(e);
26 });
27
28 // Start the session using the Session Description Protocol (SDP)
29 const offer = await pc.createOffer();
30 await pc.setLocalDescription(offer);
31
32 const baseUrl = "https://api.openai.com/v1/realtime";
33 const model = "gpt-4o-realtime-preview-2024-12-17";
34 const sdpResponse = await fetch(`${baseUrl}?model=${model}`, {
35 method: "POST",
36 body: offer.sdp,
37 headers: {
38 Authorization: `Bearer ${EPHEMERAL_KEY}`,
39 "Content-Type": "application/sdp"
40 },
41 });
42
43 const answer = {
44 type: "answer",
45 sdp: await sdpResponse.text(),
46 };
47 await pc.setRemoteDescription(answer);
48 }
49
50 init();
The WebRTC APIs provide rich controls for handling media streams and input devices. For
more guidance on building user interfaces on top of WebRTC, refer to the docs on MDN.
To create an ephemeral token to use on the client-side, you will need to build a small server-
side application (or integrate with an existing one) to make an OpenAI REST API request for an
ephemeral key. You will use a standard API key to authenticate this request on your backend
server.
Below is an example of a simple Node.js express server which mints an ephemeral API key
using the REST API:
https://platform.openai.com/docs/guides/realtime#connect-with-webrtc 5/9
18/4/25, 19:17 Realtime API - OpenAI API
You can create a server endpoint like this one on any platform that can send and receive HTTP
requests. Just ensure that you only use standard OpenAI API keys on the server, not in the
browser.
To learn how to send and receive events over the WebRTC data channel, refer to the Realtime
conversations guide.
Overview
In a server-to-server integration with Realtime, your backend system will connect via
WebSocket directly to the Realtime API. You can use a standard API key to authenticate this
connection, since the token will only be available on your secure backend server.
https://platform.openai.com/docs/guides/realtime#connect-with-webrtc 6/9
18/4/25, 19:17 Realtime API - OpenAI API
WebSocket connections can also be authenticated with an ephemeral client token (as shown above
in the WebRTC section) if you choose to connect to the Realtime API via WebSocket on a client
device.
Standard OpenAI API tokens should only be used in secure server-side environments.
Connection details
Speech-to-Speech Transcription
URL wss://api.openai.com/v1/realtime
Query model
Parameters
Realtime model ID to connect to, like gpt-4o-realtime-preview-2024-12-17
Substitute YOUR_API_KEY with a standard API key on the server, or an ephemeral token
on insecure clients (note that WebRTC is recommended for this use case).
OpenAI-Beta: realtime=v1
Below are several examples of using these connection details to initialize a WebSocket
connection to the Realtime API.
https://platform.openai.com/docs/guides/realtime#connect-with-webrtc 7/9
18/4/25, 19:17 Realtime API - OpenAI API
To learn how to send and receive events over Websockets, refer to the Realtime conversations
guide.
https://platform.openai.com/docs/guides/realtime#connect-with-webrtc 8/9
18/4/25, 19:17 Realtime API - OpenAI API
https://platform.openai.com/docs/guides/realtime#connect-with-webrtc 9/9