Connect Codex CLI to ApiFlux
Install Codex CLI, configure ApiFlux as a custom model provider, and verify a routed request.
This guide connects Codex CLI to ApiFlux. After setup, Codex sends model requests through ApiFlux, where you can review usage and logs in one place.
Who this guide is for
Use this guide if you want to run Codex CLI in a terminal while managing model access, balance, logs, and model selection through ApiFlux.
If you have not set up a terminal or Node.js, start here:
Prepare your terminal and development environment
Before you start
Before you start, make sure you have:
- An ApiFlux API key
- A model ID supported by ApiFlux
- ApiFlux Base URL:
https://apiflux.ai/v1Copy the model ID from the ApiFlux Models page instead of guessing it.
1. Install Codex CLI
Windows
Open PowerShell and run:
irm https://chatgpt.com/codex/install.ps1 | iexYou can also install it using npm:
npm install --global @openai/codexIf using WSL, install using the macOS/Linux command in the WSL terminal.
macOS/Linux
Open a terminal and run:
curl -fsSL https://chatgpt.com/codex/install.sh | shYou can also install it using npm:
npm install --global @openai/codexOnce the installation is complete, close the terminal and reopen it.
2. Confirm that Codex CLI is installed
Run in terminal:
codex --versionIf the command prints a version number, Codex CLI is installed.
If you see command not found or “not recognized as an internal or external command,” reopen the terminal. If the error remains, run the installation command again.
3. Open the Codex configuration file
The personal Codex configuration file is stored in your user directory.
macOS/Linux/WSL:
~/.codex/config.tomlYou can run:
mkdir -p ~/.codex
nano ~/.codex/config.tomlWindows PowerShell:
C:\Users\YOUR_USERNAME\.codex\config.tomlYou can run:
New-Item -ItemType Directory -Force "$env:USERPROFILE\.codex"
notepad "$env:USERPROFILE\.codex\config.toml"You can also open the file with VS Code.
4. Add ApiFlux Provider
Add the following configuration to config.toml:
model = "YOUR_MODEL_ID"
model_provider = "apiflux"
[model_providers.apiflux]
name = "ApiFlux"
base_url = "https://apiflux.ai/v1"
env_key = "APIFLUX_API_KEY"Replace YOUR_MODEL_ID with the model ID shown in the ApiFlux Models page.
Example:
model = "gpt-5.4-mini"
model_provider = "apiflux"
[model_providers.apiflux]
name = "ApiFlux"
base_url = "https://apiflux.ai/v1"
env_key = "APIFLUX_API_KEY"[!NOTE]
gpt-5.4-miniis an example model only. Please refer to the ApiFluxModelspage for the actual available models.
5. Set API key environment variable
Provide the API key through an environment variable instead of writing it directly in config.toml.
Windows PowerShell
For the current terminal session:
$env:APIFLUX_API_KEY="YOUR_APIFLUX_API_KEY"Verify the variable:
echo $env:APIFLUX_API_KEYTo persist the variable for future sessions:
[Environment]::SetEnvironmentVariable("APIFLUX_API_KEY","YOUR_APIFLUX_API_KEY","User")After setting persistent environment variables, close PowerShell and reopen it.
macOS/Linux
For the current terminal session:
export APIFLUX_API_KEY="YOUR_APIFLUX_API_KEY"Verify the variable:
echo $APIFLUX_API_KEYmacOS usually uses zsh. To persist the variable, add it to ~/.zshrc:
echo 'export APIFLUX_API_KEY="YOUR_APIFLUX_API_KEY"' >> ~/.zshrc
source ~/.zshrcLinux usually uses bash. To persist the variable, add it to ~/.bashrc:
echo 'export APIFLUX_API_KEY="YOUR_APIFLUX_API_KEY"' >> ~/.bashrc
source ~/.bashrc[!WARNING] Do not display shell configuration files containing real API keys in public screenshots, screen recordings, or repositories.
6. Start the Codex CLI and initiate a test request
Run in terminal:
codexAfter launching the Codex CLI, you can enter a simple question:
Explain what ApiFlux is in one sentenceAfter Codex returns a response, open Logs in the ApiFlux console and confirm that the request appears.
FAQ
API key not found
Windows PowerShell check:
echo $env:APIFLUX_API_KEYmacOS/Linux check:
echo $APIFLUX_API_KEYIf there is no output, reset the environment variables.
401 or Unauthorized
Usually caused by:
- The API key is incorrect
- The key was deleted or expired
- The copied key contains extra spaces
- The current terminal session does not have the environment variable
To fix it:
- Confirm that the key is valid in the ApiFlux console
- Reset
APIFLUX_API_KEY - Close and reopen the terminal and test again
Model not found
Please check the configuration:
model = "YOUR_MODEL_ID"The model ID must come from the ApiFlux Models page.