Gemini API key in Google AI Studio

Published 2026-10-06 · Updated 2026-10-23

Google AI Studio hands out keys in the standard Google API format: AIza plus 35 characters of letters, digits, dashes and underscores. If you’ve seen a Google Maps or YouTube key, it’s the same shape, what differs is which APIs the key can call.

Where the key lives

Keys are created at aistudio.google.com under “Get API key”. Each key is tied to a Google Cloud project behind the scenes, and quota is per-project, burn through one key’s free tier and a sibling key on the same project won’t save you.

Two ways to call it

Native Gemini API, the key goes in the URL, not a header:

curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-3-flash:generateContent?key=$GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{"parts": [{"text": "Say hello"}]}]
  }'

OpenAI-compatible endpoint, Google exposes an OpenAI-shaped surface, which is the easier drop-in:

from openai import OpenAI
import os

client = OpenAI(
    api_key=os.environ["GEMINI_API_KEY"],
    base_url="https://generativelanguage.googleapis.com/v1beta/openai/",
)

resp = client.chat.completions.create(
    model="gemini-3-flash",
    messages=[{"role": "user", "content": "Say hello"}],
)
print(resp.choices[0].message.content)

Note the trailing slash on that base URL, the OpenAI-compat path needs it.

Checking a key

curl "https://generativelanguage.googleapis.com/v1beta/models?key=$GEMINI_API_KEY"

A JSON list of models means the key works. API_KEY_INVALID is self-explanatory; PERMISSION_DENIED usually means the Generative Language API isn’t enabled on the key’s project.

Things that catch people

Keep the key in an environment variable (GEMINI_API_KEY is the conventional name) and the rest of the setup is ordinary.