15% off all models 🎉 Every model at 85% of OpenRouter list price.Browse models →

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/v1

Copy 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 | iex

You can also install it using npm:

npm install --global @openai/codex

If 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 | sh

You can also install it using npm:

npm install --global @openai/codex

Once the installation is complete, close the terminal and reopen it.

2. Confirm that Codex CLI is installed

Run in terminal:

codex --version

If 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.toml

You can run:

mkdir -p ~/.codex
nano ~/.codex/config.toml

Windows PowerShell:

C:\Users\YOUR_USERNAME\.codex\config.toml

You 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-mini is an example model only. Please refer to the ApiFlux Models page 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_KEY

To 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_KEY

macOS usually uses zsh. To persist the variable, add it to ~/.zshrc:

echo 'export APIFLUX_API_KEY="YOUR_APIFLUX_API_KEY"' >> ~/.zshrc
source ~/.zshrc

Linux 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:

codex

After launching the Codex CLI, you can enter a simple question:

Explain what ApiFlux is in one sentence

After 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_KEY

macOS/Linux check:

echo $APIFLUX_API_KEY

If 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:

  1. Confirm that the key is valid in the ApiFlux console
  2. Reset APIFLUX_API_KEY
  3. 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.

References