In this categoryGemini ยท 24
- How to Install the Gemini CLI and Run Your First PromptStart
- How to Get a Gemini API Key from Google AI Studio
- How to Call the Gemini API from Python
- How to Give Gemini CLI Project Context with a GEMINI.md File
- How to Choose the Right Gemini Model for Coding Tasks
- How to Fix a Bug in Your Codebase with the Gemini CLI
- How to Set Up Gemini Code Assist in VS Code
- How to Stream Gemini Responses in Node.js
- How to Feed a Long Document into Gemini's Long Context
- How to Run the Gemini CLI Non-Interactively in Scripts
- How to Analyze a Video File with the Gemini API
- How to Connect an MCP Server to the Gemini CLI
- How to Fix Common Gemini CLI Authentication Errors
- How to Cache Long Context in the Gemini API to Cut Costs
- How to Summarize a YouTube Video with Gemini
- How to Analyze PDFs and Docs in the Gemini Chat App
- How to Extract Data from Images and Screenshots with Gemini
- How to Generate a Short Video with Veo in Gemini
- How to Ground Gemini Answers in Your Own Documents
- How to Use Gemini Inside Google Docs to Draft Faster
How to Call the Gemini API from Python
Install the google-genai SDK and generate text from Gemini in a few lines of Python.
When you want Gemini inside a script or app rather than the terminal, the official Python SDK is the cleanest route. The package is google-genai, and it reads your API key from the environment so no secrets end up in code. This guide gets a first generation working.
What you need
- Python 3.9 or newer
- A Gemini API key in the GEMINI_API_KEY environment variable
- pip for installing packages
- About 5 minutes
Step 1: Install the SDK
Install the google-genai package into your project, ideally inside a virtual environment so it does not pollute your system Python.
python -m venv .venv
source .venv/bin/activate
pip install google-genaiStep 2: Write a minimal script
Create a file and import the client. With no key passed explicitly, the SDK looks for GEMINI_API_KEY automatically. Pick a current model such as gemini-2.5-flash, which is fast and inexpensive for everyday work.
from google import genai
client = genai.Client() # reads GEMINI_API_KEY from the environment
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="Write a one-line git commit message for fixing a null pointer in auth.",
)
print(response.text)Step 3: Run it
Execute the script. If the key is set correctly you get a single generated line printed to your terminal.
Result
You can now generate text from Gemini in any Python program. From here you can loop over inputs, feed it file contents, or wire it into a web handler, all on the same client.models.generate_content call.
Watch related tutorials
16:12
14:00
3:58:44
35:00
05:00
12:00New guides in your inbox
Fresh step-by-step how-to guides as we publish them. One email a week, no more.