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

# Codex CLI

> OpenAI's official Codex CLI connected to FortAPI

[Codex CLI](https://github.com/openai/codex) is OpenAI's official command-line coding assistant, analogous to Claude Code. It speaks the OpenAI-compatible protocol, so **`OPENAI_BASE_URL`** + **`OPENAI_API_KEY`** are all you need to route it through FortAPI.

## Prerequisites

<Steps>
  <Step title="FortAPI account + API key">
    Console → **Tokens** → **Create token**. See [API keys & security](/en/authentication).
  </Step>

  <Step title="Node.js ≥ 18">
    ```bash theme={null}
    node --version
    ```

    Install from [nodejs.org](https://nodejs.org) if needed.
  </Step>
</Steps>

## Install Codex CLI

```bash theme={null}
npm install -g @openai/codex
```

Confirm:

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

## Configure FortAPI

Codex supports two ways to configure the endpoint: **env vars** or **`~/.codex/config.toml`**. The TOML file is the longer-lived path.

### Method 1: `config.toml` (recommended)

Edit `~/.codex/config.toml` (or `%USERPROFILE%\.codex\config.toml` on Windows). Create it if missing:

```toml theme={null}
model_provider = "fortapi"
model = "your-model-name"  # see the pricing page on the main site

[model_providers.fortapi]
name = "FortAPI"
base_url = "https://www.fortapi.com/v1"
wire_api = "chat"
env_key = "FORTAPI_API_KEY"
```

Then export the key once (or persist it to `~/.zshrc` / `~/.bashrc`):

<Tabs>
  <Tab title="macOS / Linux">
    ```bash theme={null}
    export FORTAPI_API_KEY="sk-your-key"
    ```
  </Tab>

  <Tab title="Windows (PowerShell)">
    ```powershell theme={null}
    [Environment]::SetEnvironmentVariable("FORTAPI_API_KEY", "sk-your-key", "User")
    ```
  </Tab>
</Tabs>

<Note>
  `wire_api = "chat"` means classic `/v1/chat/completions` — the most reliable choice. You can try `"responses"` for the newer Responses API, but not every model on FortAPI supports it yet.
</Note>

### Method 2: env vars only

If you'd rather skip the config file:

<Tabs>
  <Tab title="macOS / Linux">
    ```bash theme={null}
    export OPENAI_BASE_URL="https://www.fortapi.com/v1"
    export OPENAI_API_KEY="sk-your-key"
    codex
    ```
  </Tab>

  <Tab title="Windows (PowerShell)">
    ```powershell theme={null}
    $env:OPENAI_BASE_URL = "https://www.fortapi.com/v1"
    $env:OPENAI_API_KEY  = "sk-your-key"
    codex
    ```
  </Tab>
</Tabs>

<Warning>
  In the OpenAI-compatible protocol, `base_url` / `OPENAI_BASE_URL` **must** end in `/v1`. That's the opposite of Anthropic's convention.
</Warning>

## First call

```bash theme={null}
cd ~/some-project
codex
```

In the interactive prompt:

```
> In one sentence, introduce yourself.
```

A normal reply plus a matching entry in FortAPI Console → **Logs** = success.

## Switching models

Inside a session:

```
/model your-model-name
```

Or change the default by editing `model = "..."` at the top of `config.toml`. See the [Pricing page](https://www.fortapi.com) for the full list.

## Troubleshooting

<AccordionGroup>
  <Accordion title="401 Unauthorized">
    * `OPENAI_API_KEY` (or `FORTAPI_API_KEY`) must be your **FortAPI** token (`sk-...`), not OpenAI's `sk-proj-...`.
    * Confirm the token is enabled and the model is in its allowlist via Console → **Tokens**.
  </Accordion>

  <Accordion title="404 / model not found">
    * Typo. Model IDs on FortAPI match OpenAI's official names: `gpt-5`, `gpt-5-mini`, `o4-mini`, `gpt-4.1`, etc.
    * `OPENAI_BASE_URL` must end in `/v1`.
  </Accordion>

  <Accordion title="'unsupported wire_api' error">
    Set `wire_api = "chat"` in `config.toml`. The `"responses"` protocol is only supported by some newer models.
  </Accordion>

  <Accordion title="Tool calling / function calling errors">
    FortAPI passes tool / function calls through unchanged. If Codex still errors, try the same prompt against OpenAI's official endpoint first to rule out a prompt issue.
  </Accordion>

  <Accordion title="Keeping both the official account and FortAPI">
    Define two providers in `config.toml` and use `/provider <name>` to switch inside a session, or install [CC Switch](/en/integrations/ccswitch).
  </Accordion>
</AccordionGroup>

## Advanced: running alongside Claude Code

A common setup is Codex (for OpenAI-strong workloads) plus Claude Code (for long-context / code reasoning). They don't interfere — configure each per [Claude Code](/en/integrations/claude-code) and this page. The same FortAPI key works for both.
