Intermediate

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:

  1. Open Google AI Studio
  2. Click "Get API key" in the left sidebar
  3. Click "Create API key in new project" (or select an existing project)
  4. Copy the key and store it securely (you won't be able to see it again)
Security first: Never hardcode your API key in source code. Use environment variables (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:

LanguagePackageInstall Command
Pythongoogle-generativeaipip install google-generativeai
JavaScript@google/generative-ainpm install @google/generative-ai
Kotlin/AndroidgenerativeaiAdd via Gradle dependency
Swift/iOSGoogleGenerativeAIAdd via Swift Package Manager
Gogoogle.golang.org/genaigo get google.golang.org/genai

Rate Limits

The free tier provides generous limits for development and prototyping:

ModelRequests per MinuteTokens per MinuteRequests per Day
Gemini Pro6032,0001,500
Gemini Flash601,000,0001,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
Cost tip: Start with Gemini Flash for development and testing. It's faster and cheaper. Only switch to Pro when you need the additional reasoning capability for your specific use case.

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).

Having a working API integration is the foundation for building AI-powered features into any application!

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.