First Gemini API Call (using Python + Colab)
Introduction to first Gemini API call
we’re going to make our first Gemini API call using Python, and we’ll do it inside Google Colab, which is one of the easiest ways to write and run Python code in the cloud.
By the end of this lesson, you’ll be able to:
- open a Colab notebook
• install the Gemini Python library
• connect to the Gemini API
• send your first prompt from Python
• receive the model’s response in your code
Once you understand this step, you’ve officially moved from testing prompts to building AI-powered applications.
Step 1: Open Google Colab
Start by opening your browser and going to Google Colab.
https://colab.research.google.com
Google Colab is an online Python environment where you can write and run code without installing anything on your computer.
Click New Notebook.
You should now see a blank notebook with a code cell ready.
This notebook will act as our small development environment where we can interact with the Gemini API.
Step 2: Install the Gemini Python Library
Before our code can communicate with Gemini, we need to install the Python library that allows our notebook to interact with the API.
In the first code cell, type the following command:
!pip install -q google-generativeai
Then run the cell.
You can run a cell by clicking the play button on the left side of the cell.
This installs the official Gemini Python SDK.
If you’re curious about what this library does or want to explore more examples later, you can visit the official documentation here:
Once the installation finishes, we’re ready to connect to the Gemini API.
Step 3: Generate a Gemini API Key
Before your code can access Gemini, Google needs to know who is making the request.
That’s done using something called an API key.
Open a new browser tab and go to:
https://aistudio.google.com/app/apikey
If this is your first time, click Create API Key.
Google will generate a key for you.
Copy that key and keep it private. Think of it like a password that allows your program to access the AI service.
Once you’ve copied the key, go back to your Colab notebook.
Step 4: Configure the API in Python
In a new code cell, paste the following code:
import google.generativeai as genai
genai.configure(api_key=”PASTE_YOUR_API_KEY_HERE”)
Replace the text inside the quotes with the API key you copied.
This code imports the Gemini library and configures it to use your API key.
Run the cell.
You won’t see any output yet, but your notebook is now connected to the Gemini service.
Step 5: Select a Gemini Model
Next, we need to choose which model we want to use.
Add another code cell and type:
model = genai.GenerativeModel(“gemini-1.5-flash”)
Gemini offers several models with different capabilities.
For most development tasks, Gemini Flash is a great starting point because it is fast and efficient.
If you want to explore the available models later, you can see them here:
https://ai.google.dev/gemini-api/docs/models
Run the cell.
Now our notebook knows which model it will send requests to.
Step 6: Send Your First Prompt
Now we’re ready to send our first prompt from Python.
In a new cell, type the following:
response = model.generate_content(
“Explain cloud computing in simple terms.”
)
Run the cell.
This line sends a request to the Gemini model with the instruction we provided.
But we haven’t displayed the response yet.
So in the next line, add:
print(response.text)
Run the cell again.
You should now see the model’s response printed directly inside your notebook.
That response is coming from the Gemini model in real time.
Step 7: Try Another Prompt
Let’s try another example so you can see how flexible this interaction is.
Replace the prompt with something more structured:
response = model.generate_content(
“Give me three real-world applications of artificial intelligence in healthcare.”
)
print(response.text)
Run the cell again.
You’ll see Gemini generate a list of examples.
At this point, you can experiment with any prompt you want.
Your code is now able to communicate directly with the model.
Understanding What Just Happened
Let’s briefly recap what we just did.
Our notebook followed four simple steps.
First, we installed the Gemini Python library.
Second, we connected our notebook to the Gemini API using an API key.
Third, we selected a model.
And fourth, we sent a prompt and printed the response.
This basic interaction is the core of almost every generative AI application.
Whether you’re building a chatbot, a writing assistant, a research tool, or a document analyzer, the application is essentially sending prompts to a model and receiving responses.
How This Fits into Real AI Applications
In real systems, this code usually sits inside a backend service.
For example, a user might type a question into a web app.
That app sends the question to a server.
The server then sends that input to the Gemini API.
The model generates a response.
And the application displays that response back to the user.
So even though this example looks simple, it represents the core interaction that powers modern AI applications.
Summary – Key Takeaways
You learned how to:
- open a Google Colab notebook
- install the Gemini Python library
- generate an API key
- connect Python to the Gemini API
- send prompts from Python
- display the model’s response
This is the point where prompt experimentation turns into real development.
In the next lesson, we’ll start turning this basic API call into something more useful by building a small AI-powered tool.
Citations :
Watch this video for further learning:
Hashtags :
- #GPSforLifeAndCareer
- #ConsciousIntelligence
- #Kidswiki
- #MindInTheMaking
- #RelevanceInEducation
- #MeaningfulLearning
- #RealWorldSkills
- #SafeContent
- #360DegreeDevelopment
- #Skillioma
- #BeyondCurriculum
- #SkillsBeyondSchool
