Getting Started with the Gemini API
The Gemini API lets you harness powerful language models for a variety of tasks—from generating text to analyzing data. This tutorial will walk you through setting up your environment, making your first request, and interpreting the response, all with a short, runnable Python example.
1. Prerequisites
- Python 3.8+ installed on your machine.
-
requestslibrary (pip install requests). - A Gemini API key from the Gemini console (you’ll need to create an account and enable the API).
2. Set Up the Request
Below is a minimal Python script that sends a prompt to the gemini-3.6-flash:free model and prints the generated text.
import requests import json API_KEY = "YOUR_API_KEY_HERE" MODEL = "gemini-3.6-flash:free" URL = f"https://api.unorouter.com/v1/chat/completions" headers = { "Authorization": f"Bearer {API_KEY}", "Content-Type": "application/json" } payload = { "model": MODEL, "messages": [{"role": "user", "content": "Explain in two sentences what GPT-4 is."}], "stream": False } response = requests.post(URL, headers=headers, json=payload) data = response.json() if response.ok: # Extract the assistant's reply assistant_reply = data["choices"][0]["message"]["content"] print(assistant_reply) else: print(f"Error: {data.get('error', 'Unknown error')}") How It Works
- Headers – We authenticate with the API key and set the content type to JSON.
- Payload – The
modelfield selects the free Gemini model. Themessagesarray contains a single user message; you can adjust the prompt as needed. - Response – On a successful request (
200 OK), the generated text lives underchoices[0].message.content.
3. Running the Script
Save the code to a file, e.g., gemini_demo.py, replace YOUR_API_KEY_HERE with your actual key, and run:
python gemini_demo.py You should see a concise explanation of GPT‑4 printed to the console.
4. Tips & Best Practices
- Rate Limits – The free tier includes a generous number of requests, but monitor your usage to avoid hitting limits.
- Prompt Engineering – Clear, specific prompts yield more predictable results. Experiment with temperature settings if the API supports them.
- Error Handling – Always check the response status and handle potential errors (e.g., network issues, invalid keys).
5. Next Steps
- Explore other models (e.g.,
gemini-2.0-flash-exp) for different capabilities. - Integrate the API into larger applications using the official SDKs or REST calls.
- Build simple tools like text summarizers, code assistants, or chatbots.
With just a few lines of code, you’ve unlocked the Gemini API’s potential. Happy coding!



