Fix Claude Code "API Error: 401 Invalid or disabled API key"
API Error: 401 {"type":"error","error":{"type":"authentication_error","message":"Invalid or disabled API key"}}Claude Code shows API Error: 401, "Invalid API key" or "invalid x-api-key", or your app says "API key is invalid"? The key is not reaching CCSub, or it is wrong, revoked or expired. Check /status and the base URL, test the key, and remove a stale ANTHROPIC_API_KEY.
Fix it in 3 steps
- 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
Check the key and test it on its own
Open API Keys in the CCSub dashboard, make sure the key is enabled, and compare the masked value there (first characters and last 4) with the key you configured. A full key starts with sk- and is 67 characters long; it is shown only once, at creation, so if you no longer have it, create a new one. 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
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 /loginNot logged in · Please run /loginAPI Error: 401 {"type":"error","error":{"type":"authentication_error","message":"此 API Key 已作废(修改或重置密码后会自动作废,或因安全原因被平台吊销),请到 https://www.ccsub.net/zh/keys 新建一个 Key 并替换到客户端配置里 | This API key has been revoked (keys are revoked automatically after a password change or reset, or for security reasons). Create a new key at https://www.ccsub.net/en/keys and update your client."}}API Error: 401 {"type":"error","error":{"type":"authentication_error","message":"此 API Key 已过期,请到 https://www.ccsub.net/zh/keys 修改有效期或新建一个 Key | This API key has expired. Extend it or create a new key at https://www.ccsub.net/en/keys."}}
Which 401 message do you have?
When the key is the problem, CCSub answers with one of these 401 messages:
Missing or invalid API key— the request had no key, or the value didn't start withsk-. CCSub looks at theX-Api-Keyheader first, thenAuthorization: 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 ansk-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.This API key has been revoked ...— changing or resetting your password revokes every key on your account, and a key can also be revoked for security reasons. A revoked key can't be switched back on. Create a new one on API Keys and put it into every tool that used the old one.This API key has expired ...— the expiry date set on this key has passed. Set a later date for it on API Keys, or create a new key.
The last two messages are bilingual: the Chinese text comes first, then the English after a |. Their full wording is listed above, under Other forms of the same error.
If your 401 message is none of these, the request most likely never reached CCSub's key check: ANTHROPIC_BASE_URL isn't being applied, so go back and check the URL. Rarely, the 401 is an upstream line's error passed back to you. If the Anthropic base URL line in /status shows https://www.ccsub.net and the key test page passes, email support@ccsub.net with the time of the error.
"invalid x-api-key"
This all-lowercase message isn't one of the messages listed above. Most likely your key went to a different server, so fix the URL before you change the key:
- Claude Code: run
/statusand check theAnthropic base URLline. It should showhttps://www.ccsub.net, as in step 1 at the top of this page. - opencode: set
options.baseURLin your provider entry tohttps://www.ccsub.net/api/v1, and don't leave it out. See the opencode setup guide.
Once the URL is fixed, the same request gets a normal reply and shows up on your Usage page with token counts. If the URL is already right, the key test page passes and the error stays, email support@ccsub.net with the time of the error.
This applies to Claude Code, and to opencode set up with the @ai-sdk/anthropic package as in its setup guide. For other tools, compare the base URL with that tool's page in all integration guides.
"This turn failed" and "API key is invalid"
Some apps don't print the raw response. Instead of API Error: 401 ... they show a short notice, such as This turn failed and API key is invalid. The notice stands for a 401: the key that app sent was rejected, or it sent no key. Some apps show the same notice for a 403 too. Check the app's own provider settings, not just your terminal:
- Base URL. It has to be CCSub's address exactly as that tool's setup guide gives it:
https://www.ccsub.netfor Claude Code,https://www.ccsub.net/api/v1for Codex and opencode. Other tools are in all integration guides. - Key. Use the full key you saved when you created it:
sk-plus 64 hex characters, with no spaces or line breaks. If you changed or reset your password, the old key was revoked; create a new one on API Keys. - Test the key on its own. Use the key test page or the curl command below. If the test passes, the key is fine, and the app is sending something else or sending it somewhere else.
When it's fixed, the next message gets a normal reply and shows up on your Usage page with token counts. If the address and the key are both right, the test passes and the notice keeps coming back, email support@ccsub.net with the time of the error.
This applies to apps that replace the raw response with a short notice of their own. If your tool prints the raw 401, match the message against the list above instead.
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: make sure it's complete (if you lost it, create a new one on 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. For security, the full key is shown only once, when you create it; the list afterwards shows only a masked value. If you no longer have the full key, create a new one and disable the old 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?
- Read the troubleshooting guide (Chinese)
- Contact support: Email support@ccsub.net with the tool and version, your OS, the full error text, and your account email (never send your API key).
- Re-check your setup against the integration guide: Claude Code setup guide, CC Switch setup guide
Related errors
unexpected status 401 Unauthorized: {"type":"error","error":{"type":"authentication_error","message":"Missing or invalid API key"}}, url: https://www.ccsub.net/api/v1/responsesCheck the url in the error to see where the request went, then define a custom provider with env_key and set that variable in the same environment Codex runs in.Unable to connect to API (ECONNREFUSED)The request never reached CCSub — usually a proxy variable pointing at a proxy that's off. Check env | grep -i proxy, unset it or add NO_PROXY for ccsub.net. Stuck on first launch? Edit ~/.claude.json.API Error: 402 {"type":"error","error":{"type":"insufficient_balance","message":"余额不足,请前往 https://www.ccsub.net/purchase 充值"}}Your available balance or today's subscription quota ran out. Top up, wait for the daily reset at midnight Beijing time, or — if you do have balance — retry in a few minutes.
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.