Gemini Api Ephemeral token not working

When i try and connect to Gemini Live Client using ephemeral tokens i get the following errors:
genai-live-client.ts:171 WebSocket connection to 'wss://generativelanguage.googleapis.com//ws/google.ai.generativelanguage.v1beta.GenerativeService.BidiGenerateContentConstrained?access_token=auth_tokens/4969d96da97639001864b5be08549d58509411b9a099d4685b984dc45c0f2274' failed:

Then i get a type: "server.error" but with an undefined message

Here is the backend code:

const { GoogleGenAI } = require("@google/genai");
require("dotenv").config();

const client = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

async function getEphemeralToken() {

  const expireTime = new Date(Date.now() + 30 * 60 * 1000).toISOString();
  const token = await client.authTokens.create({
    config: {
      expireTime, // Default 1 minute in the future
      newSessionExpireTime: new Date(Date.now() + (1 * 60 * 1000)),
      httpOptions: {apiVersion: 'v1alpha'},
    },
  });

  return token;
}

module.exports = { getEphemeralToken };

Here is the frontend code:

// token generated from above
const tokenData = await tokenResponse.json();

this.client = new GoogleGenAI({
    ...this.clientOptions,
    apiKey: tokenData.name,
});

this._session = await this.client.live.connect({
    model,
    config,
    callbacks,
});

The token looks like this: auth_tokens/4969d96da97639001864b5be08549d58509411b9a099d4685b984dc45c0f2274

Seems like it’s calling the v1beta endpoint? Need to add the same httpOptions to the connection?

Good catch! I tried this:

const token = await client.authTokens.create({
    config: {
      expireTime, // Default 1 minute in the future
      newSessionExpireTime: new Date(Date.now() + (1 * 60 * 1000)),
      httpOptions: {apiVersion: 'v1beta'},
    },
  });

It threw this error: “Error getting ephemeral token: ApiError: {"error":{"message":"","code":404,"status":"Not Found"}}”

Is ephemeral tokens only supported in v1alpha?

I figured it out by using httpOptions: {"apiVersion": "v1alpha"} in both the frontend:

new GoogleGenAI({
  httpOptions: {"apiVersion": "v1alpha"},
  ...this.clientOptions,
  apiKey: tokenData.name,
});

And the backend:

const expireTime = new Date(Date.now() + 30 * 60 * 1000).toISOString();
const token = await client.authTokens.create({
  config: {
    expireTime, // Default 1 minute in the future
    newSessionExpireTime: new Date(Date.now() + (1 * 60 * 1000)),
    httpOptions: {apiVersion: 'v1alpha'},
  },
});
1 Like

ty, you save me, the docs don’t mention that we need to pass httpOptions: {“apiVersion”: “v1alpha”} on the client side, i struggled with that

1 Like