Module 5: OpenRouter - Introduction

OpenRouter Account Setup

Overview

You'll create an OpenRouter account and get an API key to access 100+ models.

Time: 15 minutes
Difficulty: Very low


🎯 Objectives

  • ✅ Create an OpenRouter account
  • ✅ Get an API key
  • ✅ Configure .env
  • ✅ Verify with curl

📝 Step 1: Create an Account

1.1 Signup:

  1. Visit: https://openrouter.ai/
  2. Click "Sign Up"
  3. Options:
    • Email + password
    • Google OAuth
    • GitHub OAuth

Recommendation: Google (1 click)


1.2 Verification:

If you used email, verify your inbox (check spam).


💳 Step 2: Credits and Billing

Free credits:

Some users get $1 free for testing.

Check:

  • Dashboard → Credits
  • Available balance

If you need to add credits:

  1. Dashboard → Billing
  2. Add payment method (card)
  3. Add credits ($5, $10, $20, etc.)

Spending limits:

  • Set limit: $10/month (recommended)
  • Alert: $5/month

🔑 Step 3: Get an API Key

3.1 Generate a key:

  1. Dashboard → API Keys
  2. Click "Create Key"
  3. Name: "Module 5 - Learning"
  4. Click "Create"

3.2 Copy the key:

sk-or-v1-abc123...

⚠️ You'll only see it once! Copy it now.


3.3 Format:

OpenRouter API keys:

sk-or-v1-XXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Starts with sk-or-v1-


🔐 Step 4: Configure .env

Create/edit .env:

# .env
OPENROUTER_API_KEY=sk-or-v1-abc123...

Update .gitignore:

# .gitignore
.env
.env.local

✅ Step 5: Verify with Curl

curl https://openrouter.ai/api/v1/models \
  -H "Authorization: Bearer $OPENROUTER_API_KEY"

Expected output (first lines):

{
  "data": [
    {
      "id": "openai/gpt-3.5-turbo",
      "name": "GPT-3.5 Turbo"
    },
    {
      "id": "anthropic/claude-3-haiku",
      "name": "Claude 3 Haiku"
    },
    ...
  ]
}

If you see a list of models: ✅ Your API key works!


🧪 Step 6: Test with Python

from dotenv import load_dotenv
import os
import requests

load_dotenv()
api_key = os.getenv("OPENROUTER_API_KEY")

if not api_key:
    print("❌ API key not found")
    exit(1)

print(f"✅ API Key: {api_key[:20]}...")

# List models
headers = {"Authorization": f"Bearer {api_key}"}
response = requests.get(
    "https://openrouter.ai/api/v1/models",
    headers=headers
)

if response.status_code == 200:
    models = response.json()["data"]
    print(f"✅ API works! {len(models)} models available")
    print(f"Examples: {models[0]['id']}, {models[1]['id']}")
else:
    print(f"❌ Error: {response.status_code}")

🐛 Troubleshooting

Error: 401 Unauthorized

Cause: Incorrect API key

Solution:

  • Verify the key starts with sk-or-v1-
  • No spaces at the start/end
  • Regenerate the key if needed

Error: 402 Payment Required

Cause: Credits exhausted

Solution:

  • Dashboard → Add credits
  • Minimum $5

📊 Dashboard Overview

Explore the dashboard:

1. Models:

  • 100+ models available
  • Filters: Provider, Cost, Context

2. Usage:

  • Requests per model
  • Accumulated cost
  • Charts

3. API Keys:

  • Manage keys
  • Revoke keys

✅ Summary

  • OpenRouter account created
  • API key obtained (sk-or-v1-...)
  • .env configured
  • Verified with curl and Python
  • Ready to use 100+ models

Next: 03-your-first-requests.md

You'll make requests to multiple models (GPT, Claude, Gemini) with the same API.

Time: 20 min