Back to Blog

Claude Code Setup Guide: Use Your Own API Key

Learn how to configure Claude Code with your own API key for 77% cheaper access to Claude models via derouter.ai

Claude Code Setup Guide: Use Your Own API Key

You can configure Claude Code with your own API key by setting environment variables to point to a custom API endpoint, allowing you to access Claude models at up to 77% lower cost. This guide shows you exactly how to set up Claude Code API key configuration using derouter.ai as your API provider.

What You'll Learn

By following this tutorial, you'll learn how to:

  • Configure Claude Code with environment variables for custom API access
  • Set up derouter.ai as your API provider for significant cost savings
  • Test your configuration to ensure everything works correctly
  • Troubleshoot common setup issues

Prerequisites

Before starting this claude code setup process, ensure you have:

  • Claude Code CLI installed on your system
  • A derouter.ai account with API credits (minimum $10 top-up)
  • Basic command line familiarity
  • Your derouter.ai API key ready

If you don't have a derouter.ai account yet, sign up at https://derouter.ai using email authentication with OTP verification. The platform accepts USD payments and crypto via NOWPayments, with no subscription fees required.

Step-by-Step Claude Code API Key Configuration

Step 1: Obtain Your API Key from derouter.ai

First, log into your derouter.ai dashboard and generate an API key. The platform provides access to multiple Claude models:

  • claude-opus-4-6: $1.14 input / $5.71 output per 1M tokens (vs Anthropic official $5/$25 — 77% savings)
  • claude-sonnet-4-6: $0.69 input / $3.43 output per 1M tokens (vs official $3/$15 — 77% savings)
  • claude-haiku-4-5: $0.23 input / $1.14 output per 1M tokens (vs official $1/$5 — 77% savings)

Prices are subject to change; refer to the derouter.ai dashboard for current rates.

Step 2: Set Environment Variables

Claude Code configuration relies on two key environment variables. Set them according to your operating system:

For Linux/macOS:

Set environment variables on Linux/macOS
export ANTHROPIC_API_KEY=\

For Windows Command Prompt:

Set environment variables on Windows
set ANTHROPIC_API_KEY=your-derouter-api-key-here
set ANTHROPIC_BASE_URL=https://api.derouter.ai/proxy/v1

For Windows PowerShell:

Set environment variables in PowerShell
$env:ANTHROPIC_API_KEY = \

Step 3: Make Variables Permanent

To avoid setting these variables every time you open a new terminal, add them to your shell profile:

For Bash users (.bashrc or .bash_profile):

Add to .bashrc for persistence
echo 'export ANTHROPIC_API_KEY=\

For Zsh users (.zshrc):

Add to .zshrc for persistence
echo 'export ANTHROPIC_API_KEY=\

Step 4: Verify Environment Variables

Check that your AI coding API configuration is properly set:

Verify environment variables are set
echo $ANTHROPIC_API_KEY
echo $ANTHROPIC_BASE_URL

You should see your API key and the derouter.ai base URL displayed.

Testing Your Configuration

Basic Functionality Test

Test your claude code setup by running a simple query:

Test Claude Code with a simple query
claude-code \

If configured correctly, Claude Code will use derouter.ai's API endpoint to process your request at significantly reduced costs.

Model Selection Test

You can specify which Claude model to use in your requests:

Test with specific model selection
claude-code \

Code Generation Test

Test the AI coding capabilities with a more complex request:

Test code generation functionality
claude-code \

Integration with Code Editors

Cursor IDE Setup

If you're using Cursor IDE, you can also configure it to use derouter.ai:

  1. Open Cursor Settings
  2. Navigate to Models > OpenAI API
  3. Set API Base URL: https://api.derouter.ai/openai/v1
  4. Set your derouter.ai API key
  5. Save the configuration

This allows you to use the same cost-effective API access within your IDE.

Monitoring Usage and Costs

Check Account Balance

Monitor your spending through the derouter.ai dashboard. The platform provides detailed usage analytics and real-time balance updates.

Cost Comparison

