Many early-career developers I mentor believe they need a big cloud budget or an enterprise account to start with Generative AI. But you can actually build strong, production-ready prototypes for free. Today, we’ll create one of these scripts together. I’ll show you how to set up and use the Gemini API for free with Python.
Use the Gemini API for Free: Getting Started
Before we write code, we need two things:
- an access key
- and the right tools.
When you use the Gemini API, your Python script works like a messenger. It sends your text prompt to Google’s servers, where the main processing happens, and then brings the response back to your terminal.
For this to work, Google needs to know who is sending the message. That’s why you need an API key.
Let’s start setting up and using the Gemini API for free.
Step 1: Get Your Free API Key
Go to Google AI Studio, which is a web-based prototyping tool for developers. You only need a regular Google account, not a Google Cloud account. Click “Get API Key” to generate your key. Keep this key private, just like a password.
Step 2: Install the Right Library
Google has updated their Python SDK to make it easier to use. You’ll need to install the google-genai library.
Open your terminal or command prompt and run:
pip install google-genai
Step 3: Writing Your First Script with the Gemini API
Now let’s write some code. This will be a simple, working script to connect to the Gemini API.
We’ll use gemini-2.5-flash, which is the default model right now. It’s fast, affordable, and great for everyday reasoning tasks.
Create a new Python file, such as gemini_test.py, and write this code into it:
from google import genai
# 1. Add your API key directly
API_KEY = "Your_API_Key"
# 2. Initialize the client with the API key
client = genai.Client(api_key=API_KEY)
# 3. Pick your model and ask a question
response = client.models.generate_content(
model="gemini-2.5-flash",
contents="Explain how AI works in one short and funny sentence."
)
print(response.text)Output:
AI is a hyper-caffeinated librarian who swallowed the internet and now tries to guess what you want, often brilliantly, sometimes bizarrely.
The API returns more than just a string. It gives you a detailed response object with metadata, safety ratings, and token usage. When you use response.text, you get only the raw string the model created.
Run the script in your terminal. You should see a short, clever explanation of AI appear on your screen.
Closing Thoughts
That’s how you can set up and use the Gemini API for free with Python.
After you get this script working, try changing the prompt. Ask it to format the output as JSON or to summarize a long piece of text. By experimenting with these inputs and outputs, you’ll start to understand how Large Language Models work in practice.
If you found this article helpful, you can follow me on Instagram for daily AI tips and practical resources. You may also be interested in my latest book, Hands-On GenAI, LLMs & AI Agents, a step-by-step guide to prepare you for careers in today’s AI industry.





