Introduction
The Google Gemini API is Google's latest generative AI, powered by models like Gemini 1.5 Flash or Pro. In 2026, it's essential for developers adding smart features to apps: text generation, natural conversations, image analysis, or even video. Unlike closed APIs, Gemini offers a simple official SDK, free up to quotas, and scales effortlessly.
This beginner tutorial guides you step-by-step to build a Node.js app that interacts with Gemini. You'll learn to get a free API key, install the SDK, generate simple text, manage conversations, and dive into multimodal (text + image). Every example is complete and functional, ready to copy-paste. By the end, you'll master the basics to boost your web projects, bots, or internal tools. Estimated time: 15 minutes to your first successful call.
Prerequisites
- Node.js 20+ installed
- Free Google account
- Basic JavaScript knowledge (promises/async)
- Code editor (VS Code recommended)
- Terminal to run commands
Get your Gemini API key
echo "Créez un compte sur https://aistudio.google.com/"
echo "Cliquez sur 'Get API key' > New API key"
echo "Copiez la clé et créez un fichier .env :"
cat > .env << EOF
GOOGLE_GEMINI_API_KEY=your_api_key_here
EOF
npm init -y
npm install @google/generative-ai dotenvThis command sets up a Node.js project, installs the official @google/generative-ai SDK and dotenv to securely manage your API key. Replace your_api_key_here with your real key from AI Studio. Never commit .env to Git (add it to .gitignore).
Understanding the API key and security
The API key is free for 15 RPM (requests per minute) on Gemini 1.5 Flash. Store it in .env to avoid leaks. Use environment variables in production (Vercel, Render). Think of it like a house key—keep it safe!
First call: Simple text generation
require('dotenv').config();
const { GoogleGenerativeAI } = require('@google/generative-ai');
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: 'gemini-1.5-flash' });
async function generateText() {
const prompt = 'Explique-moi l\'IA générative en 3 phrases simples.';
const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
console.log(text);
}
generateText().catch(console.error);This script loads the key via dotenv, initializes the 'gemini-1.5-flash' model (fast and free), sends a prompt, and prints the response. Run with node generate-text.js. Pitfall: Forget await and you'll hit async errors—always wrap in an async function.
Anatomy of a Gemini response
- text() : Extracts the main text
- response.full() : Everything (citations, safety)
- usageMetadata : Tokens used (for quotas)
node generate-text.js: You'll see a clear explanation of generative AI.Multi-turn conversation (chat)
require('dotenv').config();
const { GoogleGenerativeAI } = require('@google/generative-ai');
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_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: 200 }
});
const result = await chat.sendMessage('Quelle est la capitale de la France ?');
console.log(result.response.text());
}
chat().catch(console.error);Use startChat() for contextual history, perfect for Telegram/Discord bots. generationConfig limits tokens to control costs. Add safetySettings to filter sensitive content. Run it and see the conversational memory in action.
Multimodal generation (text + image)
require('dotenv').config();
const { GoogleGenerativeAI } = require('@google/generative-ai');
const fs = require('fs');
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: 'gemini-1.5-flash' });
async function analyzeImage() {
const imagePart = {
inlineData: {
data: fs.readFileSync('image.jpg').toString('base64'),
mimeType: 'image/jpeg'
}
};
const result = await model.generateContent(['Décris cette image en détail.'], imagePart);
const response = await result.response;
console.log(response.text());
}
analyzeImage().catch(console.error);Gemini shines in multimodal tasks: load a local image as base64 and analyze it. Replace 'image.jpg' with a real file (download a test one). Limit: 20MB max. Great for AI vision apps like CV analysis or product photos.
Streaming for real-time responses
For a smooth UX (like ChatGPT), use streamGenerateContent(). Add it to your scripts to see tokens arrive progressively.
Streaming generation
require('dotenv').config();
const { GoogleGenerativeAI } = require('@google/generative-ai');
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: 'gemini-1.5-flash' });
async function streamContent() {
const prompt = 'Rédige un poème sur Node.js en streaming.';
const stream = await model.streamGenerateContent(prompt);
let fullText = '';
for await (const chunk of stream.stream) {
const text = chunk.value.text();
process.stdout.write(text);
fullText += text;
}
console.log('\n\nTexte complet :', fullText);
}
streamContent().catch(console.error);for await loops over chunks for live display. Ideal for web interfaces with Server-Sent Events. Saves memory compared to full responses.
Express-integrated script (API server)
require('dotenv').config();
const express = require('express');
const { GoogleGenerativeAI } = require('@google/generative-ai');
const app = express();
app.use(express.json());
const genAI = new GoogleGenerativeAI(process.env.GOOGLE_GEMINI_API_KEY);
const model = genAI.getGenerativeModel({ model: 'gemini-1.5-flash' });
app.post('/generate', async (req, res) => {
try {
const { prompt } = req.body;
const result = await model.generateContent(prompt);
res.json({ response: result.response.text() });
} catch (error) {
res.status(500).json({ error: error.message });
}
});
app.listen(3000, () => console.log('Serveur sur http://localhost:3000'));
// npm install express requisTurn it into a REST API with Express. POST to /generate with {prompt: '...'}. Handle errors for production. Test with curl or Postman: curl -X POST -d '{"prompt":"Hello"}' http://localhost:3000/generate. (Requires npm install express)
Best practices
- Rate limiting : Add Redis caching to avoid quotas.
- Security : Enable
safetySettings(BLOCK_MEDIUM_AND_ABOVE) to filter content. - Tokens : Monitor
usageMetadataand use short prompts. - Models : Flash for speed, Pro for complexity.
- Errors : Always use try/catch on async calls.
Common errors to avoid
- Invalid key : Check your API key on AI Studio (no exceeded quotas).
- No await : Promises reject silently without async.
- Image file too large : <20MB, compress before base64.
- Forgot dotenv :
undefinedAPI_KEY → 401 Unauthorized.
Next steps
Master embeddings for semantic search, integrate with Next.js or React. Check out our advanced AI courses at Learni. Official docs: ai.google.dev. GitHub examples: google/generative-ai-node.