Realtime API - SDP response says invalid realtime token

I’m intermittently getting an error trying to connect to the realtime API.
My app was working, but now suddenly I’m getting errors
based on the realtime console code here

Is it the case that this realtime (preview) API is very flaky and goes up and down?

this comes from the more simple SDK demo
the EPHEMERAL_KEY seems to be a valid key as far as I can see.

    const sdpResponse = await fetch(`${baseUrl}?model=${model}`, {
      method: 'POST',
      body: offer.sdp,
      headers: {
        Authorization: `Bearer ${EPHEMERAL_KEY}`,
        'Content-Type': 'application/sdp'
      }
    })

    const sdp = await sdpResponse.text()
    console.log('sdp', sdp) // invalid

gives me:

sdp {
    "error": {
        "message": "Invalid realtime token",
        "type": "invalid_request_error",
        "code": "",
        "param": ""
    }
}

I also tried in a new browser in case there’s something with cookies/caching.

I haven’t seen this exact error elsewhere in the forum, so appreciate any help!

update - I can get a working token on the deployed app.

Are OpenAIs servers blocked for development from localhost, sometimes? Or some occasional CORS error?

I also thought maybe it’s the dreaded development mode with double renders in react, but that isn’t the case here since the token isn’t on an ‘effect’. It’s a button I push explicitly to call that method.

Oddly I can get the openAI sample App working, so there is something subtle with how my own app runs locally and this code

what i’ve checked

  • error only occurs on localhost code is fine when deployed
  • same API key used for local and staging envs
  • ephemeral key is valid (or at least there is one)
  • API is not ‘bouncing’ - only called once
  • tried with both model names gpt-4o-realtime-preview-2024-12-17 and
    `gpt-4o-realtime-preview’
  • using Next/vercel so i tried with their NestResponse.json and plain new Response
  • tried new browser sessions (incognito etc)
  • checked browser voice permissions
    const baseUrl = 'https://api.openai.com/v1/realtime'
    const sdpResponse = await fetch(`${baseUrl}?model=${modelName}`, {
      method: 'POST',
      body: offer.sdp,
      headers: {
        Authorization: `Bearer ${EPHEMERAL_KEY}`,
        'Content-Type': 'application/sdp'
      }
    })

FWIW the server code is

export async function GET(req: Request) {
  const modelName = 'gpt-4o-realtime-preview-2024-12-17'
  try {
    const response = await fetch(
      'https://api.openai.com/v1/realtime/sessions',
      {
        method: 'POST',
        headers: {
          Authorization: `Bearer ${process.env.OPENAI_API_KEY}`,
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({
          model: modelName,
          // modalities: ['audio', 'text'],
          voice: 'verse'
        })
      }
    )

    const data = await response.json()
    // console.log('tokenResponse', data)
    // return new Response(JSON.stringify(data), { status: 200 })
    return NextResponse.json(data)
  } catch (error) {
    console.error('Token generation error:', error)
    return new Response(JSON.stringify({ error: 'Failed to generate token' }), {
      status: 500
    })
  }
}

very frustrating and weird. Like I said, the app was working locally and just stopped.

update again - seems this was related to using an old API key.
the bizarre thing is it worked a few times, worked in prod, then stopped working locally. at least it seems that was what happened? unless its just general flakiness but seems reproducible.