How to Get and Configure a Claude API Key

How to Get and Configure Your Claude API Key

An API key is your authentication credential for accessing the Claude API. Whether you are using Anthropic's direct API or a relay service like claude4u.com, proper key setup is the first step toward building with Claude. This guide walks you through obtaining, configuring, and securing your API key.

Option 1: Getting a Key from Anthropic Console

To get a direct Anthropic API key, follow these steps:

  1. Visit console.anthropic.com and create an account or sign in.
  2. Navigate to Settings > API Keys in the left sidebar.
  3. Click Create Key and give it a descriptive name (e.g., "production-backend" or "dev-testing").
  4. Copy the key immediately. Anthropic only shows it once. It starts with sk-ant-.
  5. Add a payment method under Billing to activate your key for production use.
Warning: Store your API key securely the moment you create it. Anthropic will not display the full key again. If you lose it, you must generate a new one.

Option 2: Getting a Key from a Relay Service

Relay services like claude4u.com issue their own API keys that work as drop-in replacements. Relay keys typically use a custom prefix (e.g., cr_) and provide additional benefits including multi-account pooling and usage tracking.

To obtain a relay API key from claude4u.com:

  1. Register at claude4u.com and access the admin dashboard.
  2. Navigate to the API Key management section.
  3. Generate a new key. Your key will start with cr_.
  4. Configure permissions, model access, and rate limits for the key.

Setting Up Environment Variables

The recommended way to configure your API key is through environment variables. This keeps credentials out of your source code.

For Direct Anthropic API

# Add to your shell profile (~/.bashrc, ~/.zshrc, etc.)
export ANTHROPIC_API_KEY="sk-ant-your-key-here"

For Relay Service (claude4u.com)

# Set both the base URL and relay API key
export ANTHROPIC_BASE_URL="https://claude4u.com/antigravity"
export ANTHROPIC_API_KEY="cr_your_relay_key_here"

After adding these lines, reload your shell:

# Reload your shell configuration
source ~/.zshrc   # or source ~/.bashrc

Configuration for Different Platforms

macOS / Linux

# Temporary (current session only)
export ANTHROPIC_API_KEY="your-key-here"

# Permanent (add to shell profile)
echo 'export ANTHROPIC_API_KEY="your-key-here"' >> ~/.zshrc

Windows (PowerShell)

# Temporary
$env:ANTHROPIC_API_KEY = "your-key-here"

# Permanent (user-level)
[System.Environment]::SetEnvironmentVariable("ANTHROPIC_API_KEY", "your-key-here", "User")

Docker / Docker Compose

# docker-compose.yml
services:
  app:
    environment:
      - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY}
      - ANTHROPIC_BASE_URL=https://claude4u.com/antigravity

.env File (for development)

# .env file (add .env to .gitignore!)
ANTHROPIC_API_KEY=cr_your_relay_key_here
ANTHROPIC_BASE_URL=https://claude4u.com/antigravity
Tip: Always add .env to your .gitignore file to prevent accidentally committing credentials to version control.

Using the Key in Code

Python

import anthropic

# Reads ANTHROPIC_API_KEY and ANTHROPIC_BASE_URL from environment
client = anthropic.Anthropic()

# Or set explicitly
client = anthropic.Anthropic(
    api_key="cr_your_relay_key_here",
    base_url="https://claude4u.com/antigravity"
)

Node.js / TypeScript

import Anthropic from '@anthropic-ai/sdk';

// Reads from environment variables automatically
const client = new Anthropic();

// Or set explicitly
const client = new Anthropic({
  apiKey: 'cr_your_relay_key_here',
  baseURL: 'https://claude4u.com/antigravity',
});

Verifying Your Key

Test that your API key is working correctly with a simple curl command:

curl -s https://claude4u.com/antigravity/v1/messages \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "content-type: application/json" \
  -H "anthropic-version: 2023-06-01" \
  -d '{"model":"claude-sonnet-4-20250514","max_tokens":50,"messages":[{"role":"user","content":"Say hello"}]}' \
  | python3 -m json.tool

API Key Security Best Practices

Warning: If you suspect your API key has been compromised, revoke it immediately from your dashboard and generate a new one. Check your usage logs for any unauthorized requests.

With your API key properly configured, you are ready to start building with Claude. Check out our guides on Claude Code setup, streaming API implementation, and prompt engineering to get the most out of your integration.

Get Started with 轻舟 AI

Stable, fast AI API relay — supports Claude, OpenAI, Gemini and more

Sign Up Free