Hello!
I am encountering an issue when attempting to use Ephemeral Tokens with the Lyria Realtime API, specifically when using ai.live.music.connect().
Here’s a summary of my findings:
- Lyria Realtime works perfectly with my regular, long-lived Gemini API key. This indicates my frontend setup for Lyria, including the model ID (
models/lyria-realtime-exp or similar) and ai.live.music.connect() call, is correct.
- My Ephemeral Token generation and usage flow is also confirmed to be correct. I can successfully generate ephemeral tokens on my backend (Python, using
client.auth_tokens.create() with http_options: {"api_version": "v1alpha"}) and use them on my frontend (TypeScript, passing the token string to GoogleGenAI({ apiKey: ephemeralToken, httpOptions: { apiVersion: 'v1alpha' } })) to connect to other Gemini Live API models (e.g., gemini-2.0-flash-live).
The Problem:
When I try to use an ephemeral token (generated and used via the exact same successful flow as for other Live API models) to connect to Lyria Realtime via ai.live.music.connect(), the connection fails.
Does anyone succeed in using ephemeral token when connecting to lyria realtime?
Hello,
Welcome to the Forum,
Thank you for detailed information, we will try to reproduce your issue. Would it be possible to share your and prompts (if applicable) so that we can repro this?
Hello,
here is the code I use to generate the token
client = genai.Client(
api_key=settings.gemini_api_key.get_secret_value(),
http_options={
"api_version": "v1alpha",
}
)
def create_token()
now = datetime.datetime.now(tz=datetime.timezone.utc)
token = client.auth_tokens.create(
config={
"uses": 5,
'expire_time': now + datetime.timedelta(hours=10),
'new_session_expire_time':now + datetime.timedelta(hours=5),
"live_connect_constraints": {
"model": "models/lyria-realtime-exp",
}
}
)
return { "token": token }
It successfully gives me the token in format { "name": "auth_tokens/..." }
after recieving token on the frontend I’m trying to connect to lyria realtime:
const ai = new GoogleGenAI({
apiKey: token.name,
httpOptions: { apiVersion: 'v1alpha' }
});
const liveMusicHelper = new LiveMusicHelper(genAI, "lyria-realtime-exp");
liveMusicHelper.setWeightedPrompts(buildInitialPrompts());
liveMusicHelper.playPause();
LiveMusicHelper and buildInitialPrompts are copied from https://aistudio.google.com/app/apps/bundled/promptdj-midi
NOTE: FE code works with the regular Gemini token, but doesn’t work with the ephemeral token.
onclose callback is called right after an attempt to establish a connection
Hello,
Could you please confirm if you issue is regarding Gemini API?
Hi, could you share the error message you are getting?
Hi, it says that websocket connection is closed right after the messages are sent into the websocket.

the websocket endpoint url wss://generativelanguage.googleapis.com/ws/google.ai.generativelanguage.v1alpha.GenerativeService.BidiGenerateMusic?key=auth_tokens/[my_token]
1 Like
Hello,
Apologies for the delayed response. I looked into your issue, unfortunately ephemeral tokens currently work only with the Live API. This information is also provided in Gemini API ephemeral token documentation.