Skip to content
Nilay Kabariya

Entry No.024·AI··2 min read

Fix: Could not resolve authentication method (Anthropic / Claude SDK)

Anthropic's SDK can't find your key. The old and new wording, ANTHROPIC_API_KEY vs ANTHROPIC_AUTH_TOKEN, and the 401 you get when a key is in the wrong one.

by Nilay#anthropic#node#pythonAI

The error, verbatim

Jump to the fix ↓
Error: Could not resolve authentication method. Expected either apiKey or authToken to be set.
Or for one of the "X-Api-Key" or "Authorization" headers to be explicitly omitted

# newer versions of the SDK:
Error: Could not resolve authentication method. Expected one of apiKey, authToken, credentials,
config, or profile to be set. Or for one of the "X-Api-Key" or "Authorization" headers to be explicitly omitted
✓ Reproduced on Windows 11 Pro

Tested on

@anthropic-ai/sdk
0.30.1 and 0.128.0
anthropic (Python)
1.8.0
Runtime
Node 24.14.0, Python 3.14.5
OS
Windows 11 Pro
Contents
  1. Which wording you have01
  2. The fix02
  3. ANTHROPIC_API_KEY vs ANTHROPIC_AUTH_TOKEN03
  4. How this was tested04

The SDK looked for credentials and found none. It checks two environment variables, ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN, and they are not interchangeable, which is where most of the confusion around this error comes from.

Which wording you have

SDK Message
JavaScript, older (0.30) …Expected either apiKey or authToken to be set.
JavaScript, current (0.128) …Expected one of apiKey, authToken, credentials, config, or profile to be set.
Python (1.8) TypeError: "Could not resolve authentication method. Expected one of api_key, auth_token, or credentials to be set…"

Note that Python raises it as a TypeError, which is easy to misread as a bug in your own code.

The fix

Put your key from the Anthropic Console in ANTHROPIC_API_KEY:

ANTHROPIC_API_KEY=sk-ant-...

If it’s in a .env file, it has to be loaded before the client is created, exactly as with OpenAI’s SDK:

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

const client = new Anthropic();

If the key is in .env and you still see this error, nothing is loading the file, or it’s being loaded from the wrong folder. The causes and fixes are the same as for OpenAI’s version of this error: OPENAI_API_KEY is missing or empty.

ANTHROPIC_API_KEY vs ANTHROPIC_AUTH_TOKEN

The SDK sends them in two different ways. From its own source code:

Variable Sent as
ANTHROPIC_API_KEY X-Api-Key: <your key>
ANTHROPIC_AUTH_TOKEN Authorization: Bearer <your token>

A normal Console key (sk-ant-…) belongs in ANTHROPIC_API_KEY. The token variable is for setups that authenticate with a bearer token instead of a Console key.

If you’re not sure which variable yours landed in, the 401 tells you. With the same invented key in each variable, the API answered differently:

Where the key was 401 message
ANTHROPIC_API_KEY API key is invalid.
ANTHROPIC_AUTH_TOKEN invalid x-api-key

So if you see invalid x-api-key while you only set ANTHROPIC_AUTH_TOKEN, move the key to ANTHROPIC_API_KEY. If you see API key is invalid., the variable is right and the key itself is wrong or revoked.

How this was tested

@anthropic-ai/sdk 0.30.1 and 0.128.0 on Node 24.14.0, and the Python SDK 1.8.0 on Python 3.14.5, on Windows 11. Each wording was produced with no credentials set; the 401s came from the real API, with an invented key placed in each variable in turn. The header mapping was read from the SDK’s client.mjs. No real key was used.

— N.K., end of entry No.024

Useful? Pass it on:Post on XFollow @EmotionalMatter

Related entries

  1. No.045

    Fix: APIConnectionError "Connection error." (OpenAI, Anthropic)

    OpenAI's and Anthropic's SDKs both say only Connection error. Five real causes reproduced, from a local model that isn't running to antivirus HTTPS scanning.

    > APIConnectionError: Connection error.

    AI5 min
  2. No.023

    Fix: OpenAI 401 Incorrect API key provided (AuthenticationError)

    OpenAI rejects a key you just copied. How to read which key was really sent from the error itself, why an old key can beat your .env, and the fix.

    > AuthenticationError: 401 Incorrect API key provided: sk-old-s*****************1111.

    AI3 min
  3. No.021

    Fix: The OPENAI_API_KEY environment variable is missing or empty

    OpenAI's SDK or Codex CLI says your key is missing, though it's in .env. Every wording, in JavaScript, Python and Codex, and the fix for each.

    > OpenAIError: The OPENAI_API_KEY environment variable is missing or empty; either provide it,

    AI4 min

Post card · Newsletter

Get the next fix in your inbox.

One short email when a new entry is published. No spam, never shared, and you can leave any time.

— Nilay

or follow by RSSor on X

By subscribing you agree to the privacy note. One click to leave.

tip: paste the exact error text