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
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
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