Short answer
Unable to connect to API means Claude Code couldn't open a connection to the address it sends model requests to, or the connection broke. The code in parentheses tells you which part failed: nothing answered (ConnectionRefused), the name didn't resolve (ENOTFOUND), the connection was cut (ECONNRESET), or a certificate wasn't trusted.
Work through it in this order:
- Find the address. Run
/statusand read theAnthropic base URLline. If you never set one, it'sapi.anthropic.com. Also runecho $ANTHROPIC_BASE_URL. - Test that address with
curlfrom the same shell. Ifcurlfails too, the problem is your network, VPN, firewall or proxy. Ifcurlworks, the problem is in what Claude Code reads: its environment and settings files. - Look for a leftover
ANTHROPIC_BASE_URL. A value that points at a local proxy or switcher that is no longer running givesConnection refused, even thoughapi.anthropic.comis reachable.
Claude Code has already retried before it shows this error, so sending the message again rarely helps until you change something.
What the error looks like
Since v2.1.227, the message names the kind of failure and keeps the code in parentheses. From the Claude Code error reference:
Unable to connect to API. Check your internet connection
Connection refused — a firewall or proxy may be blocking it (ConnectionRefused)
Can't reach the API server — check your internet or DNS (ENOTFOUND)
No internet route — check your connection or VPN (EHOSTUNREACH)
Couldn't connect through your proxy (ERR_PROXY_TUNNEL) — the proxy refused the tunnel: check its credentials and that it allows this host
Connection dropped (ECONNRESET)
Request timed out. Check your internet connection and proxy settings
Older versions showed Unable to connect to API followed by the code. While Claude Code retries, the spinner counts down, as in anthropics/claude-code#84782:
Unable to connect to API (ConnectionRefused) · Retrying in Ns · attempt N/10
Intermittent drops look like this (#83399):
API Error: Unable to connect to API (ECONNRESET)
Certificate problems use the same opening. Current versions add the OpenSSL code and a NODE_EXTRA_CA_CERTS hint; older ones read as in #74369:
API Error: Unable to connect to API: Self-signed certificate detected. Check your proxy or corporate SSL certificates
What each code means
| Code | What failed | Check first |
|---|---|---|
ConnectionRefused / ECONNREFUSED |
Nothing accepted the connection at that address and port | A leftover ANTHROPIC_BASE_URL pointing at localhost, a stopped local proxy, a firewall |
ENOTFOUND / FailedToOpenSocket |
The hostname didn't resolve | A typo in the base URL, a broken DNS resolver (common in WSL) |
EHOSTUNREACH |
No network route to the host | A VPN that is half-connected or was uninstalled |
ERR_PROXY_TUNNEL |
Your proxy refused to open a tunnel | Proxy credentials, and whether the proxy allows that host |
ECONNRESET |
The connection was opened, then cut | A proxy, VPN or firewall dropping long connections; an unstable network |
Request timed out |
No response headers arrived in time | A slow or blocked path; API_TIMEOUT_MS |
Self-signed certificate detected, SSL certificate verification failed |
A TLS-inspecting proxy or a gateway with a private CA | NODE_EXTRA_CA_CERTS |
How to fix it
1. See which address Claude Code is calling
Run /status. The Anthropic base URL line shows where requests go, and the Proxy line shows the active proxy URL (or marks one it can't parse as invalid). Then check the places a base URL can come from:
echo $ANTHROPIC_BASE_URL
env | grep -i -E 'anthropic|proxy'
In PowerShell, use echo $env:ANTHROPIC_BASE_URL and Get-ChildItem Env:*PROXY*. Also open ~/.claude/settings.json and the project's .claude/settings.local.json and look at the env block. A settings-file value wins over a shell export.
2. Test the same address with curl
For the default endpoint, the error reference suggests:
curl -I https://api.anthropic.com
On Windows PowerShell, type curl.exe so the built-in Invoke-WebRequest alias isn't used. If you use a gateway, test the gateway's URL with your key instead. The gateway guide uses a one-token request:
curl -sS -w '\n%{http_code}\n' -X POST "$ANTHROPIC_BASE_URL/v1/messages" \
-H "Authorization: Bearer $ANTHROPIC_AUTH_TOKEN" \
-H "anthropic-version: 2023-06-01" \
-H "content-type: application/json" \
-d '{"model": "claude-sonnet-5", "max_tokens": 1, "messages": [{"role": "user", "content": "."}]}'
Any HTTP status, even a 401 or an unknown-model error, means the network path works. A curl connection error means it doesn't, so fix the network path before you change anything in Claude Code.
3. If curl fails too: fix the network path
- VPN. Turn it off and on again, or try without it. On macOS, a disconnected or uninstalled VPN can leave a stale
utuninterface behind (checkifconfig) or a network extension in System Settings. - Corporate proxy. Set
HTTPS_PROXYbefore starting Claude Code, with the scheme included:export HTTPS_PROXY=http://proxy.example.com:8080. Claude Code doesn't support SOCKS proxies, and it stops at launch if it can't parse the proxy URL. - Firewall. Make sure it allows the host in your base URL. Anthropic lists the hosts Claude Code needs under network access requirements.
- DNS. On Linux and WSL, check
/etc/resolv.conffor a nameserver that can't be reached.
4. If curl works but Claude Code fails: fix what Claude Code reads
- Remove a stale base URL. The error reference calls this out: a leftover
ANTHROPIC_BASE_URLpointing at a local proxy or gateway that's no longer running givesConnection refusedwhilecurl https://api.anthropic.comsucceeds. Remove it from your shell profile and from any settingsenvblock, then open a new terminal. - Give Claude Code the same proxy settings. A proxy exported in one terminal doesn't reach an editor started from the dock or Start menu, or background sessions. Put
HTTPS_PROXYandNO_PROXYin theenvblock of~/.claude/settings.jsonso every session gets them. - Rule out Docker Desktop. Container runtimes can intercept outbound traffic. Quit it and retry.
- Update Claude Code. Run
claude update. Several connection bugs have been fixed in recent versions.
5. Certificate errors: trust the right CA
A TLS-inspecting proxy, or a gateway signed by a private CA, presents a certificate Claude Code doesn't trust. Point it at the CA bundle:
export NODE_EXTRA_CA_CERTS=/path/to/ca-bundle.pem
Don't set NODE_TLS_REJECT_UNAUTHORIZED=0, which turns off certificate checks entirely. Since v2.1.199, certificate failures aren't retried, so you see the error on the first attempt.
If you use a gateway
With ANTHROPIC_BASE_URL set, every model request goes to that address, so "Unable to connect" is about the path to the gateway, not to Anthropic.
- Check the URL format. Use the gateway's base URL exactly as documented. For TeamoRouter, that is
https://api.teamorouter.comwith no/v1, because Claude Code adds the path itself. - Local switchers and proxies. Tools that point Claude Code at
http://127.0.0.1:<port>only work while that local process runs. If you stopped using the tool, remove the variable it left behind. - A 401 isn't a connection error. If
curlreaches the gateway and returns 401, the network is fine and the credential is the problem. See Claude Code "Invalid API key · Please run /login".
A different message, Unable to connect to Anthropic services, comes from the connectivity check during first-run setup, before sign-in. The error reference covers when that check runs.
Connecting Claude Code to TeamoRouter
TeamoRouter is an API gateway with one key for Claude, GPT, Gemini and DeepSeek. Claude runs on the native Anthropic protocol (/v1/messages), so Claude Code only needs a base URL and a key. There's no subscription; you pay per use.
- Create an API key in the TeamoRouter console. It starts with
sk-teamo-. - Top up from $5. The top-up dialog takes cards. You can also click Pay with crypto and pay with USDT, USDC or ETH from any exchange app — the checkout shows which networks your amount supports (BEP20 works from $5). Step by step: Pay with USDT.
- Test the path and the key from the shell you'll run Claude Code in:
curl https://api.teamorouter.com/v1/models \
-H "Authorization: Bearer sk-teamo-..."
A model list means both the network path and the key work. A connection error means the network path to api.teamorouter.com is the problem, so go back to step 3 above.
- Set the two variables and start Claude Code from the same shell:
export ANTHROPIC_BASE_URL=https://api.teamorouter.com
export ANTHROPIC_AUTH_TOKEN="your TeamoRouter API key"
claude
Run /status to confirm the base URL. Windows commands and settings-file setup are in Install and connect Claude Code.
FAQ
Why does claude -p work when the interactive session doesn't?
First check that both run with the same environment: a script or another terminal may have different variables. If they do, it's likely a bug. In #84782, only the interactive session failed, and the reporter's network checks all passed. If yours matches, update Claude Code and follow that report.
Will reinstalling Claude Code fix it? Rarely. The address, proxy and certificate settings live in your environment and settings files, which a reinstall keeps.
Is ECONNRESET a problem on my side? Often it's something between you and the server cutting long connections: a proxy, VPN or firewall. Claude Code retries dropped connections automatically. If it happens on every request, test without the VPN or proxy.
Does Unable to connect to API mean my key is wrong?
No. A wrong key gets a response, usually a 401. This error means no usable response came back at all.