> ## Documentation Index
> Fetch the complete documentation index at: https://docs.flatkey.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Use Flatkey with the OpenAI Codex CLI Terminal Agent

> Route OpenAI Codex CLI traffic through Flatkey to access GPT models at up to 50% off official pricing. Set two environment variables to get started.

OpenAI's Codex CLI is an AI coding assistant that runs in your terminal. By setting two environment variables, you can route all Codex CLI requests through Flatkey and pay Flatkey's discounted rates — typically 40%+ savings on GPT model usage.

## Prerequisites

* Codex CLI installed: `npm install -g @openai/codex`
* A Flatkey account with an API key — [get one here](https://console.flatkey.ai/sign-up)

## Configuration

Set the following environment variables before running Codex CLI:

```bash theme={null}
export OPENAI_BASE_URL="https://router.flatkey.ai/v1"
export OPENAI_API_KEY="sk-fk-your-flatkey-key"
```

Or add them to your shell profile (`~/.bashrc`, `~/.zshrc`) to make the setting permanent:

```bash ~/.zshrc theme={null}
export OPENAI_BASE_URL="https://router.flatkey.ai/v1"
export OPENAI_API_KEY="sk-fk-your-flatkey-key"
```

## Configure with CC Switch

[CC Switch](https://ccswitch.io) is a third-party desktop configuration manager for coding assistants. You can also download it from the [official GitHub repository](https://github.com/farion1231/cc-switch).

1. Open CC Switch and select **Codex**.
2. Add a **Custom Provider** named `Flatkey`.
3. Set **Base URL** to `https://router.flatkey.ai/v1`.
4. Select the **OpenAI Responses** protocol.
5. Enter your Flatkey API key (`sk-fk-...`) and choose a supported GPT model from the [Model Directory](/reference/model-list).
6. Activate the provider, fully quit Codex CLI, and start it again.
7. Run a small command, then confirm the request in [Usage Logs](https://console.flatkey.ai/usage-logs/common).

<Note>
  Use **Custom Provider** if your CC Switch version does not include a Flatkey preset. Flatkey supports the Responses route directly, so you do not need CC Switch Local Routing for this setup.
</Note>

## Running Codex CLI

Once the environment variables are set, use Codex normally:

```bash theme={null}
codex "Refactor this function to use async/await"
```

or in interactive mode:

```bash theme={null}
codex
```

All requests are routed through Flatkey and billed against your prepaid balance at Flatkey's discounted rates.

## Choosing a model

By default, Codex CLI uses a GPT model. You can specify a different model with the `--model` flag:

```bash theme={null}
codex --model gpt-4o "Write unit tests for this module"
codex --model gpt-4o-mini "Add docstrings to this file"  # cheaper for simple tasks
```

Any GPT model available in the [Model Directory](/reference/model-list) works with Codex CLI through Flatkey.

## Verifying the setup

After running a Codex command, check [Usage Logs](https://console.flatkey.ai/usage-logs/common) — you should see the request logged with the model and token counts. If no request appears, confirm the active provider and `OPENAI_BASE_URL` in the current shell session.

<Tip>
  Use the \$200 top-up tier for the best effective rate on sustained Codex usage — the stacked bonus brings costs to as low as 50% of official GPT pricing.
</Tip>

## Troubleshooting

| Issue                               | Fix                                                                       |
| ----------------------------------- | ------------------------------------------------------------------------- |
| `Authentication error`              | Verify `OPENAI_API_KEY` is set to your Flatkey key (starts with `sk-fk-`) |
| `Model not found`                   | Check the model ID in the [Model Directory](/reference/model-list)        |
| `Insufficient balance`              | Top up your balance at [console.flatkey.ai](https://console.flatkey.ai)   |
| Requests not appearing in dashboard | Confirm `OPENAI_BASE_URL` is set in the active shell                      |
