Gemini API Quickstart: From API Key to Your First Agent Call
A working Gemini integration in under five minutes
Get an API Key
Open Google AI Studio and create a key at aistudio.google.com/apikey. Store it in an environment variable rather than in source code:
export GEMINI_API_KEY="your-key-here"
On Windows PowerShell:
$env:GEMINI_API_KEY="your-key-here"
Install the SDK
Python:
pip install -U google-genai
Node.js or TypeScript:
npm install @google/genai
The current SDKs replace the older google-generativeai and @google/generative-ai packages. Use the new ones for new projects.
Your First Call
Python:
from google import genai
client = genai.Client()
response = client.models.generate_content(
model="gemini-flash-latest",
contents="Explain the Model Context Protocol in two sentences.",
)
print(response.text)
TypeScript:
import { GoogleGenAI } from "@google/genai";
const ai = new GoogleGenAI({});
const response = await ai.models.generateContent({
model: "gemini-flash-latest",
contents: "Explain the Model Context Protocol in two sentences.",
});
console.log(response.text);
The client reads GEMINI_API_KEY automatically. If you need to pass it explicitly, use the apiKey option.
Streaming Responses
Streaming keeps interfaces responsive when outputs are long:
for chunk in client.models.generate_content_stream(
model="gemini-flash-latest",
contents="Write a migration plan for moving a monolith to services.",
):
print(chunk.text, end="")
Function Calling
Function calling is how Gemini becomes an agent. Declare tools, let the model choose one, execute it in your code, and return the result:
from google.genai import types
def get_weather(city: str) -> str:
return f"18C and clear in {city}"
tools = [get_weather]
response = client.models.generate_content(
model="gemini-flash-latest",
contents="What is the weather in Bengaluru?",
config=types.GenerateContentConfig(tools=tools),
)
print(response.text)
Keep tool functions small, validate every argument, and never pass raw model output into a shell or SQL query.
Choosing a Model
Use the Flash models for high-volume extraction, classification, and chat. Use the Pro models for complex reasoning, long documents, and planning. Model aliases such as gemini-flash-latest move forward automatically; pin an explicit version in production if you need reproducible behavior.
Cost and Limits
The Gemini API has a free tier with rate limits and a paid tier billed per token. Free-tier quotas change frequently, so check the current pricing page before sizing a workload. For production, add retries with exponential backoff, cap output tokens, and cache repeated prompts where possible.
Production Checklist
- Keep the key server-side; never ship it in a browser bundle
- Set per-request timeouts and retry on 429 and 5xx responses
- Log token usage and latency per route
- Validate structured output against a schema before using it
- Use a pinned model version for reproducible evaluations
Next Steps
Once the first call works, wrap it in a tool-using loop and connect real data. The MCP guide on Tecrese shows how to expose those tools to any compatible agent, and the AI Launchpad has copy-paste setup commands for the whole stack.
Verified Practical Workflow
This guide has been tested and verified against the latest framework and model APIs.