← Back to Blog

How to Set Up a Proxy for OpenAI Codex CLI in Countries Without OpenAI Support: Complete Guide

A step-by-step guide to setting up a proxy for OpenAI Codex CLI and API in regions without OpenAI support — with configuration examples and analysis of common errors.

📅November 22, 2026

OpenAI officially does not support dozens of countries — including Russia, Belarus, Iran, and several other regions. When trying to run codex from such locations, you will either receive a direct geolocation block, a refusal to issue an API key, or an unstable connection with constant interruptions when performing cloud tasks. This can be resolved with a properly selected and configured proxy server — but nuances are important here: not every type of proxy is suitable for CLI tools and long background processes.

Why Codex is Blocked in Your Country

OpenAI applies geoblocking on several levels simultaneously. First, the IP address is checked with every API request — if it belongs to a country on the unsupported list, the server will return an error 403 Country, region, or territory not supported. Second, when registering an account and linking a payment card, the system verifies the geolocation of the IP, the billing address of the card, and the time zone of the device — discrepancies increase the risk of account blocking. Third, even after successful registration, CLI sessions may drop if the IP suddenly changes its geolocation during a task — this is typical for dynamic IPs from mobile operators or when using free VPNs with an unstable pool of addresses.

For developers using Codex CLI in conjunction with automation — writing code, reviewing pull requests, generating tests in a CI pipeline — any connection drop means a failed task in the middle of execution. Therefore, it is important not just to "bypass the block," but to ensure a stable IP with the correct geolocation for the entire duration of the work.

What Type of Proxy is Needed for Codex CLI

For working with the OpenAI API and CLI tools based on it, three parameters are critical: session stability (the IP should not jump in the middle of a request), response speed, and the cleanliness of the address's reputation (whether it is on abuse service blacklists). Let's examine the options:

Proxy Type For Codex CLI Session Stability Speed
Residential Proxies Yes, primary choice High (sticky sessions) Medium-High
Datacenter Proxies Suitable for CI/CD and scraping Very High (fixed IP) Maximum
Mobile Proxies Excessive for API tasks Medium (operator rotation) Medium

For most scenarios — local development, personal CLI, irregular API requests — the best fit is residential proxies with a binding to one IP per session (sticky session). They provide real IPs from home providers in the selected country, which reduces the risk of falling under OpenAI's abuse detection filters. However, if Codex is launched in a cloud pipeline with a large number of parallel tasks — for example, automatic reviews of dozens of repositories — it is more advantageous to use datacenter proxies: they are faster and more stable at maintaining long connections without latency spikes.

Setting Up Proxy in Codex CLI via Environment Variables

Codex CLI, like most modern Node.js and Python tools, respects standard environment variables HTTP_PROXY and HTTPS_PROXY. This is the simplest way to route all CLI traffic through a proxy without modifying application configs.

# Linux / macOS (bash, zsh)
export HTTP_PROXY="http://login:password@proxy_host:port"
export HTTPS_PROXY="http://login:password@proxy_host:port"
export NO_PROXY="localhost,127.0.0.1"

# Check before launching Codex
curl -x $HTTPS_PROXY https://api.openai.com/v1/models

# Launch CLI
codex "write a unit test for the function validateEmail"
# Windows PowerShell
$env:HTTP_PROXY = "http://login:password@proxy_host:port"
$env:HTTPS_PROXY = "http://login:password@proxy_host:port"

# Check connection
Invoke-WebRequest -Uri "https://api.openai.com/v1/models" -Proxy $env:HTTPS_PROXY

codex "refactor this module"

If the proxy requires authentication with a username and password (most providers operate this way), specify them directly in the proxy URL in the format http://user:pass@host:port. For SOCKS5 proxies, use the socks5:// protocol instead of http:// — this is important because some CLI tools differentiate between protocols and may ignore an incorrectly specified scheme.

Proxy via Configuration File and Python Wrappers

If you are writing your own scripts around the OpenAI API (for example, batch processing tasks via openai-python SDK), environment variables may not be enough — sometimes it is more convenient to explicitly pass the proxy to the client.

import httpx
from openai import OpenAI

proxy_url = "http://login:password@proxy_host:port"

client = OpenAI(
    api_key="sk-...",
    http_client=httpx.Client(proxies=proxy_url, timeout=60.0)
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Generate a database migration"}]
)

print(response.choices[0].message.content)

For Node.js wrappers (if your Codex pipeline is built on JS), a similar logic applies through https-proxy-agent:

const { HttpsProxyAgent } = require('https-proxy-agent');
const OpenAI = require('openai');

const agent = new HttpsProxyAgent('http://login:password@proxy_host:port');

const client = new OpenAI({
  apiKey: process.env.OPENAI_API_KEY,
  httpAgent: agent,
});

