Add better security to Realtime API

The following code shows a basic example of calling the Realtime api from the client browser:

  // Start the session using the Session Description Protocol (SDP)
  const offer = await pc.createOffer();
  await pc.setLocalDescription(offer);

  const baseUrl = "https://api.openai.com/v1/realtime";
  const model = "gpt-4o-realtime-preview-2024-12-17";
  const sdpResponse = await fetch(`${baseUrl}?model=${model}`, {
    method: "POST",
    body: offer.sdp,
    headers: {
      Authorization: `Bearer ${EPHEMERAL_KEY}`,
      "Content-Type": "application/sdp"
    },
  });

Note how the model is set to “gpt-4o-mini”. Specifying a model from the client side is a security risk. Hackers can just write their own code and use the cookie for authentication and then change the model to be one that is much more expensive, when more expensive models do become available. Then out of malicious intent, they could run a bot to make many API calls using that more expensive model to run up the charges on their billing.

The model should never be specified in ANY client-side api. The solution for the Realtime API is to have OpenAI’s backend keep track of the model by associating it with the ephemeral token that it issues to the client’s server. When the Realtime API is called, OpenAI’s own servers will already know which model to use based on the ephemeral token.