DEV Community

Alex Spinov
Alex Spinov

Posted on

OpenRouter Has a Free API — Heres How to Access 100+ AI Models With One API Key

OpenRouter is a unified API for 100+ AI models — GPT-4, Claude, Llama, Mistral, Gemini — all through one endpoint. Switch models by changing one string, not your entire codebase.

Why OpenRouter?

  • 100+ models: GPT-4, Claude, Llama 3, Mistral, Gemini, DeepSeek
  • One API key: Access all providers
  • OpenAI-compatible: Drop-in replacement for OpenAI SDK
  • Automatic fallback: If one provider is down, routes to another
  • Pay per token: No subscriptions, no minimums
  • Free models: Some models available at no cost

Drop-In OpenAI Replacement

import OpenAI from 'openai';

const client = new OpenAI({
  baseURL: 'https://openrouter.ai/api/v1',
  apiKey: 'sk-or-your-key',
});

const response = await client.chat.completions.create({
  model: 'anthropic/claude-sonnet-4',  // Just change this string
  messages: [{ role: 'user', content: 'Explain quantum computing simply' }],
});

console.log(response.choices[0].message.content);
Enter fullscreen mode Exit fullscreen mode

Python

import openai

client = openai.OpenAI(
    base_url='https://openrouter.ai/api/v1',
    api_key='sk-or-your-key',
)

response = client.chat.completions.create(
    model='meta-llama/llama-3.1-70b-instruct',
    messages=[{'role': 'user', 'content': 'Write a Python quicksort'}],
)
print(response.choices[0].message.content)
Enter fullscreen mode Exit fullscreen mode

curl

curl https://openrouter.ai/api/v1/chat/completions \
  -H 'Authorization: Bearer sk-or-your-key' \
  -H 'Content-Type: application/json' \
  -d '{
    "model": "google/gemini-2.0-flash",
    "messages": [{"role": "user", "content": "Hello!"}]
  }'
Enter fullscreen mode Exit fullscreen mode

Streaming

const stream = await client.chat.completions.create({
  model: 'anthropic/claude-sonnet-4',
  messages: [{ role: 'user', content: 'Write a poem about code' }],
  stream: true,
});

for await (const chunk of stream) {
  process.stdout.write(chunk.choices[0]?.delta?.content || '');
}
Enter fullscreen mode Exit fullscreen mode

Model Routing (Auto-Select Best Model)

curl https://openrouter.ai/api/v1/chat/completions \
  -H 'Authorization: Bearer sk-or-your-key' \
  -d '{
    "model": "openrouter/auto",
    "messages": [{"role": "user", "content": "Complex math problem..."}]
  }'
Enter fullscreen mode Exit fullscreen mode

OpenRouter automatically picks the best model for your prompt.

Get Available Models

curl https://openrouter.ai/api/v1/models
Enter fullscreen mode Exit fullscreen mode

Check Your Usage

curl https://openrouter.ai/api/v1/auth/key \
  -H 'Authorization: Bearer sk-or-your-key'
Enter fullscreen mode Exit fullscreen mode

Real-World Use Case

A startup used GPT-4 for everything ($2,000/mo). After switching to OpenRouter, they routed simple tasks to Llama 3 (free) and only used Claude for complex reasoning. Monthly AI costs dropped to $400 — same quality for the tasks that mattered.


Need to automate data collection? Check out my Apify actors for ready-made scrapers, or email spinov001@gmail.com for custom solutions.

Top comments (0)