const run = async () => {
  const res = await client.chat.completions.create({
    model: 'gpt-4o',
    messages: [{ role: 'user', content: 'Check the code for vulnerabilities' }],
  });
  console.log(res.choices[0].message.content);
};

run();

Explicitly passing the proxy in the code is useful when some requests in your application should go directly (internal services), while others should go through the proxy (requests to OpenAI). This way, you do not break the rest of the application's network logic with global environment variables.

Proxy for Cloud Tasks: Docker, CI/CD, GitHub Actions

If Codex is used not locally, but in an automated pipeline — code review on every pull request, nightly documentation generation, automatic refactoring — the proxy needs to be specified at the task execution environment level, not just in the developer's terminal.

# Dockerfile
FROM node:20-slim

ENV HTTP_PROXY="http://login:password@proxy_host:port"
ENV HTTPS_PROXY="http://login:password@proxy_host:port"
ENV NO_PROXY="localhost,127.0.0.1"

RUN npm install -g @openai/codex
WORKDIR /app
COPY . .
CMD ["codex", "run", "review"]
# .github/workflows/codex-review.yml
name: Codex Review
on: [pull_request]

jobs:
  review:
    runs-on: ubuntu-latest
    env:
      HTTPS_PROXY: ${{ secrets.PROXY_URL }}
      OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
    steps:
      - uses: actions/checkout@v4
      - name: Install Codex
        run: npm install -g @openai/codex
      - name: Run Codex review
        run: codex review --diff origin/main

For CI/CD, a fixed static IP is preferable — this is exactly the scenario where datacenter proxies perform better than residential ones: lower latency, connections do not drop on long jobs, and the cost of traffic for large volumes of requests is significantly lower. Always store proxy secrets (username, password, host) in the repository Secrets, not in the workflow file itself.

IP Rotation and Stability of Long Sessions

The main mistake when working with Codex through a proxy is using IPs with automatic rotation for each request within a single multi-step task. The OpenAI API supports long streaming responses and sequential calls within a single context — if the outgoing IP changes between these calls, the service may consider this suspicious activity or simply terminate the streaming connection.

The solution is sticky sessions: the proxy keeps the same IP assigned to your connection for a specified period (usually from 1 to 30 minutes, depending on the provider's settings), after which it changes to a new one. For Codex CLI, it is optimal to set the session duration to be at least as long as your longest task — if generating a large PR takes 5 minutes, the session should last at least 10-15 minutes as a buffer.

# Example of creating a sticky session through a parameter in the proxy login
# (syntax depends on the provider, typical option below)
export HTTPS_PROXY="http://user-session-abc123:password@proxy_host:port"

If tasks are launched in parallel in CI (for example, reviewing 10 pull requests at once), each parallel job should be assigned a separate session ID — this way you avoid situations where multiple processes simultaneously "pull" the same IP and hit the API rate limit prematurely.

Common Errors and How to Fix Them

Let's examine typical problems that developers encounter when setting up proxies for Codex CLI and the OpenAI API.

Error Cause Solution
403 unsupported_country_region_territory The proxy did not engage, the request went with the real IP Check environment variables via curl -x
Stream drops in the middle of the response IP changed in the middle of the session (no sticky mode) Enable sticky session, increase IP lifespan
Timeout / connection refused Incorrect port or protocol (http instead of socks5) Verify protocol and port in the proxy provider's documentation
Rate limit hits too quickly Multiple processes are using the same IP simultaneously Assign separate session IDs for each parallel process
Account received a warning about suspicious activity Datacenter IP ended up in the abuse database Switch to a residential IP with a clean reputation

Checklist Before Launching

Before entrusting Codex CLI with a production task via a proxy, go through this short list:

  • Checked the availability of api.openai.com via curl -x with your proxy
  • The geolocation of the proxy matches the country specified in the OpenAI account billing
  • Sticky session is enabled with a lifespan longer than the duration of the longest task
  • For CI/CD, proxy secrets are stored in Secrets, not in the workflow code
  • Different session IDs are used for parallel tasks, not one shared IP
  • CLI logs checked for hidden direct requests bypassing the proxy (e.g., from third-party dependencies)

Conclusion

Working with OpenAI Codex CLI from a country without official support requires not just a one-time bypass of the block, but a systematic network setup: the right type of proxy, sticky sessions for long requests, and careful integration into environment variables or client code. For local development and irregular tasks, residential IPs with a clean reputation are preferable, while for heavy CI/CD pipelines — fast and stable datacenter proxies.

If you plan to run Codex and other tools based on the OpenAI API from a region without official support, we recommend starting with residential proxies for stable CLI operation, and for large automated tasks in the cloud — with datacenter proxies, which will provide low latency and predictable costs for large volumes of API requests.