Lesson 3 of 6
Make your first Gemini API request
Set up an API key, install the Gemini Python library, and send your prompt with a short sample text, then print the response.
6 minClaude, Gemini and ChatGPT
After this lesson, you will be able to set up a Gemini API key, install the Gemini Python library, and send a prompt with a short sample text from a Python script, then print the response.
Get an API key
To call the Gemini API from Python, you need an API key. An API key is a secret string that identifies you to Google and lets the service know you are allowed to make requests. Keep it private: do not share it or put it in code that others can see.
Go to Google AI Studio in your browser and sign in with your Google account. Click the button to get an API key, then create a new key. Copy the key and store it somewhere safe on your computer, such as a password manager. You will use it in the next step.
Set the key as an environment variable
Your Python script will read the key from an environment variable. An environment variable is a value stored in your terminal session that programs can read. This keeps the key out of your code.
On macOS or Linux, open a terminal and run:
export GEMINI_API_KEY="your-api-key-here"On Windows, open a command prompt and run:
set GEMINI_API_KEY=your-api-key-hereReplace your-api-key-here with the key you copied. This sets the variable for the current terminal session only. You will need to set it again if you open a new terminal.
Install the Gemini Python library
Google provides a Python library called google-genai that makes it easy to call the Gemini API. This lesson is written for version 1 of the library. Install it with pip, bound to the major version:
pip install "google-genai>=1,<2"If you use a virtual environment, activate it first. The library needs Python 3.9 or newer.
Write a script that calls the API
Create a new file called first_request.py in your project folder. The script will read the API key from the environment, create a client, and send a prompt with a short sample text.
Open first_request.py and write this code:
import os
from google import genai
# Read the API key from the environment variable
api_key = os.environ["GEMINI_API_KEY"]
# Create a client
client = genai.Client(api_key=api_key)
# The model name is a constant at the top of the file.
# Replace it with a current model from the provider's list of models.
MODEL_NAME = "gemini-2.0-flash"
# The prompt and sample text
prompt = """You are a careful research assistant. Read the text below and list every date it mentions, with the event that happened on that date. Return the answer as a JSON object with one key \"events\". The value must be a list of objects, each with keys \"date\" and \"event\".
Text:
The company was founded on 3 April 2011. Its first product shipped on 15 November 2012. The second product shipped on 9 May 2014."""
# Send the request and print the response
response = client.models.generate_content(
model=MODEL_NAME,
contents=prompt,
)
print(response.text)The script imports the os module to read the environment variable, and the genai module from the library. It creates a client with your API key. The model name is stored in a constant at the top of the file, with a comment telling you to replace it with a current model from the provider's list of models. The prompt is the same one you used in the Gemini chat window in lesson 1, asking for a JSON list of dates and events from a short text. The generate_content method sends the prompt to the model and returns a response object. The response.text attribute contains the model's answer as a string.
Before you run the script, look up a current model name from Google's list of Gemini models and put it in the MODEL_NAME constant. The model name in the example may have been withdrawn since this lesson was written, and a withdrawn name makes the request fail.
Run the script from your terminal:
python first_request.pyThe script prints the model's response. It should be a JSON object with an events key, similar to the answer you got in the chat window. The exact wording may differ, but the structure should match.
Handle errors
If the script fails, check the error message. Common problems are:
- The environment variable is not set, or the key is wrong. Make sure you exported or set
GEMINI_API_KEYin the same terminal where you run the script. - The model name is not valid. Check the provider's list of models and update the constant.
- The library is not installed. Run the pip install command again and check for errors.
- Your API key does not have access to the Gemini API. Go back to Google AI Studio and check your key's permissions.
When the script works, you have made your first Gemini API request from Python. In the next lesson, you will send a long document through the API.
Key takeaways
- An API key is a secret string that identifies you to Google; store it in an environment variable, not in your code.
- Install the Gemini Python library with pip, bound to the major version.
- Create a client with your API key, then call
generate_contentwith a model name and a prompt. - The model name is a constant at the top of the file; replace it with a current model from the provider's list.
- The response object's
textattribute contains the model's answer as a string.
Try it
Write a new Python script called my_first_request.py. Use the same setup as first_request.py: read the API key from the environment, create a client, and set a model name constant. Change the prompt to ask Gemini to list every event with its day, date, and time from this short text: "The meeting is on Monday 12 June at 10:00. The deadline is Friday 16 June at 17:00. The review is on Wednesday 21 June at 14:00." Require the answer as a JSON object with one key "schedule", whose value is a list of objects with keys "day", "date", and "time". Run the script. A good result is valid JSON with exactly those keys, and no extra keys, for all three events.
Official documentation
Written with AI and checked automatically. Tools change, so a command may behave differently in the version you install. Spotted a mistake? Tell us.