Google’s Gemini API tutorial - how to get started with developing Gemini-based applications on Ubuntu and Windows
Last update: January 11, 2025
Google’s Gemini API tutorial - how to get started with developing Gemini-based applications on Ubuntu and Windows
Generative AI is revolutionizing technology by enabling developers to create smarter, more intuitive applications. Google’s Gemini API is a powerful tool that facilitates content generation and machine learning. This guide will walk you through the steps to test the Gemini API on both Ubuntu and Windows systems.
While Gemini 1.5 Flash may not be the most advanced model, it is completely free to use, making it an excellent starting point for exploring AI-driven software development. This guide will primarily focus on Gemini-1.5-Flash, but the steps outlined can be applied to other Gemini models as well.
Getting Started with the Gemini API
Before diving into the technical setup, you need to create a Gemini account and obtain an API key to authenticate your requests.
Obtain Your API Key
- Visit the Google AI Studio and sign in with your Google account.
- Follow the on-screen instructions to complete your account setup.
Generate Your API Key
- Navigate to the API Keys section in the Gemini dashboard.
- Create and securely store your API key—you will need it for every API request.
Testing the Gemini API
You can test the Gemini API using various tools, including the Linux terminal, Windows PowerShell, Postman, and Python. Below are step-by-step instructions for each method.
Testing from a Linux Terminal (or WSL)
Install cURL
Ensure cURL
is installed by running:
curl --version
If not installed, use:
sudo apt update && sudo apt install curl
Send an API Request
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=YOUR_API_KEY" \
-H "Content-Type: application/json" \
-X POST \
-d '{
"contents": [
{
"parts": [
{"text": "Explain how AI works"}
]
}
]
}'
Don't forget to replace
YOUR_API_KEY
with your actual API key.
The response returns formatted as json, similar to this:
{
"response": {
"text": "AI works by processing data using algorithms and models to simulate human intelligence, enabling tasks such as learning, reasoning, and decision-making."
}
}
Testing from Windows PowerShell
If you're on MS Windows, you can use use PowerShell (accessible via the Windows terminal) to query Gemini. Run the following commands:
Prepare the Request
$headers = @{ "Content-Type" = "application/json" }
$body = @{
contents = @(
@{
parts = @(
@{
text = "Explain how AI works"
}
)
}
)
} | ConvertTo-Json -Depth 10
Send the Request
Invoke-WebRequest -Uri "https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=YOUR_API_KEY" `
-Headers $headers -Method POST -Body $body
Don't forget to replace
YOUR_API_KEY
with your actual API key.
Check the Output
The API response will appear in the PowerShell console.
Testing with Postman
-
Install and Open Postman: Download and install Postman, then launch the app.
-
Create a New Request: Click New Tab or New Request, set the method to
POST
, and enter the following URL:https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key=YOUR_API_KEY
-
Configure Headers:
- Key:
Content-Type
- Value:
application/json
- Key:
-
Add Request Body:
- Select the Body tab and choose raw.
- Paste this JSON:
{ "contents": [ { "parts": [ { "text": "Explain how AI works" } ] } ] }
-
Send the Request: Click the Send button to view the response.
Testing with a Python Script in VSCode
1. Create a Python File
- Open VSCode and create a folder named
Gemini
in your Documents directory. - Create a new Python file, for example
test_gemini_api.py
.
2. Install Python Dependencies
- Create a Python virtual environment and activate it:
python3 -m venv .venv source .venv/bin/activate
- Install the
requests
library:pip install requests
3. Write the Script
import requests
import json
# Replace with your actual API Key
api_key = 'YOUR_API_KEY'
# Define the URL for the Gemini API endpoint
url = f"https://generativelanguage.googleapis.com/v1beta/models/gemini-1.5-flash:generateContent?key={api_key}"
# Define the payload
payload = {
"contents": [
{
"parts": [
{"text": "Explain how AI works"}
]
}
]
}
# Set the headers
headers = {
'Content-Type': 'application/json'
}
# Send the POST request
response = requests.post(url, headers=headers, data=json.dumps(payload))
# Print the response
if response.status_code == 200:
print("Response:", response.json())
else:
print(f"Error: {response.status_code}, {response.text}")
4. Run the Script
Run the script in the terminal:
python3 gemini_api.py
If everything is set up correctly, you will see a JSON response with AI-generated content.
Conclusion
Congratulations! You've successfully tested the Gemini API across multiple platforms. From setting up your environment to sending requests and interpreting responses, you're now ready to build more advanced applications powered by Google’s Gemini AI. Happy coding!