Skip to main content

Google AI Studio pass-through

The /gemini route forwards Gemini API paths, such as /v1beta/models/{model}:generateContent, :streamGenerateContent and :countTokens, to Google AI Studio unchanged. Teams keep the Google GenAI SDK while the gateway holds the Google AI Studio key, checks the virtual key and records the spend.

A real-world scenario​

A marketing team prototypes with the Gemini API and the Google GenAI SDK. The company has one Google AI Studio key and does not want it in five prototypes. The platform team sets the key on the gateway, and the prototypes send a virtual key in the key query parameter, exactly where the SDK puts the Google key. Each prototype gets its own budget and the shared key never leaves the gateway.

Before you start​

The gateway needs a Google AI Studio credential. It looks first for a model_list deployment of provider Gemini with use_in_pass_through: true, then for the environment below.

VariablePurpose
GEMINI_API_KEYThe Google AI Studio key the gateway sends upstream.
GEMINI_API_BASEOptional. Changes the upstream address from https://generativelanguage.googleapis.com.

Steps​

  1. Give the gateway the credential. Open /ui/models-and-endpoints, on the Add Model tab pick Google AI Studio under Provider, pick a model, open Advanced Settings, turn on Use in pass through routes and click Add Model. Or set GEMINI_API_KEY and restart the gateway.

  2. Issue a virtual key on /ui/api-keys with + Create New Key, then Create Key.

  3. Call the Gemini API through the gateway. Send the virtual key in the key query parameter or the x-goog-api-key header.

    curl -X POST "http://localhost:4000/gemini/v1beta/models/gemini-3.8-flash:generateContent?key=$SPANWAY_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{"contents": [{"role": "user", "parts": [{"text": "Summarise this ticket in one sentence."}]}]}'
  4. With the Google GenAI SDK, set the base URL to http://localhost:4000/gemini and the API key to the virtual key.

  5. Open /ui/logs and click the newest row to see the request, the key and its cost.

Expected result​

Google's own response, with Google's status code and headers. streamGenerateContent is streamed through as it arrives.

{
"candidates": [{"content": {"role": "model", "parts": [{"text": "The customer wants one of two duplicate charges refunded."}]}, "finishReason": "STOP"}],
"usageMetadata": {"promptTokenCount": 18, "candidatesTokenCount": 12, "totalTokenCount": 30}
}

The spend log row carries the key, team and user and the cost from usageMetadata. The caller's key is replaced by the gateway's key before the call leaves the gateway, so it never reaches Google.

Errors​

StatusMessageWhat it means and how to fix it
401Authentication Error, Invalid proxy server token passedThe virtual key in key or x-goog-api-key is missing, wrong, expired or blocked.
403Key/team denied access to passthrough route /gemini/...The key or team lists the route in denied_passthrough_routes. The message goes on with Matched <pattern> in denied_passthrough_routes, naming the pattern to remove from the key or team.
500Required 'GEMINI_API_KEY'/'GOOGLE_API_KEY' in environment to make pass-through calls to Google AI Studio.The gateway has no Google AI Studio credential. Add a deployment with Use in pass through routes on, or set GEMINI_API_KEY.
Any otherGoogle's own error bodyGoogle refused the request. The message is returned unchanged.

Pass-through endpoints lists every built-in route. The unified generateContent endpoint works with every provider and runs guardrails. The Gemini provider page covers the unified integration, and Vertex AI pass-through covers Gemini on Google Cloud.