OpenAI API Connection Error: How to Fix
Updated 10/24/2026
The APIConnectionError in OpenAI’s SDKs occurs when your application fails to establish a secure network socket with the OpenAI API servers (api.openai.com). This is rarely an issue on OpenAI’s side; instead, it is typically caused by local network restrictions, misconfigured proxy servers, outdated SSL certificates, or incorrect DNS resolution.
If your Python or Node.js scripts are throwing connection errors, follow these steps to isolate and resolve the issue.
1. Isolate the Connection (Network vs. Code) Before modifying your codebase, determine if your local machine can reach OpenAI's servers at all.
Open your terminal or command prompt and run a simple curl request:
`bash curl -I https://api.openai.com/v1/models `
If this command returns a 200 OK or 401 Unauthorized status, your system's network configuration is healthy, and the issue lies within your SDK configuration or local runtime environment (like Python or Node).
If the command hangs, times out, or returns a "Could not resolve host" error, the issue is system-wide, likely stemming from your router, ISP, or corporate firewall.
Agent Builder turns a task you repeat into a reusable assistant with setup text for each app.
Tickd is independent and not affiliated with any AI company.
2. Configure HTTP and HTTPS Proxies If your code is running behind a corporate network or a VPN, the OpenAI SDK must be explicitly configured to route traffic through your proxy servers.
You can set these globally using environment variables. In your terminal or deployment environment, set the following:
On Windows (Command Prompt): `cmd set HTTP_PROXY=http://your-proxy-server:port set HTTPS_PROXY=http://your-proxy-server:port `
On macOS and Linux: `bash export HTTP_PROXY="http://your-proxy-server:port" export HTTPS_PROXY="http://your-proxy-server:port" `
For the Python SDK (v1.0.0+), you can also pass proxies directly to the client initializer to avoid global environment pollution:
`python import httpx from openai import OpenAI
Pass a custom HTTPX client with proxy configuration client = OpenAI( http_client=httpx.Client( proxies="http://your-proxy-server:port" ) ) ```
3. Update SSL Certificates and Certifi Python environments rely on a package called `certifi` to verify SSL certificates. If your local certificate bundle is outdated, the SSL handshake with `api.openai.com` will fail, causing a connection error.
Update your Python certificate store by running:
`bash pip install --upgrade certifi `
If you are running on macOS, Python installations sometimes skip installing system certificates altogether. Navigate to your Python installation folder (usually /Applications/Python 3.x/) and double-click the file named Install Certificates.command to update your root certificates.
4. Increase SDK Timeouts and Retries On slow or unstable networks, requests can time out before OpenAI's servers can generate a complete response (especially when using slower models like GPT-4).
You can increase the default timeout settings directly inside your OpenAI client configuration:
`python from openai import OpenAI
Set custom timeout limits (in seconds) client = OpenAI( timeout=60.0, # Increase timeout to 60 seconds max_retries=3 # Auto-retry failed requests up to 3 times ) ```
In Node.js, configure the timeout option during initialization:
`javascript import OpenAI from 'openai';
const openai = new OpenAI({ timeout: 60000, // Timeout in milliseconds (60 seconds) maxRetries: 3 }); `
5. Force IPv4 Over IPv6 Some ISPs and routers have modern IPv6 configurations that struggle to route packets to OpenAI's CDN properly. Forcing your system or your Python runtime to use IPv4 can resolve sudden connection dropouts.
To verify if this is the issue, try forcing a curl request over IPv4:
`bash curl -4 -I https://api.openai.com/v1/models `
If this connects immediately while the default curl hangs, configure your local environment or Docker container to prefer IPv4.