Connect Codex CLI to an AI API from Iran
Codex CLI is OpenAI's open-source coding agent for the terminal: it reads a project's code, edits files and runs commands. Instead of signing in with a ChatGPT account or an OpenAI key, you define a custom provider in ~/.codex/config.toml that points at 1xAi; the 1xai-* key is read from an environment variable, and each call's cost is taken in Toman from your account.
Setting up Codex CLI
- [01]
Create an account and a 1xAi key. Register at 1xai.ir (the app is in Persian) and confirm your email or mobile number. You spin the sign-up gift wheel (from 100,000 to 1,000,000 Toman of gift credit) once, which is enough to test the connection. Then create a 1xai-* key in the dashboard and copy it at once: a key is shown only once.
- [02]
Install Codex CLI. Install Codex CLI with npm install -g @openai/codex (on macOS, brew install --cask codex works too). Installing needs no key.
- [03]
Define the 1xAi provider in config.toml. Open ~/.codex/config.toml (create it if it is missing) and add the block [model_providers.onexai]: base_url is https://1xai.ir/v1, env_key is ONEXAI_API_KEY and wire_api is responses. At the top of the file set model_provider to onexai and model to the model name.
- [04]
Put the key in an environment variable. In the terminal, export ONEXAI_API_KEY with your 1xai-* key, and add the same line to ~/.zshrc or ~/.bashrc to keep it. The key is never written into config.toml.
- [05]
Run codex in the project folder. Run codex in the project folder and describe the task; for a task without a conversation, use codex exec followed by the task. The Toman cost of each call is recorded in the 1xAi dashboard.
Ready to copy
# https://developers.openai.com/codex/config-advanced model = "gpt-5.3-codex" model_provider = "onexai" [model_providers.onexai] name = "1xAi" base_url = "https://1xai.ir/v1" env_key = "ONEXAI_API_KEY" wire_api = "responses"
export ONEXAI_API_KEY="1xai-XXXXXXXXXXXXXXXX" echo 'export ONEXAI_API_KEY="1xai-XXXXXXXXXXXXXXXX"' >> ~/.zshrc codex codex exec "Add a unit test for the parse function in utils.py"
Recommended model
Recommended: gpt-5.3-codex. A cheaper alternative: gpt-5.1-codex-mini. Codex works only with OpenAI models. The recommended model is built for multi-file edits and running commands; for small, frequent tasks the alternative costs several times less. Every model and its Toman price is on the models page.
What it costs
Each call is billed at the model's Toman price: the vendor's list price plus a markup of 15–30% depending on the model, at the day's dollar rate (refreshed every day at 9 in the morning, Tehran time). Codex sends the whole conversation again at every step, and OpenAI caches the repeated part by itself, so most of a session's input is billed at the cheaper cache rate; writing to the cache costs nothing extra. The cost of an example session in Toman.
Common errors
- Error: wire_api = "chat" is no longer supported
- Codex no longer accepts Chat Completions; set wire_api to responses in the provider block. On 1xAi the Responses route takes OpenAI models only, so model has to be an OpenAI model too, not a claude-* id.
- Codex asks for a ChatGPT sign-in or cannot find the environment variable
- A sign-in screen means the model_provider line of config.toml was not read, or its name differs from the block [model_providers.onexai]. A missing-variable error means ONEXAI_API_KEY is empty in this terminal: check with echo $ONEXAI_API_KEY and open a new terminal.
- 401: invalid key
- The key is wrong, has expired, or you revoked it. Copy it again from the dashboard or create a new one, with no space before or after it.
- 402: insufficient balance
- The account's Toman credit is used up. A wallet top-up by the Zarinpal gateway or the Digipay gateway or card-to-card transfer with a receipt; calls work again as soon as a payment succeeds.
- Model name or routing error
- 1xAi routes by model name: claude-* to Anthropic, gemini-* to Google, deepseek-* to DeepSeek; grok-*, kimi-*, glm-*, qwen*, MiniMax-* and hy* reach the Grok, Kimi, GLM, Qwen, MiniMax and Hunyuan models; every other name goes to OpenAI. Write a model id exactly as the model list shows it.
Every error code is in the error reference.
Does Codex CLI work with Claude or Gemini?
Not through 1xAi. Codex speaks only the Responses API, and on 1xAi that route takes OpenAI models only. For Claude, use Claude Code on the native Anthropic route; the same 1xai-* key works there.
What does a Codex CLI session cost?
It depends on the size of the project and the number of steps. Each call is billed at the model's Toman price, OpenAI's list price plus a markup of 15–30% depending on the model, and cached input is cheaper. An example session is worked out at live prices on the coding-costs page. The sign-up gift wheel (from 100,000 to 1,000,000 Toman of gift credit) is enough to test the connection.
Other guides: Cursor, Claude Code, n8n, Dify, Cline, Open WebUI, LibreChat, Continue, LangChain, LlamaIndex, Vercel AI SDK, LiteLLM.
1xAi is an independent gateway. It is not affiliated with, endorsed by, or a reseller for OpenAI, Anthropic, Google, DeepSeek, xAI, Moonshot AI, Z.ai, Alibaba Qwen, MiniMax or Tencent Hunyuan. Requests are relayed upstream rather than answered by models of our own, so each provider's own usage policies apply to what their models will and will not do, and their model availability is theirs to change. Current reachability is published on the status page.