Complete Guide to Codex CLI Installation and API Configuration (Third-Party Gateway Support)
A step-by-step guide on installing Codex CLI and its VS Code extension, including how to configure auth.json and config.toml for OpenAI and third-party API providers.
Codex CLI is a powerful command-line tool that integrates the capabilities of large language models directly into your terminal. Whether you are using the official OpenAI API or a third-party API gateway, proper configuration is key to ensuring stable operation.
π οΈ Prerequisites
Before starting, ensure your system has the following environments installed:
- Node.js: Version 22+
- npm: Version 10+
π¦ Step 1: Install Codex CLI
Depending on your operating system, choose the corresponding installation method:
π» Windows
- Environment Setup: It is recommended to install Git Bash (for a Unix-like terminal experience) and Node.js LTS.
- Execution: Run the following command in CMD or PowerShell:
npm install -g @openai/codex - Verification:
codex --version
π macOS
- Execution:Note: If you encounter permission issues, prepend the command with
npm install -g @openai/codexsudo. - Verification:Optional: You can also install via Homebrew $\rightarrow$
codex --versionbrew install codex.
π§ Linux
- Environment Setup: Install Node.js and npm based on your distribution (e.g.,
sudo apt install nodejs npm). - Execution:
sudo npm install -g @openai/codex - Verification:
codex --version
βοΈ Step 2: Configure API Keys and Providers
All configurations for Codex CLI are stored in a hidden folder named .codex in your user directory.
1. Locating the Directory
- Windows:
C:\Users\YourUsername\.codex(If not visible, enable “Show hidden items” in File Explorer). - macOS/Linux:
~/.codex
If the folder does not exist, create it manually:
mkdir -p ~/.codex
2. Configuring auth.json (Authentication)
Create an auth.json file in this directory to store your API Key.
File Content:
{
"OPENAI_API_KEY": "your-actual-api-key"
}
3. Configuring config.toml (Models and Gateways)
Create a config.toml file to define the model provider and request path.
Config Template:
model_provider = "ProviderName"
model = "SpecificModelName"
model_reasoning_effort = "high"
disable_response_storage = true
preferred_auth_method = "apikey"
[model_providers.ProviderName]
name = "ProviderName"
base_url = "https://actual-gateway-url/v1"
wire_api = "responses"
β οΈ Important: Ensure that the value of model_provider matches the section name [model_providers.xxx] exactly.
Applying Changes: After modifying the configuration, please restart your terminal and relaunch codex for the changes to take effect.
π Step 3: Integrate with VS Code Extension
After configuring the .codex folder, you can enjoy a more intuitive UI experience:
- Search for and install the Codex extension in the VS Code Extension Marketplace.
- Once installed, the extension will appear in the sidebar.
- The plugin automatically reads the configuration from
~/.codex, allowing you to call AI capabilities directly within your editor.
π‘ Summary
By following the path of npm install $\rightarrow$ configuring the .codex directory $\rightarrow$ linking the VS Code plugin, you now have a highly efficient AI-powered programming environment. Regardless of how the API changes, you can simply update config.toml to switch providers quickly.