Getting Started

Getting Started with the Gemini API

Learn to integrate Google's Gemini models into your applications using the Gemini API and Google AI SDK.

What is the Gemini API?

Google's Gemini API provides access to the Gemini family of multimodal AI models. Gemini is built natively multimodal — it can understand and reason across text, images, audio, video, and code in a single model.

Why Gemini?

As covered in our [Claude vs GPT vs Gemini comparison](/blog/claude-vs-gpt-vs-gemini), Gemini stands out for:

  • Longest context window — Up to 1 million tokens (Gemini 1.5 Pro), enabling entire codebases or long documents in a single call
  • Native multimodality — Text, image, audio, video, and code understood natively
  • Google ecosystem — Seamless integration with Vertex AI, BigQuery, and Google Cloud
  • Grounding — Connect to Google Search for up-to-date factual information
  • Competitive pricing — Gemini 1.5 Flash offers strong performance at low cost

Available Models

ModelContext WindowBest For
gemini-1.5-pro1,000,000 tokensLong docs, codebases, complex reasoning
gemini-1.5-flash1,000,000 tokensFast, cost-efficient tasks
gemini-2.0-flash1,000,000 tokensLatest, multimodal, agentic tasks
gemini-1.0-pro32,000 tokensStandard text generation

Getting an API Key

  1. Go to [Google AI Studio](https://aistudio.google.com)
  2. Sign in with your Google account
  3. Click Get API KeyCreate API key
  4. Store the key in your environment variables — never hard-code it

Two Access Paths

Google AI Studio / Google AI SDK — For direct API access, prototyping, and applications outside Google Cloud:

text
npm install @google/generative-ai

Vertex AI — For production deployments on Google Cloud with enterprise features, IAM, and VPC controls:

text
npm install @google-cloud/vertexai

Example

typescript
// Install: npm install @google/generative-ai

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

const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY!);

async function main() {
  const model = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });

  const result = await model.generateContent(
    "Explain the difference between REST and GraphQL in 3 sentences."
  );

  console.log(result.response.text());
}

main();
Try it yourself — TYPESCRIPT