Working with AI APIs

Google Gemini API

Learn how to use Google's Gemini API for text, vision, and multimodal AI applications.

Google Gemini API

Google's Gemini models are natively multimodal — built from the ground up to understand text, images, audio, and video. This topic covers how to build with the Gemini API.

Setting Up

1. Get API Access

Go to ai.google.dev and create an API key through Google AI Studio.

bash
export GOOGLE_API_KEY="your-key-here"

2. Install the SDK

bash
# Python
pip install google-generativeai

# Node.js
npm install @google/generative-ai

Your First Gemini API Call

Python

python
import google.generativeai as genai

genai.configure(api_key=os.environ["GOOGLE_API_KEY"])

model = genai.GenerativeModel("gemini-1.5-pro")

response = model.generate_content("Explain how WiFi works in simple terms.")

print(response.text)

Node.js

javascript
import { GoogleGenerativeAI } from "@google/generative-ai";

const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY);
const model = genAI.getGenerativeModel({ model: "gemini-1.5-pro" });

const result = await model.generateContent("Explain how WiFi works.");
console.log(result.response.text());

Available Models

ModelStrengthsContext Window
Gemini 1.5 ProBest quality, long context2M tokens
Gemini 1.5 FlashFast, cost-effective1M tokens
Gemini 2.0 FlashLatest, improved capabilities1M tokens

The 2M token context window is the largest among major providers.

Chat Conversations

python
model = genai.GenerativeModel("gemini-1.5-pro")
chat = model.start_chat(history=[])

response = chat.send_message("What is machine learning?")
print(response.text)

response = chat.send_message("Give me a Python example.")
print(response.text)

# The chat object maintains conversation history automatically

System Instructions

python
model = genai.GenerativeModel(
    "gemini-1.5-pro",
    system_instruction="You are a helpful coding tutor. Always provide "
                       "code examples in Python. Explain concepts simply."
)

response = model.generate_content("What is a decorator?")
print(response.text)

Configuration Parameters

python
generation_config = genai.GenerationConfig(
    temperature=0.7,
    top_p=0.95,
    top_k=40,
    max_output_tokens=2048,
    stop_sequences=["END"],
)

model = genai.GenerativeModel(
    "gemini-1.5-pro",
    generation_config=generation_config
)

Vision — Analyzing Images

python
import PIL.Image

model = genai.GenerativeModel("gemini-1.5-pro")

image = PIL.Image.open("diagram.png")

response = model.generate_content([
    "Explain this architecture diagram in detail.",
    image
])

print(response.text)

Multiple Images

python
image1 = PIL.Image.open("before.png")
image2 = PIL.Image.open("after.png")

response = model.generate_content([
    "Compare these two UI designs. What changed? Which is better and why?",
    image1,
    image2
])

Processing Long Documents

With up to 2M tokens, Gemini can process extremely long documents:

python
model = genai.GenerativeModel("gemini-1.5-pro")

# Upload a large file
sample_file = genai.upload_file("large_document.pdf")

response = model.generate_content([
    "Summarize the key findings in this research paper.",
    sample_file
])

print(response.text)

Audio Understanding

Gemini can process audio files:

python
audio_file = genai.upload_file("meeting_recording.mp3")

response = model.generate_content([
    "Transcribe this meeting and list the action items.",
    audio_file
])

Video Understanding

python
video_file = genai.upload_file("product_demo.mp4")

response = model.generate_content([
    "Summarize what happens in this video and note key timestamps.",
    video_file
])

Streaming

python
response = model.generate_content(
    "Write a guide to Python async programming",
    stream=True
)

for chunk in response:
    print(chunk.text, end="")

JSON Mode

python
model = genai.GenerativeModel(
    "gemini-1.5-pro",
    generation_config=genai.GenerationConfig(
        response_mime_type="application/json"
    )
)

response = model.generate_content(
    "List the top 3 Python web frameworks with name, description, "
    "and github_stars as a JSON array."
)

import json
data = json.loads(response.text)

Function Calling

python
def get_weather(location: str) -> dict:
    """Get the current weather for a location."""
    # Your actual weather API call
    return {"temperature": 22, "condition": "sunny"}

model = genai.GenerativeModel(
    "gemini-1.5-pro",
    tools=[get_weather]
)

chat = model.start_chat()
response = chat.send_message("What's the weather in Mumbai?")

# Gemini may call the function automatically
print(response.text)

Safety Settings

Gemini has configurable safety filters:

python
from google.generativeai.types import HarmCategory, HarmBlockThreshold

model = genai.GenerativeModel("gemini-1.5-pro")

response = model.generate_content(
    "Explain common cybersecurity attack vectors for educational purposes",
    safety_settings={
        HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT: HarmBlockThreshold.BLOCK_ONLY_HIGH,
    }
)

Error Handling

python
import google.generativeai as genai
from google.api_core import exceptions

try:
    response = model.generate_content("Hello")
except exceptions.ResourceExhausted:
    print("Rate limit hit — wait and retry")
except exceptions.InvalidArgument as e:
    print(f"Invalid request: {e}")
except exceptions.GoogleAPIError as e:
    print(f"API error: {e}")

Token Counting

python
model = genai.GenerativeModel("gemini-1.5-pro")

# Count tokens before sending
token_count = model.count_tokens("Your prompt text here")
print(f"Tokens: {token_count.total_tokens}")

# Count tokens in a conversation
token_count = model.count_tokens(chat.history)
print(f"Conversation tokens: {token_count.total_tokens}")

Gemini's Unique Advantages

  1. Massive context window — 2M tokens (about 1.5M words)
  2. Native multimodal — Text, image, audio, video in one model
  3. Free tier — Generous free usage for development
  4. Google ecosystem — Integrates with Vertex AI, Google Cloud
  5. Grounding with Google Search — Can ground responses in search results

Quick Reference

python
import google.generativeai as genai

# Setup
genai.configure(api_key="your-key")
model = genai.GenerativeModel("gemini-1.5-pro")

# Simple text
response = model.generate_content("Your prompt")
print(response.text)

# With image
image = PIL.Image.open("photo.jpg")
response = model.generate_content(["Describe this", image])

# Chat
chat = model.start_chat()
response = chat.send_message("Hello")

Now that you know how to use all three major APIs, the next topic compares them to help you choose the right one for your projects.