Python / FastAPI

How to secure a Gemini API key in Python

Keep the Gemini key out of your Python process and give the app a stand-in it can't abuse. KeyVault Edge stores the key at the edge, and your code sends a kvt_ token with its own request limits, monthly cost cap and allowed endpoints; Google's spend caps apply per project, this adds a limit per app.

Gemini keys found in repos, logs and bundles produced public bills of thousands of dollars in 2026. Google now rejects unrestricted keys and offers project-level spend caps, but those limits cover a whole project, not one app. With KVE the real key never sits in your app, and each token carries its own limits.

The Problem

This is what most Python codebases look like today. The GOOGLE_AI_API_KEY key (AIza...) lives directly in the process environment. If your repo, your CI logs, or your deployment config leaks — so does your key.

Python
# gemini.py — real Google AI key in environment
import os
import google.generativeai as genai

genai.configure(api_key=os.environ["GOOGLE_AI_API_KEY"])  # AIzaSy...
model = genai.GenerativeModel("gemini-3.5-flash")

def generate_content(prompt: str) -> str:
    response = model.generate_content(prompt)
    return response.text

The Fix

Send the request to the KeyVault Edge proxy at https://proxy.keyvaultedge.com with the provider's usual path, your kvt_ token in X-KV-Token and the provider's base URL in X-KV-Target. The proxy refuses requests whose Origin isn't on the token's allowed hosts, then injects your real GOOGLE_AI_API_KEY, so it never sits in your app's environment.

Python
# gemini.py — KVE_TOKEN is host-bound; Google AI key not in process
import os
import httpx

KVE_TOKEN = os.environ["KVE_TOKEN"]

def generate_content(prompt: str) -> str:
    response = httpx.post(
        "https://proxy.keyvaultedge.com/v1beta/models/gemini-3.5-flash:generateContent",
        headers={
            "X-KV-Token": os.environ["KVE_TOKEN"],  # kvt_... token
            "X-KV-Target": "https://generativelanguage.googleapis.com",
            # Must match one of the token's allowed hosts
            "Origin": os.environ["APP_ORIGIN"],
            "Content-Type": "application/json",
        },
        json={
            "contents": [{"parts": [{"text": prompt}]}]
        },
        timeout=30.0,
    )
    response.raise_for_status()
    return response.json()["candidates"][0]["content"]["parts"][0]["text"]

Setup in 4 steps

1

Create a KVE token for Google AI

At keyvaultedge.com, create a token with your AIza... key from Google AI Studio. Set provider to Google AI and add your Python server's hostname as an allowed host.

2

Update your environment

Replace GOOGLE_AI_API_KEY with KVE_TOKEN in your .env or deployment secrets.

Shell
# .env
KVE_TOKEN=kvt_...your_token
APP_ORIGIN=https://yourdomain.com  # one of the token's allowed hosts

pip install httpx
# pip uninstall google-generativeai  # optional
3

Replace the google-generativeai SDK with httpx

Remove genai.configure and the model calls. Use httpx.post to the KVE proxy with the usual Gemini REST path, your token in X-KV-Token and https://generativelanguage.googleapis.com in X-KV-Target. The proxy adds your real key in the x-goog-api-key header and forwards the request.

4

Set limits on both sides

On the KVE token, set requests per minute, hour and day, a monthly cost cap and an endpoint scope such as POST /v1beta/models/gemini-3.5-flash:generateContent. In Google Cloud, also set a project spend cap and a budget alert. KVE limits each token; Google limits the whole project.

What this protects, and what it doesn't

  • Your real GOOGLE_AI_API_KEY stays at the edge, so it can't leak from your repo, logs, bundle or server environment.
  • The kvt_ token is still a credential. A copied token can be replayed by anyone who can send the right headers, including a fake Origin from a server. The protection is that each token has its own rate limits, monthly cost cap and endpoint scope, and you can revoke it in one click without rotating the provider key.
  • The cost cap is estimated from the usage the provider reports, so a request already in flight can overshoot it slightly.

Already leaked a Gemini key? Read what to do in the first hour. Plans and limits are on the pricing page.

Start protecting your keys

Your Google AI key is one leaked .env file away from being exploited. With KeyVault Edge the real key stays at the edge, and the token that replaces it is capped, scoped and revocable in one click.