With derouter.ai pricing, a typical development session using Claude Sonnet costs approximately:

  • 100K input tokens: $0.069 (vs $3.00 official)
  • 50K output tokens: $0.172 (vs $7.50 official)
  • Total: $0.241 vs $10.50 official pricing

This represents a 97.7% cost reduction for typical AI coding workflows.

Troubleshooting Common Issues

Issue 1: "API Key Not Found" Error

If Claude Code can't find your API key:

  1. Verify environment variables are set correctly
  2. Restart your terminal session
  3. Check for typos in variable names
  4. Ensure no extra spaces in the API key

Issue 2: Connection Timeout

If you experience connection timeouts:

  1. Verify your internet connection
  2. Check if the API endpoint URL is correct
  3. Ensure your derouter.ai account has sufficient credits
  4. Try a different network if behind restrictive firewalls

Issue 3: Authentication Failed

For authentication errors:

  1. Confirm your API key is valid and active
  2. Check your derouter.ai account status
  3. Verify the API key hasn't expired
  4. Regenerate the API key if necessary

Issue 4: Model Not Available

If a specific model isn't accessible:

  1. Check the model name spelling (use claude-opus-4-6, not claude-opus-4.6)
  2. Verify the model is available on derouter.ai
  3. Ensure your account has access to the requested model
  4. Try with a different model like claude-haiku-4-5

Advanced Configuration Options

Using Different Models

You can set a default model preference:

Set default model preference
export ANTHROPIC_MODEL=\

Batch Processing

For processing multiple files or requests, you can create scripts that leverage your claude code configuration:

Batch process Python files
#!/bin/bash
for file in *.py; do
    echo \

Cost Optimization Tips

Choose the Right Model

  • Use claude-haiku-4-5 for simple tasks and quick responses
  • Use claude-sonnet-4-6 for balanced performance and cost
  • Reserve claude-opus-4-6 for complex reasoning tasks

Optimize Prompt Length

Shorter, more focused prompts reduce input token costs while maintaining output quality.

Monitor Token Usage

Track your token consumption patterns to optimize your coding workflows and predict monthly costs.

<FAQ items={[{"question": "Can I use multiple API keys with Claude Code?", "answer": "Claude Code uses environment variables, so you can only have one active configuration at a time. To switch between different API providers, you'll need to update the ANTHROPIC_API_KEY and ANTHROPIC_BASE_URL environment variables."}, {"question": "Does derouter.ai support all Claude models available in Claude Code?", "answer": "derouter.ai supports Claude Opus 4.6, Claude Sonnet 4.6, and Claude Haiku 4.5, which covers the main models used with Claude Code. The platform also provides access to GPT models if you need alternatives."}, {"question": "What happens if my derouter.ai account runs out of credits?", "answer": "When your account balance reaches zero, API requests will fail with an authentication error. You'll need to top up your account with a minimum of $10 to continue using the service. The platform accepts both USD payments and cryptocurrency via NOWPayments."}, {"question": "Is it safe to use a third-party API provider like derouter.ai?", "answer": "derouter.ai operates as a legitimate API access platform with standard security practices. Your API key is used only for authentication, and the service provides the same functionality as official APIs. However, always review any third-party service's terms of service and privacy policy before use."}, {"question": "Can I switch back to the official Anthropic API later?", "answer": "Yes, you can easily switch back by updating your environment variables. Simply set ANTHROPIC_BASE_URL to the official Anthropic endpoint and use your official Anthropic API key. The configuration change takes effect immediately without requiring any Claude Code reinstallation."}]} />

Conclusion

Setting up Claude Code with your own API key through derouter.ai provides substantial cost savings while maintaining full functionality. This claude code setup process takes just a few minutes but can reduce your AI coding costs by up to 77%.

The pay-per-use model with no subscription fees makes it perfect for developers who want flexible, affordable access to frontier AI models. With proper configuration, you'll have seamless access to Claude's powerful coding capabilities at a fraction of the official cost.

Remember to monitor your usage through the derouter.ai dashboard and choose the appropriate model for each task to optimize your costs further.

Ready to Get Started?

Access Claude, GPT, and more at a fraction of the cost.

Create Free Account

Related Articles