Skip to content
Nilay Kabariya

Entry No.043·AI·Updated ·5 min read

Fix: APIConnectionError "Connection error." (OpenAI, Anthropic)

OpenAI's and Anthropic's SDKs both say only Connection error. Five real causes reproduced, from a local model that isn't running to antivirus HTTPS scanning.

by Nilay#openai#anthropic#node#windowsAI

The error, verbatim

Jump to the fix ↓
APIConnectionError: Connection error.
    at OpenAI.makeRequest (file:///C:/project/node_modules/openai/client.mjs:911:19)
    at process.processTicksAndRejections (node:internal/process/task_queues:104:5) {
  status: undefined,
  headers: undefined,
  requestID: undefined,
  cause: [TypeError: fetch failed] {
    [cause]: Error: unable to verify the first certificate; if the root CA is installed locally, try running Node.js with --use-system-ca
        at TLSSocket.onConnectSecure (node:internal/tls/wrap:1649:34) {
      code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'
    }
  }
}
✓ Reproduced on Windows 11 Pro

The fix that worked

Print the error's cause. That's where the real reason is:

console.error(err.cause?.cause ?? err.cause);
Not it? Every cause, tested ↓

Tested on

openai (JS)
7.30.0; re-tested on 7.47.0
@anthropic-ai/sdk
0.131.0; re-tested on 1.3.0
Node
24.14.0
OS
Windows 11 Pro

✓ Last re-run on GitHub Actions · see the run · all repros

“Connection error.” means the SDK never got an answer from the API at all: no status code, no error body. It says nothing about your API key, your model or your code. The SDK’s message is the same for every cause, and both OpenAI’s and Anthropic’s SDKs use the same class and wording. The real reason is two levels down, in the error’s cause.

Read the cause first

Log the cause chain instead of the message:

try {
  await client.chat.completions.create({ /* … */ });
} catch (err) {
  console.error(err.cause?.cause ?? err.cause);
}

Then match it here:

Cause in the error What it means Go to
ECONNREFUSED Nothing is listening at that address 1
ENOTFOUND The host name doesn’t exist 2
UNABLE_TO_VERIFY_LEAF_SIGNATURE (or DEPTH_ZERO_SELF_SIGNED_CERT for a self-signed one) Something is replacing the API’s HTTPS certificate 3
Fails only on a work network that requires a proxy Node isn’t using your proxy 4

If the message is Request timed out. instead (APIConnectionTimeoutError), the connection was made but the answer took longer than the SDK’s timeout. That’s a different problem: raise timeout, or ask for less.

1. ECONNREFUSED: a local model server that isn’t running

The most common one now. You point baseURL at Ollama, LM Studio or a local proxy, and it isn’t started:

APIConnectionError: Connection error.
  cause → TypeError: fetch failed
  cause → AggregateError: ECONNREFUSED

That was baseURL: 'http://localhost:11434/v1' (Ollama’s port) with nothing running. Anthropic’s SDK with the same address failed identically.

Start the server, then check the address answers from the same machine before blaming the SDK:

curl http://localhost:11434/v1/models

If curl can’t connect either, it’s the server or the port, not your code.

2. ENOTFOUND: a typo or a wrong base URL

APIConnectionError: Connection error.
  cause → TypeError: fetch failed
  cause → Error: getaddrinfo ENOTFOUND api.openai.con

One letter off in baseURL (or in an environment variable like OPENAI_BASE_URL) and the name simply doesn’t exist. The cause prints the exact host it tried, so compare it character by character.

3. Something is replacing the HTTPS certificate

This is the confusing one, because it only happens on some machines. Company SSL inspection and antivirus “HTTPS scanning” both work the same way: they decrypt your traffic and re-sign it with their own certificate. Node doesn’t trust that certificate by default, so the connection fails:

APIConnectionError: Connection error.
  cause → TypeError: fetch failed
  cause → Error: unable to verify the first certificate; if the root CA is installed locally,
          try running Node.js with --use-system-ca
  code: 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'

I hit this for real. The test machine runs Avast, whose Web Shield intercepts HTTPS: the certificate Node received for api.openai.com was issued by Avast Web/Mail Shield Root, not by OpenAI’s CA. With nothing telling Node to trust that root, both SDKs failed against the real APIs with the message above.

Node’s own hint is the fix. Tell Node to trust the certificates Windows already trusts, which includes the antivirus or company root:

node --use-system-ca app.js

Or set it once in the environment, so every Node process picks it up:

NODE_USE_SYSTEM_CA=1

4. A proxy you set, which Node ignores

On networks where everything must go through a proxy, people set HTTPS_PROXY and expect it to be used. The SDKs call Node’s built-in fetch, and by default it ignores that variable.

I ran a local proxy that logs every connection, set HTTPS_PROXY=http://127.0.0.1:8899, and called the API. The request succeeded, and the proxy log stayed empty: it went straight out, past the proxy. I couldn’t block direct traffic on this machine to show the failure itself, but on a network that only lets traffic out through the proxy, that direct attempt is the one that can’t get through.

On Node 24, tell Node to use the proxy variables:

NODE_USE_ENV_PROXY=1

If the proxy itself is down, you’re back to case 1: with the proxy port closed, the error’s cause was connect ECONNREFUSED 127.0.0.1:8898, the proxy’s address rather than the API’s.

What didn’t work

How this was tested

The OpenAI JavaScript SDK 7.30.0 and Anthropic’s 0.131.0 on Node 24.14.0, Windows 11, each making one chat request with a fake key, so the only possible success was a 401 from the real API. Causes were produced on purpose: a baseURL on a closed port, a misspelled host, a request timeout of 1 ms, a local logging proxy, and the machine’s own Avast HTTPS scanning, with Node’s certificate settings added and removed one at a time. I tested the JavaScript SDKs only. Python’s SDKs use the same error class name but a different HTTP client, so their proxy and certificate behaviour may differ.

November 2, 2026: all four causes were re-run on Linux (GitHub Actions ubuntu-latest, Node 24.22.1) with openai 7.47.0 and @anthropic-ai/sdk 1.3.0, fake keys only. ECONNREFUSED and ENOTFOUND came back as the cause of “Connection error.” in both SDKs. For section 3, a local HTTPS server with a self-signed certificate stood in for the intercepting software: it failed with DEPTH_ZERO_SELF_SIGNED_CERT, and NODE_EXTRA_CA_CERTS, --use-system-ca and NODE_USE_SYSTEM_CA=1 (with the certificate in the system store) each fixed it, while the system store alone did not. With HTTPS_PROXY set, a logging proxy saw no traffic until NODE_USE_ENV_PROXY=1, then logged CONNECT api.openai.com:443 and CONNECT api.anthropic.com:443. The script and output are public.

November 11, 2026: the same causes and fixes ran again on GitHub Actions ubuntu-latest with Node 24.22.2, openai 7.55.0 and @anthropic-ai/sdk 1.8.0: ECONNREFUSED, ENOTFOUND, the self-signed certificate, each certificate fix, and HTTPS_PROXY ignored until NODE_USE_ENV_PROXY=1 was set. All matched. Run 43056332195.

— N.K., end of entry No.043

Useful? Pass it on:Post on XFollow @EmotionalMatter

Related entries

  1. No.088

    Responses.create() unexpected keyword argument: new names

    max_tokens, messages, response_format, seed: their names in OpenAI's Responses API, tested. Plus the 'proxies' TypeError from httpx 0.28.

    > TypeError: Responses.create() got an unexpected keyword argument 'max_tokens'

    AI6 min
  2. No.087

    Claude SDK: unexpected keyword argument temperature, betas

    Messages.create() got an unexpected keyword argument after an anthropic upgrade, plus what SDK 2.0 (October 21) breaks next. Python and TypeScript, tested.

    > TypeError: Messages.create() got an unexpected keyword argument 'temperature'

    AI5 min
  3. No.060

    Claude "temperature is deprecated for this model" (400)

    Switching to Claude Sonnet 5.5, Haiku 5.5 or Opus 4.7 breaks code that sets temperature, even temperature: 0. Every case tested, in Node and Python.

    > BadRequestError: 400 {"type":"error","error":{"type":"invalid_request_error","message":"`temperature` is deprecated for this model."}}

    AI4 min

tip: paste the exact error text