API & Integration
Generate API keys, integrate Gemini into your applications with Python, JavaScript, and curl, and understand rate limits and pricing.
Generating API Keys
Your API key is the gateway to using Gemini in your applications:
- Open Google AI Studio
- Click "Get API key" in the left sidebar
- Click "Create API key in new project" (or select an existing project)
- Copy the key and store it securely (you won't be able to see it again)
GEMINI_API_KEY), .env files (added to .gitignore), or a secrets manager. If your key is ever exposed, revoke it immediately and create a new one.Gemini API Quickstart
The Gemini API uses a REST interface. Here are quickstart examples in three languages:
Python
pip install google-generativeai
import google.generativeai as genai
import os
# Configure with your API key
genai.configure(api_key=os.environ["GEMINI_API_KEY"])
# Create a model instance
model = genai.GenerativeModel("gemini-pro")
# Generate content
response = model.generate_content("Explain quantum computing in simple terms.")
print(response.text)
# Multi-turn chat
chat = model.start_chat()
response = chat.send_message("What is machine learning?")
print(response.text)
response = chat.send_message("How does it differ from deep learning?")
print(response.text)
JavaScript (Node.js)
npm install @google/generative-ai
const { GoogleGenerativeAI } = require("@google/generative-ai");
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
async function run() {
const model = genAI.getGenerativeModel({ model: "gemini-pro" });
// Simple generation
const result = await model.generateContent(
"Write a haiku about programming."
);
console.log(result.response.text());
// Multi-turn chat
const chat = model.startChat();
const chatResult = await chat.sendMessage("What is TypeScript?");
console.log(chatResult.response.text());
}
run();
curl (REST API)
# Simple text generation
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [{
"parts": [{"text": "List 5 uses of AI in healthcare."}]
}]
}'
# With generation config
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=$GEMINI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"contents": [{"parts": [{"text": "Write a creative story opener."}]}],
"generationConfig": {
"temperature": 0.9,
"topK": 40,
"topP": 0.95,
"maxOutputTokens": 1024
}
}'
SDK Setup
Official SDKs are available for multiple languages:
| Language | Package | Install Command |
|---|---|---|
| Python | google-generativeai | pip install google-generativeai |
| JavaScript | @google/generative-ai | npm install @google/generative-ai |
| Kotlin/Android | generativeai | Add via Gradle dependency |
| Swift/iOS | GoogleGenerativeAI | Add via Swift Package Manager |
| Go | google.golang.org/genai | go get google.golang.org/genai |
Rate Limits
The free tier provides generous limits for development and prototyping:
| Model | Requests per Minute | Tokens per Minute | Requests per Day |
|---|---|---|---|
| Gemini Pro | 60 | 32,000 | 1,500 |
| Gemini Flash | 60 | 1,000,000 | 1,500 |
For higher limits, upgrade to a paid plan or use the API through Google Cloud's Vertex AI.
Pricing
Google AI Studio offers a generous free tier:
- Free tier: Rate-limited but sufficient for development and small projects
- Pay-as-you-go: Charged per token (input and output priced separately)
- Gemini Flash: Significantly cheaper than Pro for high-volume use
- Tuned models: Priced similarly to the base model they're built on
Using with Google Cloud
For production workloads, consider using the Gemini API through Vertex AI on Google Cloud:
- Higher rate limits and SLAs for production use
- VPC-SC and data residency controls
- Integration with other Google Cloud services
- Enterprise support and compliance certifications
- Access to additional models and features
💡 Try It: Build a Simple API Integration
Choose Python or JavaScript and create a simple script that uses the Gemini API. Start with basic text generation, then try a multi-turn chat. Experiment with different generation config values (temperature, topK).
Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX