DEV Community

Phil IPS
Phil IPS

Posted on

Using the Gemini API with Python (Free Tier)

Using the Gemini API with Python (Free Tier)

Build AI-powered applications using Google's Gemini model via the free tier.

Prerequisites

Quick Start

import requests
import os

# Configuration
OPENROUTER_URL = "https://openrouter.ai/api/v1/chat/completions"
MODEL = "google/gemini-2.0-flash-exp:free"  # Free tier model

def call_gemini(prompt: str, api_key: str, max_tokens: int = 200) -> str:
    """Call the free Gemini API via OpenRouter."""
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    data = {
        "model": MODEL,
        "messages": [{"role": "user", "content": prompt}],
        "temperature": 0.7,
        "max_tokens": max_tokens
    }

    response = requests.post(OPENROUTER_URL, headers=headers, json=data)

    if response.status_code == 200:
        return response.json()["choices"][0]["message"]["content"]
    else:
        raise Exception(f"API Error: {response.status_code} - {response.text}")

# Usage
if __name__ == "__main__":
    api_key = os.getenv("OPENROUTER_API_KEY")
    if not api_key:
        print("Set OPENROUTER_API_KEY environment variable")
        exit(1)

    prompt = "Write a haiku about Python programming"
    try:
        result = call_gemini(prompt, api_key)
        print(f"Prompt: {prompt}")
        print(f"Response: {result}")
    except Exception as e:
        print(f"Error: {e}")
Enter fullscreen mode Exit fullscreen mode

Advanced Example: Chat Loop

import requests
import os

def chat_with_gemini(api_key: str):
    """Interactive chat with Gemini."""
    conversation = []
    url = "https://openrouter.ai/api/v1/chat/completions"
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }

    print("Gemini Chat (type 'quit' to exit)")
    print("-" * 40)

    while True:
        user_input = input("You: ")
        if user_input.lower() in ('quit', 'exit', 'q'):
            break

        conversation.append({"role": "user", "content": user_input})

        data = {
            "model": "google/gemini-2.0-flash-exp:free",
            "messages": conversation,
            "temperature": 0.7,
            "max_tokens": 300
        }

        response = requests.post(url, headers=headers, json=data)

        if response.status_code == 200:
            assistant_msg = response.json()["choices"][0]["message"]["content"]
            conversation.append({"role": "assistant", "content": assistant_msg})
            print(f"Gemini: {assistant_msg}")
        else:
            print(f"Error: {response.status_code}")

        print("-" * 40)

if __name__ == "__main__":
    api_key = os.getenv("OPENROUTER_API_KEY")
    chat_with_gemini(api_key)
Enter fullscreen mode Exit fullscreen mode

Key Parameters

Parameter Range Purpose
temperature 0.0-1.0 Randomness (low=factual, high=creative)
max_tokens 1-4096 Response length limit
model various Choose free models with :free suffix

Error Handling

import time

def robust_call(prompt: str, api_key: str, retries: int = 3) -> str:
    """Call API with exponential backoff retry."""
    for attempt in range(retries):
        try:
            return call_gemini(prompt, api_key)
        except Exception as e:
            if "429" in str(e):  # Rate limited
                wait = 2 ** attempt
                print(f"Rate limited. Waiting {wait}s...")
                time.sleep(wait)
            else:
                raise
    raise Exception("Max retries exceeded")
Enter fullscreen mode Exit fullscreen mode

Best Practices

  1. Secure API keys - Use environment variables
  2. Handle rate limits - Implement exponential backoff
  3. Limit token usage - Set max_tokens to control costs
  4. Validate inputs - Sanitize user prompts
  5. Log usage - Track API calls for debugging

Conclusion

The free Gemini tier via OpenRouter provides an excellent way to integrate AI into Python applications at zero cost. Start with the simple function above, then expand into chatbots, content generators, or analysis tools.

Top comments (0)