Comparisons Use Cases Research Papers Alternatives Glossary RAG Benchmarks
API Examples

Gemini API Examples

APIGoogle

Real, working code examples for calling the Google Gemini API, covering the most common patterns you'll need.

Basic Content Generation (Python)

import google.generativeai as genai
genai.configure(api_key="YOUR_API_KEY")

model = genai.GenerativeModel("gemini-2.5-pro")
response = model.generate_content(
    "Explain what a context window is in one paragraph."
)
print(response.text)

Multi-Turn Chat

chat = model.start_chat(history=[])
response = chat.send_message("What is RAG?")
print(response.text)

response2 = chat.send_message("How does that differ from fine-tuning?")
print(response2.text)

JavaScript / Node.js Example

import { GoogleGenerativeAI } from "@google/generative-ai";
const genAI = new GoogleGenerativeAI("YOUR_API_KEY");

const model = genAI.getGenerativeModel({ model: "gemini-2.5-pro" });
const result = await model.generateContent("Summarize this in two sentences: ...");
console.log(result.response.text());

Common Parameters Worth Knowing

Generation config options like temperature and maxOutputTokens can be passed when initializing the model or per-request depending on the SDK version; the start_chat pattern shown above handles conversation history automatically across turns. See Google's own current documentation for the complete, up-to-date parameter list and model names.

Frequently Asked

Is the model name in these examples always current?

Model names and versions change over time; check Google's current documentation for the latest available Gemini model identifiers before deploying.

Does the Gemini API handle conversation history automatically?

The chat-session pattern shown above manages history for you across turns within that session object, simplifying multi-turn conversations.

Where do I get an API key?

Through Google AI Studio or Google Cloud, depending on your setup; never commit a real API key to public source code or version control.

Where can I see similar examples for other providers?

See our OpenAI API Examples and Claude API Examples pages for equivalent code in other major providers.

Chat with us+91 88401 46999