OpenRouter Setup Guide

Step-by-step guide to setting up OpenRouter integration with Claude Code.

Prerequisites

  • OpenRouter API key
  • Claude Code installed and configured
  • Basic understanding of API integrations

Setup Steps

1. Obtain API Key

  1. Visit OpenRouter
  2. Create an account or sign in
  3. Add credits to your account (prepaid system)
  4. Navigate to API keys section
  5. Generate a new API key

2. Configure Environment

Add your OpenRouter API key to your environment:

export OPENROUTER_API_KEY="your-api-key-here"

Or add to your .env file:

OPENROUTER_API_KEY=your-api-key-here

3. Install Dependencies

OpenRouter is OpenAI-compatible, so you can use the OpenAI SDK:

npm install openai

For Vercel AI SDK integration:

npm install @openrouter/ai-sdk-provider

4. Basic Configuration

import { OpenAI } from 'openai';
 
const openai = new OpenAI({
  apiKey: process.env.OPENROUTER_API_KEY,
  baseURL: "https://openrouter.ai/api/v1/",
});
 
// Example usage with Claude 3.7 Sonnet
const response = await openai.chat.completions.create({
  model: 'anthropic/claude-3.7-sonnet',
  messages: [
    { role: 'user', content: 'Hello!' }
  ]
});

5. Direct API Usage

For direct API calls:

const response = await fetch('https://openrouter.ai/api/v1/chat/completions', {
  method: 'POST',
  headers: {
    'Authorization': `Bearer ${process.env.OPENROUTER_API_KEY}`,
    'Content-Type': 'application/json',
    'HTTP-Referer': 'https://yourapp.com', // Optional
    'X-Title': 'Your App Name' // Optional
  },
  body: JSON.stringify({
    model: 'anthropic/claude-3.7-sonnet',
    messages: [
      { role: 'user', content: 'Hello!' }
    ],
    stream: true // Optional: Enable streaming responses
  })
});

Key Features

  • Hundreds of Models: Access to models from OpenAI, Anthropic, Google, Meta, and more through one unified API
  • Automatic Fallback: Intelligent routing to available providers with built-in redundancy
  • OpenAI Compatible: Drop-in replacement for OpenAI API with full compatibility
  • Pay-Per-Use Pricing: No subscriptions - pay only for what you use with transparent per-token pricing
  • Better Uptime: Distributed infrastructure ensures reliability

Best Practices

  1. Use Environment Variables: Always store API keys securely
  2. Monitor Usage: Track token usage and costs via OpenRouter dashboard - pay-per-use model
  3. Handle Errors: Implement retry logic for rate limits and failures
  4. Model Selection: Include organization prefix (e.g., ‘anthropic/claude-3.7-sonnet’)
  5. Cost Optimization: Use free or budget models for simple tasks, premium models only when needed
  6. Credits System: Add credits to your account before making API calls

Next Steps

🧭 Quick Navigation

← Back to OpenRouter | Integrations Home | Examples →

Verifications

  • OpenRouter API Documentation: Verified base URL as https://openrouter.ai/api/v1 from official documentation (2025)
  • Installation Methods: Confirmed OpenAI SDK compatibility and @openrouter/ai-sdk-provider package availability
  • Authentication: Verified prepaid credit system and API key generation process - no subscriptions required
  • Features: Confirmed access to hundreds of models from multiple providers (OpenAI, Anthropic, Google, Meta), automatic fallback, and full OpenAI compatibility
  • Pricing Model: Verified pay-per-use model with transparent per-token pricing - Claude 3.7 Sonnet: 15/million output tokens (including thinking tokens)
  • Claude Integration: Confirmed Claude 3.7 Sonnet availability with model identifier ‘anthropic/claude-3.7-sonnet’ - first hybrid reasoning model with controllable thinking tokens
  • Cost Optimization: Verified recommendation to use free/budget models for simple tasks and premium models only when necessary