Skip to content
Learni
View all tutorials
Intelligence Artificielle

How to Use the Google Gemini API in 2026

Lire en français

Introduction

The Google Gemini API, launched by Google in 2023 and matured by 2026, provides access to powerful multimodal AI models like Gemini 1.5 Pro and Flash. Unlike closed APIs like GPT, Gemini excels in multimodal reasoning (text, images, audio) and cost/performance efficiency.

This beginner tutorial guides you step-by-step through integrating it into Node.js with TypeScript. You'll learn how to get a free API key via Google AI Studio, generate text, create contextual chats, analyze images, and stream responses. Every example is complete and functional, ready to copy-paste. Ideal for quick prototypes or production apps.

Why Gemini in 2026? Low pricing ($0.00025/1k tokens), low latency, and native integration with Vertex AI for scalability. By the end, you'll master 80% of common use cases.

Prerequisites

  • Node.js 20+ installed
  • Free Google account for AI Studio (API key in 1 min)
  • Basic TypeScript/JavaScript knowledge
  • Editor like VS Code
  • npm or yarn

1. Initialize the project and install dependencies

terminal
mkdir gemini-api-tutorial
cd gemini-api-tutorial
npm init -y
npm install @google/generative-ai typescript ts-node dotenv
npm install -D @types/node

These commands create a Node.js project, install the official @google/generative-ai SDK for interacting with Gemini, plus TypeScript, ts-node to run TS directly, and dotenv to securely manage your API key. Never expose your key in source code.

2. Configure the API key with .env

.env
GEMINI_API_KEY=your_api_key_here

Replace your_api_key_here with your key from AI Studio. The .env file is automatically ignored by Git (via .gitignore from npm init). Never commit API keys to avoid security leaks.

TypeScript Configuration

Create a basic tsconfig.json for strict compilation:

{ "compilerOptions": { "target": "ES2022", "module": "NodeNext", "strict": true } }

Add .env to .gitignore: echo ".env" >> .gitignore.

3. Simple text generation

generate-text.ts
import { GoogleGenerativeAI } from '@google/generative-ai';
import dotenv from 'dotenv';

dotenv.config();

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

async function generateText() {
  const prompt = 'Explique-moi l\'IA en 50 mots.';
  const result = await model.generateContent(prompt);
  const response = await result.response;
  const text = response.text();
  console.log(text);
}

generateText().catch(console.error);

This script initializes the AI client with your key, selects the fast 'gemini-1.5-flash' model, and generates content from a prompt. Run with npx ts-node generate-text.ts. Pitfall: Always check if GEMINI_API_KEY exists to avoid 401 errors.

4. Contextual conversation (chat)

chat.ts
import { GoogleGenerativeAI } from '@google/generative-ai';
import dotenv from 'dotenv';

dotenv.config();

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

async function chat() {
  const chat = model.startChat({
    history: [
      {
        role: 'user',
        parts: [{ text: 'Bonjour, qui es-tu ?' }]
      },
      {
        role: 'model',
        parts: [{ text: 'Je suis Gemini, un modèle IA de Google.' }]
      }
    ],
    generationConfig: { maxOutputTokens: 100 }
  });

  const result = await chat.sendMessage('Quelle est la capitale de la France ?');
  const response = await result.response;
  console.log(response.text());
}

chat().catch(console.error);

Here, we create a chat with history to maintain context, limiting output tokens to 100 for efficiency. Use startChat() for persistent sessions. Caution: History grows; truncate it to avoid quotas.

Going multimodal

Gemini handles text + images. To test, prepare a local image (e.g., image.jpg). In 2026, 1.5+ models excel in vision with up to 1M token context.

5. Image analysis (multimodal)

analyze-image.ts
import { GoogleGenerativeAI } from '@google/generative-ai';
import dotenv from 'dotenv';
import fs from 'fs';

dotenv.config();

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

async function analyzeImage() {
  const imagePart = {
    inlineData: {
      data: Buffer.from(fs.readFileSync('image.jpg')).toString('base64'),
      mimeType: 'image/jpeg'
    }
  };
  const result = await model.generateContent(['Décris cette image en détail.'], { contents: [imagePart] });
  const response = await result.response;
  console.log(response.text());
}

analyzeImage().catch(console.error);

The code reads a local image, encodes it to base64, and sends it with a prompt. Place image.jpg in the root folder. Pitfall: Exact MIME type required; for production, handle >20MB sizes via URLs.

6. Streaming responses

stream.ts
import { GoogleGenerativeAI } from '@google/generative-ai';
import dotenv from 'dotenv';

dotenv.config();

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

async function streamResponse() {
  const prompt = 'Raconte une histoire courte.';
  const result = await model.generateContent(prompt);
  const stream = model.streamGenerativeContent(prompt);
  for await (const chunk of stream.stream) {
    const response = await chunk.response;
    process.stdout.write(response.text());
  }
}

streamResponse().catch(console.error);

Use streamGenerativeContent() for real-time responses, perfect for chat UIs. 'gemini-1.5-pro' for higher quality. Avoid infinite streams; add timeouts and buffers for web apps.

7. Complete utility script with error handling

gemini-utils.ts
import { GoogleGenerativeAI, GenerativeModel } from '@google/generative-ai';
import dotenv from 'dotenv';

dotenv.config();

class GeminiUtils {
  private genAI: GoogleGenerativeAI;
  private model: GenerativeModel;

  constructor() {
    const apiKey = process.env.GEMINI_API_KEY;
    if (!apiKey) throw new Error('GEMINI_API_KEY manquante');
    this.genAI = new GoogleGenerativeAI(apiKey);
    this.model = this.genAI.getGenerativeModel({ model: 'gemini-1.5-flash' });
  }

  async generate(prompt: string): Promise<string> {
    try {
      const result = await this.model.generateContent(prompt);
      return result.response.text() || 'Erreur de génération';
    } catch (error) {
      throw new Error(`Erreur API: ${error}`);
    }
  }
}

const utils = new GeminiUtils();
utils.generate('Bonjour !').then(console.log).catch(console.error);

A reusable class encapsulates the client with key validation and try-catch. Perfect for scalable apps. Extend it for logging or retries. Always type errors for quick debugging.

Best practices

  • Secure the key: Use secrets managers (Vercel, AWS Secrets) in production, never client-side.
  • Limit tokens: generationConfig: { maxOutputTokens: 1024, temperature: 0.7 } for control.
  • Rate limiting: 60 RPM free; implement queues for bursts.
  • Prompt engineering: Be specific, use roles (user/system).
  • Monitoring: Log usage via usageMetadata to optimize costs.

Common errors to avoid

  • Invalid key (401): Check expiration in AI Studio; regenerate if needed.
  • Quota exceeded: Free = 15 RPM/1M tokens/day; upgrade to paid.
  • Prompt too long: 1M input token limit; chunk documents.
  • Missing await: Forget await result.response → undefined.
  • Corrupted base64 image: Verify encoding and MIME.

Next steps

Check out our Learni Group AI training courses to master fine-tuning and RAG.