Claude Code API errors: Invalid API key, Not logged in, 403
When Claude Code runs on your own API key or a custom base URL, the error text does not always point at the real cause. A base URL that ends in /v1, for example, is reported as a problem with the model. Below, each error is listed by its exact text. Checked against Claude Code 2.1.312 on 30 October 2026.
The RoamAI teamPublished October 30, 2026
First, check what Claude Code is using
Type /status inside claude. Three lines matter here:
- Anthropic base URL: where requests go. If the line is missing, ANTHROPIC_BASE_URL is not set and requests go straight to Anthropic.
- Auth token: where ANTHROPIC_AUTH_TOKEN comes from.
- API key: where ANTHROPIC_API_KEY comes from.
A line marked not in use exists but is not being used. Values in the env block of settings.json override variables exported in your shell, so a changed shell variable can have no effect. See ANTHROPIC_AUTH_TOKEN vs ANTHROPIC_API_KEY.
Invalid API key · Fix external API key
The server rejected the key in ANTHROPIC_API_KEY with HTTP 401. Common causes:
- The key was copied with missing or extra characters, spaces or quotes.
- The key was disabled or deleted.
- The key and the base URL belong to different providers.
- The provider expects an Authorization header. ANTHROPIC_API_KEY sends x-api-key, so switch to ANTHROPIC_AUTH_TOKEN.
After fixing it, open a new terminal and try again. If the key is in settings.json, change it there.
Not logged in · Please run /login
This appears in two cases. Either neither ANTHROPIC_AUTH_TOKEN nor ANTHROPIC_API_KEY is set, so Claude Code expects a claude.ai login. Or ANTHROPIC_AUTH_TOKEN is set and the server answered 401: Claude Code 2.1.312 shows this same line instead of saying the token is invalid.
With a custom base URL, do not run /login. Check in /status that the Auth token line is there, then check the token itself as in the section above.
API Error: 403 Request not allowed
The full message usually starts with Failed to authenticate. API Error: 403 and then shows what the server returned, such as Request not allowed. The most common cause is that the request went to Anthropic directly from a region Anthropic does not serve. Anthropic's troubleshooting page says that if the error mentions App unavailable in region, Claude Code is not available in your country.
- If you meant to use a custom base URL, look for the Anthropic base URL line in /status. If it is missing, ANTHROPIC_BASE_URL did not take effect and the request still went to Anthropic.
- If claude.ai opens in your browser but claude in the terminal gets 403, the terminal may not be using your system proxy. Set HTTPS_PROXY in the terminal.
- With a Claude subscription, Anthropic suggests checking at claude.ai/settings that it is still active. With a Console API key, check that your account has the Claude Code or Developer role.
- A third-party service can also answer 403 for its own reasons. Read the text after 403 and follow the provider's instructions.
There's an issue with the selected model
The full text is There's an issue with the selected model (model name). It may not exist or you may not have access to it. Claude Code shows it whenever the server returns 404, so the model is not always the problem.
- ANTHROPIC_BASE_URL ends in /v1. Claude Code adds /v1/messages itself, so https://example.com/v1 sends requests to /v1/v1/messages and the server answers 404. Stop the base URL at the host and port unless your provider tells you to add a path.
- The provider does not offer that model, or your key cannot use it. With an API key, Claude Code 2.1.312 defaults to claude-opus-5-5. Pick another with /model, or start with --model claude-sonnet-5-5.
Connection refused and timeouts
API Error: Connection refused — a firewall or proxy may be blocking it (ECONNREFUSED)Claude Code could not reach the address: the host or port is wrong, the service is down, or a firewall or proxy is in the way. Test the address with curl as shown below. If curl cannot connect either, the problem is the network or the address, not Claude Code.
The error takes a long time to appear
Claude Code retries failed requests automatically, including 401s, so a wrong key can take a while to show an error. While you troubleshoot, turn retries off and the error appears at once:
CLAUDE_CODE_MAX_RETRIES=0 claude -p "hi"Remove the variable afterwards. In normal use, retries cover short network hiccups.
Test the URL and key with curl
Send one minimal request without Claude Code. For a provider that expects x-api-key:
curl https://your-provider.example/v1/messages \
-H "x-api-key: YOUR_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{"model":"claude-sonnet-5-5","max_tokens":32,"messages":[{"role":"user","content":"hi"}]}'For a provider that expects Authorization, replace the second line with -H "Authorization: Bearer YOUR_KEY".
- JSON with a content field: the URL and key are fine. Go back to the Claude Code settings.
- 401: the key is wrong, or the header is the wrong one.
- 404: the path is wrong. Check for a missing or extra segment.
- No connection: a network or address problem.
Set it up again from the start
The full setup is in Connect Claude Code to your API key. Which of the two key variables to use is covered in ANTHROPIC_AUTH_TOKEN vs ANTHROPIC_API_KEY.
One key per person for Claude Code
RoamAI is a Claude API for teams: create an API key in the console and use it in Claude Code, Claude Desktop or an SDK. VIP adds lower usage prices, dedicated servers and a named contact.
Get started