Status 401Authentication (401)Claude CodeCC Switch

Fix Claude Code "API Error: 401 Invalid or disabled API key"

Error message
API Error: 401 {"type":"error","error":{"type":"authentication_error","message":"Invalid or disabled API key"}}

Getting API Error: 401 authentication_error in Claude Code with a custom ANTHROPIC_BASE_URL? The key is either not reaching CCSub or it's the wrong one. Check /status, re-copy the key, and remove a stale ANTHROPIC_API_KEY.

Fix it in 3 steps

  1. 1

    Run /status inside Claude Code

    The Status tab should show an Anthropic base URL line with https://www.ccsub.net and an Auth token line naming ANTHROPIC_AUTH_TOKEN. If the base URL line is missing, your environment variables never reached this session.

  2. 2

    Re-copy the key and test it on its own

    Open API Keys in the CCSub dashboard, make sure the key is enabled, and copy it again (it starts with sk- and is 67 characters long). Test it with the curl command below or the dashboard's key test page. If that works, the key is fine and the problem is your Claude Code config.

  3. 3

    Remove conflicting variables and restart the terminal

    Unset ANTHROPIC_API_KEY if you don't need it (it is sent as X-Api-Key, which CCSub reads first), check the env block in ~/.claude/settings.json for an old key or URL, then close every terminal window and start claude from a fresh one.

Other forms of the same error

  • API Error: 401 {"type":"error","error":{"type":"authentication_error","message":"Missing or invalid API key"}}
  • Invalid API key · Please run /login
  • Not logged in · Please run /login

Which 401 message do you have?

CCSub returns exactly two 401 messages, and they mean different things:

  • Missing or invalid API key — the request had no key, or the value didn't start with sk-. CCSub looks at the X-Api-Key header first, then Authorization: Bearer. If neither is present you get this one, which almost always means your environment variables didn't load.
  • Invalid or disabled API key — the request carried an sk- value, but no matching key exists or it's been disabled or deleted in the dashboard. Typical causes: a trailing space or newline from copy-paste, a truncated key, a key from a different provider, or a key you deleted.

If your 401 message is anything other than these two strings, the request never reached CCSub at all. That means ANTHROPIC_BASE_URL isn't being applied — go back and check the URL.

Test the key with curl

Run this from the same shell where you exported the variables. It sends one tiny billable request:

curl -sS -w '\n%{http_code}\n' https://www.ccsub.net/v1/messages \
  -H "Authorization: Bearer $ANTHROPIC_AUTH_TOKEN" \
  -H "anthropic-version: 2023-06-01" \
  -H "content-type: application/json" \
  -d '{"model":"claude-sonnet-4-6","max_tokens":16,"messages":[{"role":"user","content":"hi"}]}'

A 200 with a JSON body means the key and your network are fine, and Claude Code is reading something else. A 401 means the key itself is the problem: copy it again from API Keys, or double-check it on the key test page. If echo $ANTHROPIC_AUTH_TOKEN prints nothing, the variable doesn't exist in that shell.

The five usual suspects

1. You didn't open a new terminal, or your editor was launched from the Dock. An export in ~/.zshrc only applies to terminals opened afterwards. On Windows, variables set with SetEnvironmentVariable need every PowerShell window closed and reopened. The Claude Code docs also warn that an editor launched from the Dock or Start menu doesn't see shell exports. For the VS Code extension, put both variables in claudeCode.environmentVariables in your VS Code user settings.

2. A stale key is sitting in ANTHROPIC_API_KEY. Claude Code sends ANTHROPIC_API_KEY as X-Api-Key and ANTHROPIC_AUTH_TOKEN as Authorization: Bearer. CCSub reads X-Api-Key first. So if ANTHROPIC_API_KEY holds an old key, another provider's key, or a deleted one, you'll get Invalid or disabled API key even when ANTHROPIC_AUTH_TOKEN is perfect. Run unset ANTHROPIC_API_KEY and remove the line from your shell profile.

3. ~/.claude/settings.json overrides your shell. According to the official docs, when the same variable is set in your shell and in a settings file's env block, the settings file wins. If you've used CC Switch or a similar tool, ~/.claude/settings.json may still contain another provider's key and URL. Open it, look at env, and fix or remove the duplicates. Check .claude/settings.local.json in your project folder too.

4. Extra characters from copy-paste. A CCSub key is sk- followed by 64 hex characters — 67 characters total. Chat apps and docs often add a space, a line break, or smart quotes. The dashboard keeps your keys, so you can go back to the list and copy again at any time; no need to create a new one.

5. The key was disabled or deleted. Check its status on API Keys. A disabled key can be re-enabled. A deleted key is gone — create a new one and update your config.

"Not logged in · Please run /login"

This one doesn't come from CCSub. It's Claude Code telling you it found no usable credential locally. Usually the variables never made it into the session (see #1). There's one more trap: if you use ANTHROPIC_API_KEY, Claude Code asks on first launch whether to use it, and the default answer is No. Press Enter once and the key is silently ignored from then on. You can turn it back on with Use custom API key under /config, but the simpler fix is switching to ANTHROPIC_AUTH_TOKEN, which needs no approval.

FAQ

What's the difference between "Missing or invalid API key" and "Invalid or disabled API key"?

"Missing or invalid API key" means the request carried no key at all, or the value didn't start with sk-. "Invalid or disabled API key" means it carried an sk- value that CCSub doesn't recognize, or one you disabled or deleted in the dashboard. The first points at your environment, the second at the key itself.

Should I use ANTHROPIC_AUTH_TOKEN or ANTHROPIC_API_KEY?

Use ANTHROPIC_AUTH_TOKEN for CCSub. It goes out as an Authorization: Bearer header and takes effect immediately. ANTHROPIC_API_KEY goes out as X-Api-Key and, in interactive mode, needs a one-time approval — decline it once and Claude Code silently ignores it. If both are set, CCSub reads X-Api-Key first, so don't leave an old key in ANTHROPIC_API_KEY.

Am I charged for 401 errors?

No. Authentication failures are rejected before anything is forwarded to a model, so there is no charge.

I switched providers with CC Switch and still get 401. Why?

CC Switch writes the provider settings into the env block of ~/.claude/settings.json. Per the Claude Code docs, when a variable is set both in your shell and in a settings file, the settings file wins. Update the key in CC Switch and re-enable the provider, or edit the file by hand.

Still stuck?

New to CCSub?

Sign up, create an API key in the dashboard, and paste it into Claude Code, Codex CLI or your editor. You pay for what you use.