Accessing Claude via OpenRouter | Generated by AI

Home 2025.08

How to Use Claude Models with OpenRouter (As a Chinese User Without Anthropic API Access)

As a Chinese user, you may face restrictions accessing Anthropic’s Claude API directly due to regional blocks. However, OpenRouter is a reliable alternative that acts as a unified API gateway to multiple AI providers, including Anthropic’s Claude models. OpenRouter is accessible in China (both the website and API endpoints are not blocked), allowing you to route requests to Claude without needing a direct Anthropic account or API key. It’s pay-per-use (you’ll need to add a payment method), but signing up is free, and it supports a free tier for limited usage.

OpenRouter’s API is compatible with OpenAI’s format, so you can use familiar libraries like the OpenAI Python SDK. Below, I’ll outline the steps to get started and provide code examples for using Claude in Python.

Step 1: Sign Up for OpenRouter

  1. Visit the OpenRouter website: https://openrouter.ai.
  2. Click on “Sign Up” or “Get Started” (usually in the top right).
  3. Create an account using your email (or GitHub/Google login if available). No VPN is needed, as the site works in China.
  4. After signing up, verify your email if required.
  5. Go to the dashboard and add a payment method (e.g., credit card) to fund your account. OpenRouter charges based on token usage, but you can start with a small deposit. Check their pricing page for details on Claude models.

Step 2: Generate an API Key

  1. In your OpenRouter dashboard, navigate to the “API Keys” or “Keys” section.
  2. Create a new API key (it will look like a long string, e.g., sk-or-v1-...).
  3. Copy and save it securely—treat it like a password. You’ll use this in your code instead of an Anthropic key.

Step 3: Choose a Claude Model

OpenRouter lists Anthropic’s Claude models with IDs like:

You can browse the models page to see costs, context limits, and availability.

Step 4: Set Up Your Environment

Step 5: Use Claude in Code

Use the OpenAI SDK with OpenRouter’s base URL (https://openrouter.ai/api/v1). Specify the Claude model ID in your requests.

Here’s a simple Python example to chat with Claude 3.5 Sonnet:

from openai import OpenAI

# Initialize the client with OpenRouter's endpoint and your API key
client = OpenAI(
    base_url="https://openrouter.ai/api/v1",
    api_key="YOUR_OPENROUTER_API_KEY_HERE",  # Replace with your actual key
)

# Make a request to Claude
completion = client.chat.completions.create(
    model="anthropic/claude-3.5-sonnet",  # Use the Claude model ID
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Hello, what is the capital of China?"}
    ],
    temperature=0.7,  # Optional: Adjust for creativity (0-1)
    max_tokens=150    # Optional: Limit response length
)

# Print the response
print(completion.choices[0].message.content)
import requests
import json

response = requests.post(
    url="https://openrouter.ai/api/v1/chat/completions",
    headers={
        "Authorization": "Bearer YOUR_OPENROUTER_API_KEY_HERE",
        "Content-Type": "application/json"
    },
    data=json.dumps({
        "model": "anthropic/claude-3.5-sonnet",
        "messages": [
            {"role": "system", "content": "You are a helpful assistant."},
            {"role": "user", "content": "Hello, what is the capital of China?"}
        ]
    })
)

# Parse and print the response
data = response.json()
print(data['choices'][0]['message']['content'])

Troubleshooting

This setup lets you use Claude programmatically without direct Anthropic access. If you need integration with specific tools (e.g., Cursor IDE or Make.com), search for tutorials on those.

References:
OpenRouter Documentation
Anthropic Claude on OpenRouter
Using OpenRouter in China
How to Use Claude in China


Back Donate