Claw field notebook
last updated 2026-05-15 edit on GitHub colophon
Google / Gemini API / GAPI.2 · 2 min read

Getting started with the Gemini API

End-to-end setup for the Gemini API — get an API key from AI Studio, install the new google-genai SDK (the older google-generativeai is deprecated), make your first call in Python and TypeScript, and graduate to streaming + system instructions.

The five-step path#

  1. Get an API key — from aistudio.google.com/app/apikey
  2. Install the SDKpip install google-genai or npm install @google/genai
  3. Set the env varexport GEMINI_API_KEY="..."
  4. Make a callclient.models.generate_content(model=..., contents=...)
  5. Add streaming, system instructions, structured output as you need them

Step 1: Get an API key#

  1. Go to aistudio.google.com/app/apikey
  2. Sign in with any Google account
  3. Click Create API key
  4. Either pick an existing Google Cloud project or let AI Studio create a default one

You can hold up to 100 keys across 10 projects. AI Studio displays 50 projects at a time.

Security note: Google has flagged plans to enforce API-key restrictions during 2026 (unrestricted keys will be blocked). Restrict your keys to “Gemini API only” in AI Studio or the Cloud Console now to avoid surprises. Check the API key docs for the current enforcement timeline.

Step 2: Install the SDK#

Python — use google-genai (the new one)#

pip install google-genai

Don’t use pip install google-generativeai. That’s the old package — last released 16 December 2025, EOL’d 30 November 2025, no Live API, no Veo, no Gemini 3. PyPI literally labels it [Deprecated] Google AI Python SDK for the Gemini API.

TypeScript / JavaScript — use @google/genai#

npm install @google/genai

Don’t use @google/generative-ai. Same story — deprecated 30 November 2025.

Other languages#

  • Go: go get google.golang.org/genai
  • Java (Maven): <artifactId>google-genai</artifactId>
  • C# / .NET: dotnet add package Google.GenAI

All five SDKs reached General Availability in May 2025 and are actively maintained.

Step 3: Set the env var#

# bash / zsh
export GEMINI_API_KEY="your-key-here"

# Windows PowerShell
$env:GEMINI_API_KEY = "your-key-here"

For projects, drop a .env file at the project root (and add it to .gitignore):

GEMINI_API_KEY=your-key-here

Step 4: Your first call#

Python — bare minimum#

from google import genai

client = genai.Client()   # picks up GEMINI_API_KEY automatically

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="Explain how AI works in a few words",
)
print(response.text)

TypeScript — bare minimum#

import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({});   // picks up GEMINI_API_KEY automatically

const response = await ai.models.generateContent({
  model: "gemini-2.5-flash",
  contents: "Explain how AI works in a few words",
});
console.log(response.text);

That’s it. Run the script — you should get a few sentences back.

Step 5: Add the things you’ll actually need#

A system instruction#

System instructions go in a separate parameter, not as the first message:

from google import genai
from google.genai import types

client = genai.Client()
response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents="Why is the sky blue?",
    config=types.GenerateContentConfig(
        system_instruction="Respond in exactly one sentence.",
        temperature=0.3,
        max_output_tokens=100,
    ),
)
print(response.text)

Streaming#

for chunk in client.models.generate_content_stream(
    model="gemini-2.5-flash",
    contents="Tell me a story in 300 words."
):
    print(chunk.text, end="", flush=True)

Streaming uses Server-Sent Events under the hood; the SDK gives you an iterator. For real-time voice / video, see the Live API (separate WebSocket-based shape, covered in §GAPI.4 Patterns).

Multi-turn chat#

chat = client.chats.create(model="gemini-2.5-flash")
print(chat.send_message("My name is Sush.").text)
print(chat.send_message("What's my name?").text)

The chats helper threads contents for you. For more control, build the contents list yourself and pass to generate_content each turn.

Switching to Vertex AI later#

When you’re ready to move to Vertex AI (cloud auth, regional residency, third-party models), the only thing that changes is the client constructor:

from google import genai

# was:
client = genai.Client()

# becomes:
client = genai.Client(
    vertexai=True,
    project="your-project-id",
    location="us-central1",
)

The rest of your code (the client.models.generate_content(...) calls) stays identical. See §VAI.4 vs Gemini API direct for when to migrate.

What’s next#

  • §GAPI.3 Models — the full Gemini 3 / 2.5 lineup, pricing, deprecation dates
  • §GAPI.4 Patterns — multi-turn chat shape, streaming, files API, context caching, safety settings
  • §GAPI.5 Tool use — function calling, Google Search grounding, code execution
  • §GCL.1 Gemini CLI — if you want a terminal agent instead of writing code yourself

Sources