---
title: "ANTHROPIC_AUTH_TOKEN vs ANTHROPIC_API_KEY: Which to Use (2026)"
description: "The core difference is the header: API_KEY sends x-api-key, AUTH_TOKEN sends Authorization: Bearer. When to use each, what conflicts, and a 5-step checklist."
url: https://cdnping.org/en/blog/anthropic-auth-token-vs-api-key
language: en
published: 2026-11-27
category: "Developer tools"
tags: ["Claude Code","Environment variables","Anthropic API","Base URL","Authentication"]
source: cdnping.org
license: Free to quote and summarize with a link to the url above.
---
# ANTHROPIC_AUTH_TOKEN vs ANTHROPIC_API_KEY: Which to Use (2026)

The main difference between ANTHROPIC_AUTH_TOKEN and ANTHROPIC_API_KEY is which request header carries the credential. ANTHROPIC_API_KEY maps to `x-api-key`, the standard form for Anthropic's official API. ANTHROPIC_AUTH_TOKEN maps to `Authorization: Bearer <value>` and is commonly used when requests go through a gateway or proxy. Most people only need one of the two, and setting both tends to cause problems.

This article is based on what Claude Code and the Anthropic docs commonly say. Behavior may change between versions, so treat the official documentation as the authority.

## What does each variable do?

| Variable | Header sent | Typical use |
| --- | --- | --- |
| ANTHROPIC_API_KEY | `x-api-key: <value>` | Calling the Anthropic API directly with a key created in the Console |
| ANTHROPIC_AUTH_TOKEN | `Authorization: Bearer <value>` | A gateway, proxy, or internal forwarding layer that requires a Bearer token |

Anthropic's official API takes keys in `x-api-key`, and requests also need an `anthropic-version` header. This differs from OpenAI-style `Authorization: Bearer`. For a field-by-field comparison, see [How the Anthropic and OpenAI API formats differ](/en/blog/anthropic-vs-openai-api-format).

So "token" versus "key" does not imply a different format. Which variable you put the same string in decides which header it goes into, not what the string looks like.

## Which one should I set?

Choose based on what you are connecting to:

- Calling the official API with a key from the Console: set ANTHROPIC_API_KEY. For how to get one, see [How to get a Claude API key](/en/blog/how-to-get-claude-api-key).
- Connecting to a third-party or self-hosted gateway whose docs say "Authorization: Bearer": set ANTHROPIC_AUTH_TOKEN, and point ANTHROPIC_BASE_URL at the gateway address.
- The provider's docs only say "API key" and don't name a header: before trying both, look at the curl example in their docs, see which header it uses, and match it.

ANTHROPIC_BASE_URL changes where requests are sent. It is independent of which credential variable you use. For the full walkthrough on configuring endpoints, see [Custom API endpoints for Claude Code, Codex, and OpenCode](/en/blog/configure-custom-api-endpoint-claude-code-codex).

## What happens if I set both?

When both variables are present, which one Claude Code picks and which headers go out depend on the version and on your login state. I can't state a fixed rule with confidence, so check the official docs. In practice there are two kinds of risk:

1. The gateway only accepts one header, and the other header carries an unrelated value, so the gateway rejects the request or attributes it to the wrong identity.
2. You think you're going through the gateway, but the request is actually sent elsewhere based on the other variable, or it uses a subscription login instead of a key.

Also, if Claude Code is already logged in to a subscription account, setting a key variable changes how you are billed. For the tradeoffs between the two billing modes, see [Claude Code: subscription login or API key](/en/blog/claude-code-subscription-or-api-key). For switching steps, see [Logging in to Claude Code with an API key vs a subscription, and how to switch](/en/blog/claude-code-api-key-login).

## How do I configure and verify it? A 5-step checklist

1. Find out which header the target expects. Check the curl example in the provider's docs: is it `x-api-key` or `Authorization: Bearer`?
2. Set only the matching variable. For a quick test, run this in the current terminal:

```bash
# Pick one, don't set both
export ANTHROPIC_API_KEY="your key"
# or
export ANTHROPIC_AUTH_TOKEN="your token"

# Add this when going through a gateway
export ANTHROPIC_BASE_URL="https://gateway-address"
```

3. Clear the one you aren't using so no stale value lingers:

```bash
unset ANTHROPIC_API_KEY      # if you use AUTH_TOKEN
unset ANTHROPIC_AUTH_TOKEN   # if you use API_KEY
```

4. Verify the credential and address on their own with curl, to rule out Claude Code itself. Swap in whichever header you are testing:

```bash
curl -s "$ANTHROPIC_BASE_URL/v1/messages" \
  -H "x-api-key: $ANTHROPIC_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{"model":"a model you have access to","max_tokens":16,"messages":[{"role":"user","content":"ping"}]}'
```

To test Bearer, replace `-H "x-api-key: ..."` with `-H "Authorization: Bearer $ANTHROPIC_AUTH_TOKEN"`. The path and required fields follow your provider's documentation.

5. To make it permanent, put it in the `env` field of Claude Code's settings.json so you don't have to export it every time you open a terminal. For levels and precedence, see [settings.json levels, precedence, and env configuration](/en/blog/claude-code-settings-json-guide).

## I got an error. How do I tell which part is failing?

- 401: the credential or header is wrong. First confirm you used the right variable, then check that the key has no stray spaces or newlines.
- 403 or an insufficient-permissions message: the credential is valid but has no access to that model or organization. Check the corresponding console or gateway.
- Connection timeout or DNS error: most likely ANTHROPIC_BASE_URL is wrong or your local network is down. It has nothing to do with the credential.
- If the official API itself is having problems, check cdnping's [Anthropic API status](/en/status/anthropic) and [Is Claude Code down](/en/down/claude-code) to rule out a server-side issue before changing your config.

## No overseas credit card or Claude account?

If all you want is to get Claude Code running and you don't have an overseas payment method, AGI Plan is one option. It is a multi-provider LLM aggregation gateway with monthly subscriptions, paid in RMB (Chinese yuan), with no overseas credit card needed. Claude Code only needs one endpoint change, no Claude account is required, and it is compatible with both the OpenAI and Anthropic API formats. Note that it is not an officially authorized channel, and some models are available only outside China. See [agiplan.dev/models](https://agiplan.dev/models) for details. AGI Plan is a commercial partner of cdnping.

Setup steps and plans are at [agiplan.dev/startup](https://agiplan.dev/startup). Which credential variable such a gateway uses, and which header it needs, is defined by its own tutorial. The checklist in this article still applies.

## Summary and next steps

First check which header the target service requires, then set only the matching variable, and hand it to Claude Code once curl succeeds. Next, read [How to switch models in Claude Code](/en/blog/claude-code-switch-model), or go back to the [Claude Code commands cheat sheet](/en/blog/claude-code-commands-cheatsheet).
