Quick Answer
You can get a Claude API key through two paths in 2026: directly from Anthropic's console (requires an international credit card and phone verification) or through a unified gateway like TeamoRouter (supports Alipay/WeChat Pay, no overseas payment needed). Once you have a key, set it via the ANTHROPIC_API_KEY environment variable, configure it in your Claude Code settings, and enable prompt caching to keep costs under control. This tutorial walks you through every step.
Why Your API Key Setup Matters
Your API key is the single credential that authorizes every request you make to Claude. How you obtain it and how you configure it directly impacts:
- Accessibility: Can you get a key from your location and payment method?
- Cost: Does your provider maintain prompt caching, or does a broken cache silently 10x your bill?
- Security: Is your key stored safely, or is it hardcoded in a public repo?
- Flexibility: Does your key work across Claude Code, your own scripts, and third-party tools?
A well-configured API key setup saves you money, prevents security incidents, and gives you reliable access. A poorly configured one does the opposite. Here is how to get it right.
Path 1: Getting a Key Directly from Anthropic
This is the official path. It works anywhere Anthropic operates, but it requires an international payment method and may be inaccessible from some regions without a VPN.
Step-by-Step
-
Create an Anthropic account. Go to console.anthropic.com and sign up with your email. You will need to verify your phone number.
-
Navigate to API Keys. Once logged in, click "API Keys" in the left sidebar, then click "Create Key."
-
Name your key. Give it a descriptive name like
my-dev-machineorproduction-server-1. This helps you identify and revoke keys later. -
Copy the key immediately. Anthropic only shows the full key once. Copy it and store it somewhere safe. It begins with
sk-ant-. -
Add a payment method. Go to Billing > Payment Methods and add an international credit card. As of 2026, Anthropic accepts Visa, Mastercard, and American Express. Many China-issued cards are not accepted.
-
Top up your balance. Anthropic uses a pre-paid billing model. Add funds to your account before you can make API calls.
Limitations of the Direct Path
- Requires an international credit card.
- Console access may be slow or blocked from mainland China without a VPN.
- The API endpoint (
api.anthropic.com) may be unreliable from China. - You only get Claude models; no access to GPT, Gemini, or other providers.
Path 2: Getting a Key via TeamoRouter (Recommended for Most Users)
TeamoRouter is a unified LLM gateway that gives you one API key for Claude, GPT, Gemini, DeepSeek, and more. It accepts Alipay, WeChat Pay, and bank transfers, and its endpoints are directly accessible from China. For more on the gateway features, see TeamoRouter's AI API gateway.
Step-by-Step
-
Create a TeamoRouter account. Go to teamorouter.com and sign up with your email or phone number.
-
Navigate to the API Key section. After logging in, find "API Keys" in the dashboard sidebar.
-
Generate a new key. Click "Create Key," give it a name, and copy the key. TeamoRouter keys begin with
tr-. -
Top up your balance. Go to Billing and add funds via Alipay, WeChat Pay, or bank transfer. No international card needed.
-
Note your Base URL. Your dashboard shows your personal API endpoint, typically
https://api.teamorouter.com/anthropicfor the Anthropic-compatible interface. -
Start using it. Your TeamoRouter key works as a drop-in replacement for an Anthropic API key. Just change the
base_urlandapi_keyin your client.
Setting Environment Variables
Once you have your API key, the standard way to make it available to your tools is through environment variables.
Linux / macOS
Add these lines to your shell configuration file (~/.zshrc, ~/.bashrc, or ~/.bash_profile):
# Direct Anthropic setup
export ANTHROPIC_API_KEY="sk-ant-your-key-here"
# TeamoRouter setup (recommended)
export ANTHROPIC_BASE_URL="https://api.teamorouter.com/anthropic"
export ANTHROPIC_API_KEY="tr-your-key-here"
After editing, reload your shell:
source ~/.zshrc # or ~/.bashrc
Windows (PowerShell)
# Set for current session
$env:ANTHROPIC_API_KEY = "tr-your-key-here"
# Set permanently (run as administrator)
[System.Environment]::SetEnvironmentVariable('ANTHROPIC_API_KEY', 'tr-your-key-here', 'User')
Windows (Command Prompt)
setx ANTHROPIC_API_KEY "tr-your-key-here"
Using a .env File
For project-specific keys, create a .env file in your project root:
# .env
ANTHROPIC_BASE_URL="https://api.teamorouter.com/anthropic"
ANTHROPIC_API_KEY="tr-your-key-here"
Then load it in your application. Python example:
from dotenv import load_dotenv
import os
load_dotenv()
api_key = os.getenv("ANTHROPIC_API_KEY")
Always add .env to your .gitignore:
echo ".env" >> .gitignore
Configuring Claude Code with Your API Key
Claude Code reads the ANTHROPIC_API_KEY and ANTHROPIC_BASE_URL environment variables automatically. Here is the setup for different configurations:
Direct Anthropic Setup
export ANTHROPIC_API_KEY="sk-ant-your-key-here"
claude
TeamoRouter Setup (One Key for Claude Code + Codex + More)
export ANTHROPIC_BASE_URL="https://api.teamorouter.com/anthropic"
export ANTHROPIC_API_KEY="tr-your-key-here"
claude
Using Claude Code's Settings File
You can also configure Claude Code via its JSON settings file at ~/.claude/settings.json:
{
"apiKey": "tr-your-key-here",
"baseURL": "https://api.teamorouter.com/anthropic",
"model": "claude-sonnet-4-20250514"
}
The advantage of using the settings file is that it is specific to Claude Code and does not leak into other tools that also read ANTHROPIC_API_KEY.
Verifying Your Configuration
Run a quick test to confirm everything works:
claude -p "Say hello and confirm you are running as Claude."
If Claude Code responds normally, your API key is configured correctly. If you get an authentication error, double-check that your key is correctly set and your base URL is reachable.
Security Best Practices
An API key is a bearer token: anyone who has it can use your account and spend your balance. These practices keep your key safe.
1. Never Hardcode Keys
Do not put API keys directly in source code. A single git push to a public repository exposes your key to the world.
# Bad: hardcoded key
client = anthropic.Anthropic(api_key="sk-ant-abc123")
# Good: read from environment
client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
2. Use Per-Project Keys
If you use TeamoRouter, create separate API keys for different projects. This limits the blast radius if a key is compromised. TeamoRouter's dashboard lets you create unlimited keys and set per-key spending limits.
3. Rotate Keys Regularly
Regenerate your API keys every 30-90 days. When you rotate, create the new key first, update your configuration, verify it works, and then revoke the old key.
4. Set Spending Limits
Both Anthropic and TeamoRouter let you set spending caps. Configure a monthly limit that matches your budget to prevent surprise bills. TeamoRouter also supports per-key limits, so you can cap a dev key at $50/month and a production key at $500/month.
5. Use Environment Variables or a Secrets Manager
For production applications, use a proper secrets manager (AWS Secrets Manager, HashiCorp Vault, GitHub Secrets, or your cloud provider's equivalent) rather than environment variables, which can be leaked through process listings or error logs.
6. Monitor Usage
Check your API usage dashboard regularly. Unusual spikes may indicate a leaked key or a misconfigured agent stuck in a loop. TeamoRouter's dashboard provides real-time usage graphs with per-model and per-key breakdowns.
Key Configuration Comparison Table
| Aspect | Direct Anthropic | TeamoRouter |
|---|---|---|
| Payment methods | International credit card | Alipay, WeChat, bank transfer |
| China accessibility | VPN required | Direct access, no VPN |
| Key prefix | sk-ant- |
tr- |
| Models accessible | Claude only | Claude, GPT, Gemini, DeepSeek, more |
| Per-key spending limits | No | Yes |
| Prompt caching support | Yes | Yes (>99% hit rate) |
| Pricing | Official list price | 10-20% of official (floating rate) |
| Key generation | Console only | Dashboard + API |
| Billing currency | USD (card-dependent) | CNY, USD |
Setting Up for Multiple Tools with One Key
One of the biggest advantages of using TeamoRouter is that a single API key works across multiple tools. Here is how to configure each:
# Shared key
export TR_KEY="tr-your-key-here"
# Claude Code (Anthropic protocol)
export ANTHROPIC_BASE_URL="https://api.teamorouter.com/anthropic"
export ANTHROPIC_API_KEY="$TR_KEY"
# Codex (OpenAI protocol)
export OPENAI_BASE_URL="https://api.teamorouter.com/openai"
export OPENAI_API_KEY="$TR_KEY"
# Gemini CLI (Google protocol)
export GOOGLE_API_KEY="$TR_KEY"
# (configure base URL in Gemini CLI settings)
# General OpenAI-compatible tools
export OPENAI_API_KEY="$TR_KEY"
export OPENAI_BASE_URL="https://api.teamorouter.com/openai"
This means you maintain one key, one balance, and one billing dashboard for all your AI tools.
FAQ
How do I get a Claude API key if I am in China?
The easiest path is through TeamoRouter. Sign up at teamorouter.com, top up via Alipay or WeChat Pay, and generate your key. You get immediate access without an international credit card or VPN. If you prefer the direct Anthropic path, you will need a VPN to access console.anthropic.com and an international credit card for payment.
Can I use the same API key for Claude Code and my own Python scripts?
Yes. An API key works across all clients and tools. If you are using TeamoRouter, the same key also works for GPT, Gemini, and other models. Set the key once as an environment variable, and all your tools read it automatically.
What happens if my API key is leaked?
Immediately log into your dashboard (Anthropic console or TeamoRouter) and revoke the compromised key. Generate a new key and update your configuration. If the key was in a public GitHub repository, treat it as compromised even if you delete the commit -- bots scan public repos for API keys within seconds. Rotate the key and review your usage history for any unauthorized charges.
How do I configure Claude Code to use a custom API endpoint?
Set the ANTHROPIC_BASE_URL environment variable to your custom endpoint, or add a baseURL field in ~/.claude/settings.json. This is how you point Claude Code at TeamoRouter or any other Anthropic-compatible gateway.
Is it safe to store my API key in a .env file?
Yes, as long as you add .env to .gitignore and never commit it to version control. For production applications, use a secrets manager instead. For local development, .env files are the standard, widely-accepted practice.
How often should I rotate my API key?
Rotate every 30-90 days as a baseline. Rotate immediately if you suspect a leak, if a team member who had access leaves, or if you accidentally exposed it (e.g., in a screenshot or screen share).
Can I set different spending limits for different API keys?
Yes, if you use TeamoRouter. The dashboard lets you create multiple keys and set a monthly spending cap on each one individually. Anthropic's console does not currently offer per-key spending limits; limits apply at the account level.