To use the Gemini API, create an API key in Google AI Studio, store it in the GEMINI_API_KEY environment variable, and send a request to the Interactions API with a model such as gemini-3.8-flash and your prompt as input. With the official SDK (pip install -U google-genai or npm install @google/genai) that is a five-line program.
This follows Google's documentation as of October 2026. Google's current examples use the Interactions API rather than the older generateContent call, so many older tutorials look different; the section on that is further down.
Step 1: Create an API key in Google AI Studio
Sign in at Google AI Studio and open the API keys page (aistudio.google.com/apikey). Google's quickstart says new users get a project and key created automatically; otherwise select Create API key. Google's key documentation says all new keys created in AI Studio are "auth keys", bound to a Google Cloud service account, and that from September 2026 the Gemini API rejects requests made with the older "Standard" keys. If an old key suddenly stops working, that is the first thing to check.
Keep the key secret. Google's guidance is to "Never check API keys into source control systems like Git" and not to put keys in web or mobile app code. If a key leaks, see what to do when an API key leaks.
Step 2: Set GEMINI_API_KEY
macOS and Linux, current terminal:
export GEMINI_API_KEY="YOUR_API_KEY"
Windows PowerShell, current window:
$env:GEMINI_API_KEY = "YOUR_API_KEY"
To keep it across sessions, Google suggests adding the export line to ~/.bashrc or ~/.zshrc, or creating a user variable in Windows' Environment Variables dialog and opening a new terminal. How to set environment variables has the details.
The SDK also reads GOOGLE_API_KEY, and if both are set, GOOGLE_API_KEY takes precedence. A leftover GOOGLE_API_KEY from another project is a common reason the SDK uses the wrong key.
Step 3: First request with curl
curl -X POST "https://generativelanguage.googleapis.com/v1beta/interactions" \
-H "x-goog-api-key: $GEMINI_API_KEY" \
-H 'Content-Type: application/json' \
-d '{"model": "gemini-3.8-flash", "input": "Explain how AI works in a few words"}'
The key goes in the x-goog-api-key header. The body names the model and passes the prompt as input.
Python
pip install -U google-genai
from google import genai
client = genai.Client() # reads GEMINI_API_KEY (or GOOGLE_API_KEY)
interaction = client.interactions.create(
model="gemini-3.8-flash",
input="Explain how AI works in a few words",
)
print(interaction.output_text)
As of October 2026, pip install google-genai installs version 2.33.0. The package is google-genai, imported as from google import genai.
JavaScript / Node.js
npm install @google/genai
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({}); // reads GEMINI_API_KEY
const interaction = await ai.interactions.create({
model: "gemini-3.8-flash",
input: "Explain how AI works in a few words",
});
console.log(interaction.output_text);
Save it as quickstart.mjs (top-level await needs an ES module) and run node quickstart.mjs.
Add a system instruction
Standing instructions go in system_instruction, separate from the user's input. This is Google's own example:
interaction = client.interactions.create(
model="gemini-3.8-flash",
system_instruction="You are a cat. Your name is Neko.",
input="Hello there",
)
print(interaction.output_text)
Generation settings such as max_output_tokens go in a generation_config object. Google's text-generation guide says Gemini 4 and Gemini 3.6+ models ignore custom temperature, top_p and top_k values and advises removing them. System prompt vs user prompt explains how to split instructions between the two.
Multi-turn conversations
Unlike APIs where you resend the whole history, the Interactions API stores conversation state on Google's side by default (store=true). You continue a conversation by passing the previous interaction's ID:
interaction1 = client.interactions.create(
model="gemini-3.8-flash",
input="I have 2 dogs in my house.",
)
interaction2 = client.interactions.create(
model="gemini-3.8-flash",
input="How many paws are in my house?",
previous_interaction_id=interaction1.id,
)
print(interaction2.output_text)
If you don't want Google to keep the conversation, set store=false and manage the history yourself. Google's migration guide states that "The Interactions API stores interactions by default"; it doesn't give a retention period there, so check the terms before sending sensitive data.
Interactions API vs generateContent
Many older tutorials use the generateContent call. The two look like this side by side, from Google's migration guide:
| generateContent (older) | Interactions API (recommended) | |
|---|---|---|
| Python call | client.models.generate_content(model=..., contents="...") | client.interactions.create(model=..., input="...") |
| Reading the text | response.text | interaction.output_text |
| Conversation state | You send the history | Stored on the server by default; pass previous_interaction_id |
| Status | "remains fully supported" | Recommended "for all new development" |
Which model to use
Google's models page (updated 22 October 2026) lists gemini-4-argon as the flagship reasoning model and gemini-3.8-flash as its most capable Flash model, with lighter options such as gemini-3.5-flash-lite and gemini-3.1-flash-lite. The pricing page shows gemini-3.8-flash on the free tier, with paid usage at $0.75 per million input tokens and $3.75 per million output tokens through 31 December 2026; gemini-4-argon is not on the free tier. Google says "Most production apps should use a specific stable model" rather than a -latest alias, which moves to the newest release; breaking changes to an alias come with two weeks' notice.
Errors you will see first
Real responses captured on 26 October 2026. A wrong or revoked key returns HTTP 400, not 401:
"code": 400,
"message": "API key not valid. Please pass a valid API key.",
"status": "INVALID_ARGUMENT",
...
"reason": "API_KEY_INVALID"
No key header at all returns 403:
"code": 403,
"message": "Method doesn't allow unregistered callers (callers without established identity). Please use API Key or other form of API consumer identity to call this API.",
"status": "PERMISSION_DENIED"
In Python, creating the client with neither variable set fails immediately:
ValueError: No API key was provided. Please pass a valid API key. Learn how to create an API key at https://ai.google.dev/gemini-api/docs/api-key.
That usually means the variable was set in another terminal or after the IDE started. A 429 means you hit a rate limit; rate limit error 429 covers backoff and retries.
Frequently asked questions
- Is the Gemini API free?
- Partly. As of October 2026, Google's pricing page lists a free tier for some models, including gemini-3.8-flash. Free-tier usage may be used to improve Google's products; paid-tier usage is not. Some models, such as gemini-4-argon, are not available on the free tier at all.
- Should I use generateContent or the Interactions API?
- Google recommends the Interactions API for all new development and says generateContent remains fully supported. Most current docs and quickstart examples use client.interactions.create. Older tutorials that call client.models.generate_content still work, but the request and response fields differ, so don't mix examples from the two.
- What is the difference between GEMINI_API_KEY and GOOGLE_API_KEY?
- The Google GenAI SDK accepts either variable name. If both are set, GOOGLE_API_KEY takes precedence, which can make the SDK silently use a different key than the one you just changed. Set only one of them.
- Can I call the Gemini API from a website's JavaScript?
- Not with your API key in the page. Google's key documentation says not to hardcode API keys in web or mobile apps, because keys in client-side code can be extracted. Call the API from your own backend and have the browser talk to that.