Gemma 4 API & API Key
How to get a key and call Gemma 4 - through Google's Gemini API, a third-party provider, or an endpoint on your own machine. Includes the 2026 change to Google's API keys and embeddings with the new EmbeddingGemma 2.
Checked against Google's documentation and provider listings on October 7, 2026.
- Create a key at aistudio.google.com/apikey
- Set it as
GEMINI_API_KEY - Install the SDK and run the code
# pip install -U google-genai
from google import genai
client = genai.Client() # reads GEMINI_API_KEY from your environment
response = client.models.generate_content(
model="gemma-4-31b-it",
contents="Roses are red...",
)
print(response.text)
Three ways to call Gemma 4
There is no single "Gemma 4 API". The weights are open, so you choose who serves them - and you can switch later without changing models.
Google's Gemini API
The quickest start. One key from Google AI Studio gives you Gemma 4 31B and 26B A4B, with thinking, tools and search grounding.
Third-party providers
OpenRouter, Together and others host Gemma 4 behind OpenAI-compatible endpoints, priced per million tokens.
Your own endpoint
Ollama, LM Studio, llama.cpp and vLLM all expose a local API. No key, no per-token cost, and every model size.
Just want to try it first? Use the playground or see where to chat with it. Not sure which model to call? See model variants and benchmarks.
How to get a Gemma 4 API key
Gemma 4 on Google's API uses a Gemini API key. It takes a couple of minutes and a Google account.
Open Google AI Studio
Go to aistudio.google.com/apikey and sign in with a Google account.
Create the key
Every key belongs to a Google Cloud project. New users get a default project automatically after accepting the terms.
Store it as an environment variable
Name it
GEMINI_API_KEY. Google's SDKs read it automatically, so the key never appears in your code.Make a test call
Run the quickstart. If it prints a reply, you are set.
# Add to ~/.bashrc or ~/.zshrc, then open a new terminal
export GEMINI_API_KEY=<YOUR_API_KEY_HERE>
# Search for "Environment Variables" in Windows # System Properties > Environment Variables > New # Variable name: GEMINI_API_KEY # Variable value: your key # Click OK, then open a new terminal
# Python - pass the key explicitly instead of using the environment from google import genai client = genai.Client(api_key="YOUR_API_KEY") // JavaScript // const ai = new GoogleGenAI({ apiKey: "YOUR_API_KEY" });
# REST - send the key in a header
curl "https://generativelanguage.googleapis.com/v1beta/models/gemma-4-31b-it:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{"contents":[{"parts":[{"text":"Hello"}]}]}'
The SDKs also read GOOGLE_API_KEY; if both are set, that one takes priority. Source: Google's
API key guide.
Old keys stopped working: standard keys vs auth keys
During 2026 Google replaced the older "standard" API keys with "auth" keys for the Gemini API, after leaked keys were being abused to run up bills. If a key that used to work is now rejected, this is the likely reason.
| When | What changed |
|---|---|
| June 19, 2026 | The Gemini API began rejecting requests from unrestricted standard keys. |
| September 2026 | Google's schedule had the Gemini API rejecting all standard keys, including restricted ones. |
| Now | New keys created in Google AI Studio are auth keys by default. |
What an auth key is
A key bound to a Google Cloud service account and limited to the Generative Language API, with faster leak detection.
What to do
Create a new key in AI Studio, update your apps and environment variables, test, then revoke the old key.
Why it matters
A leaked auth key cannot be used against other Google services, which limits the damage if one escapes.
Dates as reported in June 2026 by Cybernews and DoiT, quoting Google's notice. Check Google AI Studio for the status of your own keys.
Which Gemma 4 models the Gemini API serves
Google hosts the two largest sizes. For 12B, E4B or E2B, use a third-party provider or a local endpoint.
| Model ID | Model | Context | Inputs | Best for |
|---|---|---|---|---|
gemma-4-31b-it | Gemma 4 31B · dense | 256K | Text, image | Highest quality |
gemma-4-26b-a4b-it | Gemma 4 26B A4B · Mixture of Experts | 256K | Text, image | Speed and volume |
Model IDs from Google's Gemma on the Gemini API page. Note the lowercase IDs here, unlike the capitalised Hugging Face names - the difference is explained on the models page. Full specifications are in technical specs.
Your first request
The same call in Python, JavaScript and plain HTTP.
# pip install -U google-genai
from google import genai
client = genai.Client() # reads GEMINI_API_KEY from your environment
response = client.models.generate_content(
model="gemma-4-31b-it",
contents="Roses are red...",
)
print(response.text)
// npm install @google/genai
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI(); // reads GEMINI_API_KEY
const response = await ai.models.generateContent({
model: "gemma-4-31b-it",
contents: "Roses are red...",
});
console.log(response.text);
curl "https://generativelanguage.googleapis.com/v1beta/models/gemma-4-31b-it:generateContent" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-X POST \
-d '{
"contents": [{
"parts":[{"text": "Roses are red..."}]
}]
}'
Based on Google's samples. The REST endpoint is
https://generativelanguage.googleapis.com/v1beta/models/MODEL:generateContent. For better prompts see prompting.
What the API supports
Google documents six features for Gemma 4 on the Gemini API: thinking, system instructions, multi-turn chat, image understanding, function calling and Google Search grounding.
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model="gemma-4-26b-a4b-it",
contents="What is the water formula?",
config=types.GenerateContentConfig(
thinking_config=types.ThinkingConfig(thinking_level="high") # or "minimal"
),
)
print(response.text)
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model="gemma-4-26b-a4b-it",
config=types.GenerateContentConfig(
system_instruction="You are a wise Kyoto tea master. Speak calmly and poetically, using nature metaphors. Keep answers under 3 sentences."
),
contents="What is the purpose of the tea ceremony?"
)
print(response.text)
from google import genai
from google.genai import types
get_weather = {
"name": "get_weather",
"description": "Get current weather for a given location.",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City and state, e.g. 'San Francisco, CA'",
},
},
"required": ["location"],
},
}
client = genai.Client()
tools = types.Tool(function_declarations=[get_weather])
config = types.GenerateContentConfig(tools=[tools])
response = client.models.generate_content(
model="gemma-4-26b-a4b-it",
contents="Should I bring an umbrella to Kyoto today?",
config=config,
)
if response.function_calls:
for fc in response.function_calls:
print(f"Function to call: {fc.name}")
print(f"Arguments: {fc.args}")
else:
print(response.text)
from google import genai
from google.genai import types
client = genai.Client()
response = client.models.generate_content(
model="gemma-4-26b-a4b-it",
contents="What are the dates for cherry blossom season in Tokyo this year?",
config=types.GenerateContentConfig(
tools=[{"google_search":{}}]
),
)
print(response.text)
for chunk in response.candidates[0].grounding_metadata.grounding_chunks:
print(f"Source: {chunk.web.title} - {chunk.web.uri}")
Samples from Google's documentation. Thinking takes "high" or "minimal". What the
models can do beyond the API is covered in capabilities and features; known gaps are in limitations.
Third-party APIs and prices
These hosts serve Gemma 4 behind OpenAI-compatible endpoints, each with its own key. Prices are per million tokens.
| Model | Where | Input | Output |
|---|---|---|---|
| Gemma 4 26B A4B | Lowest on OpenRouter | $0.042 | $0.22 |
| Gemma 4 26B A4B | Google Vertex AI, via OpenRouter | $0.15 | $0.60 |
| Gemma 4 31B | Lowest on OpenRouter (13 providers) | $0.09 | $0.34 |
| Gemma 4 31B | Together AI, serverless | $0.39 | $0.97 |
Checked October 7, 2026 on OpenRouter and Together AI. Prices move often - see the pricing page for a fuller comparison and a cost calculator.
Calling OpenRouter
curl https://openrouter.ai/api/v1/chat/completions \
-H "Authorization: Bearer $OPENROUTER_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "google/gemma-4-31b-it",
"messages": [{"role": "user", "content": "Hello!"}]
}'
Same code, different host
Most OpenAI client code works by changing the base URL, the key and the model name.
Each provider has its own key
A Gemini API key does not work on OpenRouter or Together, and theirs do not work on Google's API.
Run your own Gemma 4 API
Every common local runtime exposes an HTTP API on your machine. No key is needed, nothing leaves your computer, and all five model sizes are available.
| Runtime | Default address | Style |
|---|---|---|
| Ollama | localhost:11434/v1 | OpenAI-compatible, plus its own API |
| LM Studio | localhost:1234/v1 | OpenAI-compatible |
| llama.cpp server | localhost:8080/v1 | OpenAI-compatible |
| vLLM | localhost:8000/v1 | OpenAI-compatible |
# pip install openai · ollama pull gemma4:12b-it-qat
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:11434/v1", # Ollama
api_key="ollama", # any string works locally
)
response = client.chat.completions.create(
model="gemma4:12b-it-qat",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)
Get the models from the download hub; set up a runtime with the install guide or runtime selector; check memory in the VRAM guide. Serving a team? See self-hosting. A local endpoint can also back Claude Code.
Embeddings API with EmbeddingGemma 2
For search and RAG you need embeddings, not chat. Google's new open model, EmbeddingGemma 2, turns text, code, images, video and audio into vectors - and because the weights are open, you can serve it yourself with no key. Google's EmbeddingGemma 2 developer guide has the full walkthrough.
# Download once ollama pull embeddinggemma-2 # Ask the local server for an embedding curl http://localhost:11434/api/embed -d '{ "model": "embeddinggemma-2", "input": "What causes the northern lights?" }'
# pip install -U "sentence-transformers[image,audio,video]" transformers
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("google/embeddinggemma-2")
query_emb = model.encode("ocean waves at sunset", prompt_name="SearchQuery")
image_emb = model.encode({"image": "sunset_beach.jpg"})
audio_emb = model.encode({"audio": "ocean_waves.wav"})
print(model.similarity(query_emb, image_emb))
print(model.similarity(query_emb, audio_emb))
# Shorter vectors: 256 dimensions instead of 768
query_emb = model.encode(
query,
prompt_name="SearchQuery",
truncate_dim=256,
normalize_embeddings=True,
)
Sizes
740M parameters in full; 270M for text only, 440M with images, 570M with audio. On Ollama that is 378 MB to 1.3 GB.
Vectors
768 dimensions, reducible to 512, 256 or 128. A million 768-dimension vectors take about 1.5 GB; at 128, about 250 MB.
Context
8,192 tokens shared across all inputs. Use the task prompt names, such as SearchQuery, for text.
Hosted option
Google says availability in its Gemini Enterprise Agent Platform Model Garden is coming soon. Until then, run it yourself.
Python samples and figures from Google's developer guide; Ollama tag from Ollama's library. Everything else - specs, prompts, benchmarks, downloads - is in this site's EmbeddingGemma 2 guide.
Keep your API key safe
A leaked key can be used by anyone who finds it. Google's own guidance comes down to five rules.
Never commit it
Do not check API keys into Git or any other source control.
Keep it out of the browser
Never hardcode a key in client-side code. Call the API from your own server instead.
Use environment variables
Or a secret manager in production, so the key is never written into your code.
Set billing alerts
So unusual usage is noticed quickly if you are on a paid tier.
If it leaks, rotate
Create a new key, update your apps, disable the old key, then review the usage logs.
What it costs and how much you can use
Google's Gemini API
Google's pricing page lists no per-token price for Gemma models. Usage is governed by rate limits, which Google shows per model in AI Studio.
Usage tiers
Limits depend on your project's tier - Free, Tier 1, Tier 2 or Tier 3 - and rise automatically as usage and spending grow.
Everywhere else
Third-party providers charge per million tokens. A local endpoint costs nothing per token - only your hardware.
Common API errors
| Error | Usual cause | What to try |
|---|---|---|
| 401 / 403 | Missing, wrong or retired key | Check GEMINI_API_KEY is set in this terminal. If the key predates mid-2026, create a new one. |
| 404 | Wrong model ID | Use gemma-4-31b-it or gemma-4-26b-a4b-it on Google's API - lowercase, with -it. |
| 429 | Rate limit reached | Slow down and retry with a delay, or check your limits in AI Studio. |
| 400 | Malformed request or unsupported option | Compare your request with the quickstart; remove options one at a time. |
| Connection refused | Local server not running | Start Ollama or LM Studio's server, and check the port in the local API table. |
General guidance for HTTP APIs, not an official error list. More help on the FAQ page.
API questions
Is there a Gemma 4 API key?
Not a separate one. On Google's API you use a Gemini API key from Google AI Studio. Third-party providers issue their own keys, and local endpoints need none.
Is the Gemma 4 API free?
Google's pricing page lists no per-token price for Gemma models, and access is subject to rate limits shown in AI Studio. Third-party providers charge per token, from a few cents per million. Running it locally has no per-token cost. See pricing.
Why did my old key stop working?
Most likely the 2026 switch from standard keys to auth keys. Create a new key in Google AI Studio and replace the old one - details above.
Can I call Gemma 4 12B, E4B or E2B through Google's API?
Does the API support audio?
The two models Google hosts accept text and images. Audio input belongs to E2B, E4B and 12B, which you would run yourself or through another host.
Can I use the OpenAI SDK?
Yes with third-party providers and local endpoints, which speak the OpenAI format. Google's own samples for Gemma 4 use the Google GenAI SDK.
Is there an API for EmbeddingGemma 2?
Not a hosted one from Google yet - a Model Garden listing is described as coming soon. You can serve it locally in minutes with Ollama; see the embeddings section.
API or self-hosting - which should I choose?
Use a hosted API to start quickly or for light, bursty use. Self-host for privacy, offline use, the smaller models, or steady high volume. Compare in self-hosting and the model comparison.
One key, or none at all
Start on Google's API in a minute, or serve the open weights yourself.