Tutorial

Claude Code API Key: Complete Guide to Setup and Configuration

2026-04-09 7

Getting Started with Claude Code: API Key Setup

Claude Code requires an API key to function. Whether you use Anthropic directly or a compatible provider, this guide walks you through every step from zero to a working setup.

Step 1: Install Claude Code

If you have not installed Claude Code yet:

# Install globally via npm
npm install -g @anthropic-ai/claude-code

# Verify installation
claude --version

Requirements: Node.js 18+ and npm. Claude Code runs on macOS, Linux, and Windows (via WSL or native).

Step 2: Choose Your API Provider

You have two options for powering Claude Code:

Option A: Anthropic Direct

  • Cost: $3/million input tokens, $15/million output tokens (Sonnet 4.7)
  • Quality: Best available
  • Setup: Get key from console.anthropic.com
  • Cost: lower than going direct
  • Quality: Very good (87%+ of Claude quality)
  • Setup: Get key from provider (e.g., llmapi.pro)

Step 3: Get Your API Key

For Anthropic Direct:

  1. Go to console.anthropic.com
  2. Sign in or create an account
  3. Navigate to API Keys in the left sidebar
  4. Click Create Key
  5. Name it (e.g., "claude-code") and copy the key
  6. Add billing information (minimum $5 credit required)

Your key will look like: sk-ant-api03-xxxxx...

For LLM API:

  1. Go to llmapi.pro
  2. Create an account (free tier available, no credit card required)
  3. Your API key is generated automatically on the dashboard
  4. Copy the key

Your key will look like: sk-relay-xxxxx...

Step 4: Configure Environment Variables

macOS / Linux (bash/zsh)

Add to your shell profile (~/.bashrc, ~/.zshrc, or ~/.bash_profile):

# For Anthropic Direct:
export ANTHROPIC_API_KEY="sk-ant-api03-your-key-here"

# For LLM API (add both lines):
export ANTHROPIC_BASE_URL="https://llmapi.pro"
export ANTHROPIC_API_KEY="sk-relay-your-key-here"

Then reload your shell:

source ~/.zshrc  # or ~/.bashrc

Windows PowerShell

# For Anthropic Direct:
$env:ANTHROPIC_API_KEY = "sk-ant-api03-your-key-here"

# For LLM API:
$env:ANTHROPIC_BASE_URL = "https://llmapi.pro"
$env:ANTHROPIC_API_KEY = "sk-relay-your-key-here"

To persist across sessions, add to your PowerShell profile ($PROFILE) or set as system environment variables.

Windows CMD

set ANTHROPIC_API_KEY=sk-ant-api03-your-key-here
set ANTHROPIC_BASE_URL=https://llmapi.pro

Step 5: Verify Your Setup

# Start Claude Code
claude

# You should see the Claude Code prompt
# Try a simple command:
> What files are in the current directory?

If Claude Code starts and responds, your API key is configured correctly.

Troubleshooting Common Errors

"Invalid API key" or "Authentication failed"

  • Double-check you copied the full key (no trailing spaces)
  • Verify the key has not been revoked in your provider's dashboard
  • Make sure you are using ANTHROPIC_API_KEY, not CLAUDE_API_KEY or other variations

"Could not connect to API"

  • If using a custom ANTHROPIC_BASE_URL, verify the URL is correct (no trailing slash)
  • Check your internet connection
  • Try curl https://llmapi.pro to verify the endpoint is reachable

"Insufficient credits" or "Payment required"

  • Anthropic requires prepaid credits. Add billing at console.anthropic.com
  • LLM API free tier has limited usage. Check your dashboard for remaining quota

"Model not found"

  • This usually means your API key does not have access to the requested model
  • On Anthropic: ensure your account has access to Claude Sonnet 4.7
  • On LLM API: all models are available by default, contact support if this occurs

Advanced Configuration

Per-Project Configuration

Create a .claude directory in your project root with a settings.json:

{
  "apiKey": "sk-relay-project-specific-key",
  "baseUrl": "https://llmapi.pro"
}

Multiple Profiles

Use shell aliases to switch between configurations:

# Add to ~/.bashrc or ~/.zshrc
alias claude-pro='ANTHROPIC_BASE_URL="" ANTHROPIC_API_KEY="sk-ant-your-key" claude'
alias claude-cheap='ANTHROPIC_BASE_URL="https://llmapi.pro" ANTHROPIC_API_KEY="sk-relay-your-key" claude'

CI/CD Integration

For automated environments, set the environment variables in your CI configuration:

# GitHub Actions example
env:
  ANTHROPIC_BASE_URL: https://llmapi.pro
  ANTHROPIC_API_KEY: ${{ secrets.LLM_API_KEY }}

Next Steps

With your API key configured, you are ready to use Claude Code. Here are some things to try:

  • Explore a codebase: claude "explain the architecture of this project"
  • Fix a bug: claude "the login page throws a 500 error, find and fix it"
  • Add a feature: claude "add pagination to the /users endpoint"
  • Run tests: claude "run the test suite and fix any failures"

If you chose LLM API as your provider, you are already reducing your spend versus going direct — with the same Claude Code CLI experience.

分享这篇文章

开始使用 LLM API

免费套餐可用。Claude Code 一行配置。

免费开始