Claude Code Custom API Key Setup: Use Any Endpoint in 2026

Claude Code is Anthropic's terminal-based coding agent, and it's genuinely one of the best tools for working with code right now. It reads your entire repo, makes multi-file edits, runs tests, and iterates until things work. If you haven't tried it, you're missing out.

But here's the thing: by default, Claude Code talks directly to Anthropic's API. That means you need an Anthropic account, a supported credit card, and you're paying full retail pricing. For developers in regions where Anthropic doesn't accept payments — or anyone who wants more flexibility — that's a problem.

The fix is simple. Claude Code supports custom API endpoints through environment variables. You can point it at any compatible provider, use your own API key, and get the same experience at potentially lower cost. This guide walks through exactly how to do it.

Why Use a Custom Endpoint?

There are a few solid reasons to run Claude Code through a third-party API provider instead of going direct:

The Two Environment Variables You Need

Claude Code reads two environment variables for API configuration:

ANTHROPIC_BASE_URL   # The API endpoint URL
ANTHROPIC_API_KEY    # Your API key

That's it. Set these, and Claude Code will send all requests to your chosen endpoint instead of api.anthropic.com.

Quick Setup (macOS/Linux)

For a one-time session, just export them before launching:

# Set your custom endpoint
export ANTHROPIC_BASE_URL=https://api.kissapi.ai
export ANTHROPIC_API_KEY=sk-your-api-key-here

# Launch Claude Code
claude

Claude Code starts up, connects to your endpoint, and everything works exactly as before. No config files to edit, no settings to change.

Permanent Setup (Shell Profile)

If you want this to persist across terminal sessions, add the exports to your shell profile:

# For zsh (default on macOS)
echo 'export ANTHROPIC_BASE_URL=https://api.kissapi.ai' >> ~/.zshrc
echo 'export ANTHROPIC_API_KEY=sk-your-api-key-here' >> ~/.zshrc
source ~/.zshrc

# For bash
echo 'export ANTHROPIC_BASE_URL=https://api.kissapi.ai' >> ~/.bashrc
echo 'export ANTHROPIC_API_KEY=sk-your-api-key-here' >> ~/.bashrc
source ~/.bashrc

Now every time you open a terminal and type claude, it'll use your custom endpoint automatically.

Per-Project Setup with .env

If you want different endpoints for different projects, you can use a .env file in your project root:

# .env
ANTHROPIC_BASE_URL=https://api.kissapi.ai
ANTHROPIC_API_KEY=sk-your-project-key

Just make sure to add .env to your .gitignore so you don't accidentally commit your API key. Seriously, do this. The recent CVE-2026-21852 vulnerability showed how attackers can exploit project-level config files to exfiltrate API keys — don't make it easy for them.

Security Note: The ANTHROPIC_BASE_URL Vulnerability

In January 2026, researchers discovered that Claude Code versions before 2.0.65 had a vulnerability (CVE-2026-21852) where a malicious .claude/settings.json file in a cloned repo could redirect API requests to an attacker-controlled server, leaking your API key.

This has been patched. But the lesson is clear:

Choosing the Right Model

When using Claude Code through a custom endpoint, you can typically choose which model to use. Claude Code defaults to the latest Sonnet, but you can override this:

# Use Opus for complex refactoring
claude --model claude-opus-4-6

# Use Sonnet for everyday coding (default)
claude --model claude-sonnet-4-6

# Use Haiku for quick questions
claude --model claude-haiku-4-5

Here's my honest take on when to use each:

ModelBest ForCost (per 1M tokens in/out)
Opus 4.6Large refactors, complex debugging, architecture decisions$15 / $75
Sonnet 4.6Daily coding, feature implementation, code review$3 / $15
Haiku 4.5Quick lookups, simple edits, test generation$0.80 / $4

Sonnet 4.6 is the default for a reason. It handles 95% of coding tasks well, and the cost difference vs Opus is significant. I'd only switch to Opus when you're doing something that requires deep reasoning across many files — like redesigning a module's architecture or debugging a gnarly concurrency issue.

Verifying Your Setup

After configuring your custom endpoint, here's how to verify everything works:

# Check your environment variables are set
echo $ANTHROPIC_BASE_URL
echo $ANTHROPIC_API_KEY

# Test the endpoint with curl
curl -s $ANTHROPIC_BASE_URL/v1/models \
  -H "Authorization: Bearer $ANTHROPIC_API_KEY" | head -20

# Launch Claude Code and try a simple task
claude "What files are in this directory?"

If the curl command returns a list of models, your endpoint is working. If Claude Code launches and responds, you're all set.

Troubleshooting Common Issues

Claude Code ignores my ANTHROPIC_BASE_URL

Make sure you're exporting the variable, not just setting it. ANTHROPIC_BASE_URL=... without export won't be visible to child processes. Also check that you don't have a conflicting value in ~/.claude/settings.json.

Authentication errors (401/403)

Your API key format might not match what the provider expects. Some providers use sk- prefixed keys, others use different formats. Double-check with your provider's docs.

Model not found errors

Different providers use different model names. If claude-sonnet-4-6 doesn't work, try claude-sonnet-4.6 or check the provider's model list. Most gateways like KissAPI document their exact model identifiers.

Slow responses or timeouts

This usually means the provider is routing to a congested upstream. Try a different model or check the provider's status page. If you're behind a corporate proxy, set HTTPS_PROXY as well.

Using Claude Code with OpenAI-Compatible Endpoints

Some developers want to use Claude Code's interface but with non-Anthropic models (like GPT-5 or Gemini). Claude Code is specifically built for Claude models and uses the Anthropic API format, not the OpenAI format. So you can't just point it at any OpenAI-compatible endpoint.

However, some API gateways support the Anthropic API format alongside OpenAI format. In that case, you can use them with Claude Code as long as the gateway translates the request to the right upstream provider.

If you specifically want an OpenAI-compatible coding agent, look at tools like Aider or Continue.dev, which support the OpenAI API format natively.

Cost Optimization Tips

Claude Code can burn through tokens fast, especially on large codebases. Here are some practical ways to keep costs down:

  1. Use /compact regularly. This command compresses the conversation history, reducing the context size and saving tokens on subsequent messages.
  2. Be specific in your prompts. "Fix the auth bug in src/auth/login.ts" costs way less than "something's broken with login, can you look at it?" because Claude won't need to scan your entire codebase.
  3. Set a token budget. Some providers let you set spending limits per API key. Use this to avoid surprise bills during long coding sessions.
  4. Start new conversations for new tasks. Don't let context accumulate across unrelated tasks. Each message in a long conversation sends the entire history.
  5. Use Haiku for exploration. When you're just asking "what does this function do?" or "where is X defined?", Haiku is fast and cheap.

Get Claude Code Running in 2 Minutes

Sign up for KissAPI, grab your API key, and set two environment variables. That's it. Access Claude Opus 4.6, Sonnet 4.6, and more — no Anthropic account needed.

Start Free →

Wrapping Up

Setting up Claude Code with a custom API endpoint takes about 30 seconds. Two environment variables, and you're done. The benefits — regional access, cost flexibility, team billing, redundancy — make it worth doing even if you already have an Anthropic account.

The key things to remember: always use the latest Claude Code version for security, set your env vars at the shell level (not in project files from untrusted repos), and pick the right model for the job. Sonnet for daily work, Opus for the hard stuff, Haiku for quick questions.

Happy coding.