Fix OpenAI API Connection Refused Errors
Updated 10/5/2026
When your application fails to connect to OpenAI's servers, you will typically see errors like ECONNREFUSED, Connection refused, socket hang up, or APIConnectionError. These errors mean the network handshake between your server and api.openai.com was terminated before it could complete.
Because this happens before OpenAI's servers can process your API key, this is almost always a local network configuration, firewall restriction, proxy misalignment, or SDK integration issue. Follow these steps to diagnose and resolve the connection error.
1. Verify the API Endpoint and SDK Configuration
If you recently upgraded your code or changed SDK versions, your client might be pointing to an incorrect base URL or using deprecated initialization patterns.
- Verify the Base URL: Ensure your code is making requests to https://api.openai.com/v1. If you have configured a custom base URL helper or environment variable (OPENAI_BASE_URL), ensure it does not contain trailing slashes or duplicate paths.
- Update Your SDK: Outdated library versions may try to access deprecated endpoints. Update your installation to the latest version:
- Python: Run pip install --upgrade openai
- Node.js: Run npm install openai@latest
- Check the Client Initialization: In Python SDK v1.0.0+, the client must be instantiated explicitly. Do not use legacy v0.x syntax.
`python # Correct v1.0.0+ initialization from openai import OpenAI client = OpenAI( # Defaults to os.environ.get("OPENAI_API_KEY") api_key="your-api-key" ) `
2. Check Firewall, DNS, and IP Blocks
Many hosting providers, corporate networks, and local firewalls block outgoing traffic on port 443 (HTTPS) by default, or restrict traffic to unauthorized domains.
* Test with cURL: Run this command from the exact environment/server hosting your application to see if the connection reaches OpenAI: curl -I https://api.openai.com/v1/models * Analyze the output: * If you get a 200 OK or 401 Unauthorized (due to no API key), your local network is fine. The issue lies within your application's network configuration. * If you get Could not resolve host or Connection timed out, your DNS or firewall is blocking the request. * Fix DNS Resolution: If DNS fails, try switching your network interface to use Google DNS (8.8.8.8 and 8.8.4.4) or Cloudflare DNS (1.1.1.1). * Whitelist the Domains: Ensure your outbound firewall rules allow TCP traffic on port 443 to api.openai.com and files.openai.com.
3. Configure HTTP Proxy Settings
If your code runs behind a corporate or reverse proxy, your SDK client must be explicitly configured to route requests through that proxy, otherwise the connection will be dropped or refused.
For Python: Pass an explicit `http_client` using the `httpx` library to handle the proxy configuration:
`python import httpx from openai import OpenAI
Configure proxy tunnel proxies = { "http://": "http://your-proxy-address:port", "https://": "http://your-proxy-address:port", }
client = OpenAI( http_client=httpx.Client(proxies=proxies) ) `
For Node.js: Use an agent like `https-proxy-agent` to pass the connection settings:
`javascript const { OpenAI } = require('openai'); const { HttpsProxyAgent } = require('https-proxy-agent');
const agent = new HttpsProxyAgent('http://your-proxy-address:port');
const openai = new OpenAI({ httpAgent: agent, }); `
4. Resolve SSL/TLS Certificate Failures
If your local machine or server lacks updated root certificates, SSL verification will fail, causing the client library to abruptly terminate the connection.
* Update Certifi (Python): Python environments rely on the certifi package for trusted CA certificates. Run: pip install --upgrade certifi * Fix local environment paths (macOS): Python installations on macOS often miss system certificates. Run the command to install certificates included with your Python distribution: /Applications/Python\ 3.x/Install\ Certificates.command (replace 3.x with your actual version). * Avoid disabling SSL: You might see suggestions to disable SSL verification (verify=False). Do not do this in production, as it exposes your API key and data to man-in-the-middle attacks.
When to escalate
If you have confirmed that local curl requests to https://api.openai.com fail with connection timeouts or refusals from multiple machines, the issue might be an regional outage or an IP block. Check the official OpenAI Status page (status.openai.com) to ensure there are no active API delivery outages. If the status page is healthy, contact your network administrator or hosting provider to check if they are filtering outbound traffic to OpenAI's CDN provider (Cloudflare).