How to use Google Gemini API — a practical guide

If you're here, you probably want working code in five minutes. That's exactly what this guide does.

Five steps to get started

  1. 1
    Create a key at aistudio.google.com.
  2. 2
    Install `@google/generative-ai`.
  3. 3
    Call generateContent with a prompt (image/video attachments supported).

Shortest snippet that will actually run

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

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

const result = await model.generateContent("הסבר בקצרה מה זה Next.js");
console.log(result.response.text());

Common mistakes (the honest list)

Gemini 1.5 Flash = cheap/fast, Pro = quality/expensive.
For truly huge documents — only Gemini offers 2M context.