CLI Quickstart

Get up and running with Gemini from your terminal in under 5 minutes.

1

Get Your API Key

First, you'll need a Gemini API key from Google AI Studio.

Get API Key from AI Studio ↗
2

Install the SDK

Install the Google Generative AI package for your preferred language.

Python
pip install google-generativeai
Node.js
npm install @google/generative-ai
3

Configure Your API Key

Set your API key as an environment variable for security.

macOS / Linux
export GOOGLE_API_KEY="your-api-key-here"
💡 Pro tip: Add this to your ~/.zshrc or ~/.bashrc to persist across sessions.
4

Make Your First Request

Here's a complete example using Gemini 3 Flash.

Python
import google.generativeai as genai
import os

# Configure with API key
genai.configure(api_key=os.environ["GOOGLE_API_KEY"])

# Initialize the model
model = genai.GenerativeModel("gemini-3-flash")

# Generate content
response = model.generate_content("Explain quantum computing in 3 sentences")

print(response.text)
Node.js
import { GoogleGenerativeAI } from "@google/generative-ai";

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

async function run() {
  const result = await model.generateContent(
    "Explain quantum computing in 3 sentences"
  );
  console.log(result.response.text());
}

run();

Common Patterns

📝 Text Generation

response = model.generate_content("Your prompt here")

🖼️ Image Analysis

import PIL.Image
image = PIL.Image.open("image.jpg")
response = model.generate_content(["Describe this image", image])

💬 Chat Conversation

chat = model.start_chat(history=[])
response = chat.send_message("Hello!")
response = chat.send_message("What did I just say?")

⚡ Streaming Response

response = model.generate_content("Long prompt...", stream=True)
for chunk in response:
    print(chunk.text, end="")

Troubleshooting

"API key not found" error+

Make sure your environment variable is set correctly. Try running echo $GOOGLE_API_KEY to verify it's set.

"Model not found" error+

Check the model name spelling. Valid names include gemini-3-flash and gemini-3-pro.

Rate limit exceeded+

The free tier has request limits. Implement exponential backoff or upgrade to a paid plan for higher limits.

Ready to estimate your costs?

Use our calculator to plan your API budget.

Open Cost Calculator →