Skip to main content
OpenRouter provides unified access to multiple AI vision models through a single API. This guide walks you through setting up your account and obtaining an API key.

What is OpenRouter?

OpenRouter is a unified API gateway that provides access to multiple AI models from providers like:
  • OpenAI (GPT-4o, GPT-4o-mini)
  • Anthropic (Claude 3.5 Sonnet, Claude 3 Opus)
  • Google (Gemini Pro, Gemini Flash)
  • And many more
Benefits:
  • Single API key for all models
  • Pay-as-you-go pricing
  • Model fallbacks and routing
  • Usage analytics and monitoring

Step 1: Create an Account

1

Visit OpenRouter

2

Sign Up

Click “Sign In” in the top right corner and choose a sign-in method:
  • GitHub
  • Google
  • Email
3

Verify Your Account

Follow the verification process for your chosen sign-in method.

Step 2: Add Credits

OpenRouter uses a prepaid credit system.
1

Navigate to Credits

After signing in, go to openrouter.ai/credits
2

Add Credits

  • Click “Add Credits”
  • Choose an amount (minimum $5)
  • Complete payment via Stripe
3

Verify Balance

Your credit balance will appear in the top right corner of the dashboard.
Pricing for Invoice OCR:
  • Most invoices cost 0.0010.001-0.01 to process
  • A $5 credit can process 500-5000 invoices depending on model choice
  • See Model Selection for cost comparisons

Step 3: Generate API Key

1

Go to Keys Page

Navigate to openrouter.ai/keys
2

Create New Key

Click “Create Key” and provide:
  • Name: e.g., “Invoice OCR Development”
  • Credit limit (optional): Set a spending limit for this key
  • Allowed origins (optional): Restrict to specific domains
3

Copy the Key

Your API key will be displayed only once. Copy it immediately.Format: sk-or-v1-... (64 characters)
4

Save to .env.local

Create or edit .env.local in your project root:
OPENROUTER_API_KEY=sk-or-v1-your-key-here
Security Best Practices:
  • Never commit .env.local to version control
  • Don’t share your API key publicly
  • Don’t embed keys in client-side code
  • Rotate keys regularly
  • Use separate keys for development and production

Step 4: Verify Setup

Test your configuration:
1

Start Development Server

npm run dev
2

Open the Application

3

Upload a Test Invoice

  • Click “Upload Invoice” or drag-and-drop an image
  • Select an OCR mode (try “Normalized GST v4”)
  • Click “Extract”
4

Check Results

If setup is correct, you’ll see:
  • Extracted invoice data in JSON format
  • No API key errors in the console

API Key Management

Multiple Keys

Create separate API keys for different environments:
.env.local (development)
OPENROUTER_API_KEY=sk-or-v1-dev-key-here
.env.production (production)
OPENROUTER_API_KEY=sk-or-v1-prod-key-here

Key Limits

Set spending limits per key:
  1. Go to openrouter.ai/keys
  2. Click on a key to edit
  3. Set “Credit Limit” (e.g., $10/month)
  4. Save changes
This prevents unexpected charges if your application has high usage or is compromised.

Rotating Keys

Rotate keys periodically for security:
1

Create New Key

Generate a new key at openrouter.ai/keys
2

Update .env.local

Replace the old key with the new one
3

Restart Server

Stop and restart your development server
4

Verify

Test an API call to ensure the new key works
5

Delete Old Key

Once verified, delete the old key from OpenRouter dashboard

Monitoring Usage

Track your API usage and costs:

Usage Dashboard

  1. Go to openrouter.ai/activity
  2. View:
    • Request count by model
    • Total cost per day/week/month
    • Average cost per request
    • Error rates

Per-Key Analytics

View usage for a specific key:
  1. Go to openrouter.ai/keys
  2. Click on a key name
  3. View detailed usage stats

Troubleshooting

Possible causes:
  • Key copied incorrectly (check for extra spaces)
  • Key was deleted from OpenRouter dashboard
  • Key expired or reached credit limit
Solution:
  • Verify key in .env.local matches dashboard
  • Generate new key if needed
  • Check credit balance
Error message: “Insufficient credits” or 402 status codeSolution:
  1. Go to openrouter.ai/credits
  2. Add more credits
  3. Retry the request
Error message: “Rate limit exceeded” or 429 status codeSolution:
  • Wait a few seconds and retry
  • Implement exponential backoff in your code
  • Upgrade to a higher tier plan if needed
Error message: “Model not found” or “Model not available”Solution:
  • Check available models at openrouter.ai/models
  • Some models require allowlist access
  • Update OPENROUTER_MODEL to a supported model

OpenRouter Headers

Invoice OCR sends the following headers to OpenRouter:
headers: {
  'Content-Type': 'application/json',
  'Authorization': `Bearer ${process.env.OPENROUTER_API_KEY}`,
  'HTTP-Referer': process.env.OPENROUTER_SITE_URL || 'http://localhost:3000',
  'X-Title': process.env.OPENROUTER_APP_NAME || 'Invoice OCR',
}
Why these headers?
  • HTTP-Referer: Identifies your application in OpenRouter analytics
  • X-Title: Displays a friendly name in your usage dashboard
Configure these in .env.local:
OPENROUTER_SITE_URL=https://your-domain.com
OPENROUTER_APP_NAME=Your App Name

Next Steps

Environment Variables

Complete reference for all configuration options

Model Selection

Choose the best model for your use case

Quick Start

Start extracting invoice data

API Reference

Integrate OCR into your application

Build docs developers (and LLMs) love