Overview
~/.codex/ (config.toml and auth.json).
Integrating with APIYI comes down to one sentence:
Replace OpenAI’s endpoint with APIYIAPIYI is an OpenAI-compatible interface (transparent proxy) — configure it once, and the desktop app, extensions, and terminal all work.
🔁 One Config, Three Surfaces
~/.codex/ — set it once⚡ Latest Models
gpt-5.6-sol / gpt-5.5 / grok-4.5, plus other models💰 Pay As You Go
🪟 Cross-Platform
~/.codex/config.toml and put your Key in ~/.codex/auth.json. Both the desktop app and IDE extensions rely on these files — so this guide leads with config files, not environment variables.1. Prerequisites: Get Your APIYI Key
Sign up / Log in to APIYI
Create an API Key
Copy the Key
sk-***) and keep it safe — you’ll paste it into the config file.Pick Your Surface
All three surfaces use exactly the same config — pick whichever fits your workflow:🖥️ Desktop App
🧩 IDE Extension
⌨️ CLI
2. Core Config (Recommended: Config Files, Not Env Vars)
Three ways below — pick one. Recommended order: hand-write config files (most reliable) → visual → environment variables.Option 1 · Hand-write auth.json + config.toml (recommended, most reliable)
Open Codex’s config directory (create it if missing) and add/edit two files inside:
- 🪟 Windows
- Mac / Linux
%USERPROFILE%\.codex\ (i.e. C:\Users\YourName\.codex\).Open it in File Explorer.auth.json — put your Key here:
config.toml — point the model provider at APIYI:
For a brand-new file, paste the content below. For an existing file, add the “global keys” at the very top and append the [model_providers.apiyi] block at the very bottom (reason in the tip below).
How to safely edit an existing config.toml (backup + merge best practice)
How to safely edit an existing config.toml (backup + merge best practice)
model / model_provider / preferred_auth_method lines at the very top, and append the [model_providers.apiyi] block at the very bottom. Leave everything else as-is.Step 3: Just want to test without touching your main config? Use a profile: create ~/.codex/apiyi.config.toml with the content above, then run codex --profile apiyi — fully isolated (see Advanced).base_url: alwayshttps://api.apiyi.com/v1— it must include/v1, or you’ll get 404s.experimental_bearer_token: puts the Key directly in the provider block and sends it as a Bearer token. This is the one form that reliably works across the desktop app, IDE extension, and CLI — no environment variable involved.- A provider’s auth fields are mutually exclusive — pick exactly one:
experimental_bearer_token(Key in the config, recommended) /env_key(reads the Key from the launching process’s environment variable — note it does not fall back toauth.json, and the desktop app cannot see variables exported in your terminal) /requires_openai_auth(reuses the official login state inauth.json). If you followed an older version of this guide that combinedenv_key+requires_openai_auth, switch to the current form. wire_api = "responses": Codex’s default and preferred protocol, supported by APIYI. If a specific model returns 404 / unknown endpoint, switch it to"chat"as a fallback (see Advanced).- Do not hard-code absolute paths like
C:\Users\xxx\.codex\...in this file — it breaks across machines.
Option 2 · cc-switch Visual Config (GUI, no hand-editing)
If you’d rather not edit files by hand, use CC Switch — a GUI that writes APIYI’s URL, Key, and model into the Codex config with a few clicks. It also manages Claude Code, Codex, Gemini CLI, and more in one place, with one-click switching, and handles the backup/merge above for you — a good first choice for beginners. See CC Switch Visual Config. Once set, Codex’s desktop app / extension / CLI all pick up the config automatically.Option 3 · Environment Variables (optional, fiddly, not recommended)
Just want a quick terminal test? Expand for the env-var method (not for long-term use)
Just want a quick terminal test? Expand for the env-var method (not for long-term use)
OPENAI_BASE_URL / OPENAI_API_KEY environment variables:3. Using Each Surface (Desktop First)
Once~/.codex/ is set up, pick any surface below. Restart the program after changing config (Codex reads config only at startup).
1. Codex Desktop App (most recommended)
- Install and open the Codex desktop app.
- On first launch, choose the auth method: select apikey (not chatgpt login).
- In the model / provider selector, choose the
apiyiprovider and your target model (e.g.gpt-5.4). - Restart the app to apply.
- Run a minimal task to verify (see Section 4).
2. IDE Extension (VSCode / Cursor)
- Open the extensions marketplace (in VSCode press
Ctrl+Shift+X/Cmd+Shift+X), searchCodex — OpenAI's coding agent, and clickInstall. - After install, a Codex icon appears in the sidebar — click it to open the panel.
- On first open, answer three prompts: ① auth method — select apikey; ② Key source — choose “config file / environment variable”; ③ enable
AGENTS.md(recommended). - Restart the editor to apply.
- Run a minimal task in the Codex panel to verify.
3. CLI
Install the official CLI globally (requires Node.js 18+):4. Minimal Verification
After configuring and restarting, enter a minimal task in any surface:5. Models (APIYI Recommendations)
Set these inconfig.toml’s model field, or switch at runtime:
/v1/responses protocol support — keep wire_api = "responses" as-is in Codex and just switch model to grok-4.5; Codex’s agent features (tool calls, reasoning items, etc.) all run over the native protocol. The responses endpoint is verified on APIYI with grok-4.5; other Grok models share the same architecture and are expected to behave identically — if one returns 404, use the fallback in Section 6. See the Grok API Guide.Compare with Claude / Gemini: on APIYI these two only run in OpenAI-compatible chat mode — no responses endpoint — so in Codex you must fall back to wire_api = "chat". Since Codex’s agent scenarios are designed around the responses protocol, chat mode can show incompatibilities in tool calling and a degraded experience. For coding with Claude / Gemini, use their native tools instead (Claude Code / Gemini CLI).glm-5.2. Just change config.toml’s model field (or -m at runtime) to the target model ID.4 Ways to Switch Models
① Specify at launch (CLI):/model in the interactive panel and follow the prompts.
④ Configure a default model (permanent): edit ~/.codex/config.toml, change model, save, and restart:
6. Advanced Configuration
Custom system prompts (instructions.md)
Custom system prompts (instructions.md)
~/.codex/instructions.md to define coding style, output language, and project conventions, e.g.:Project-level AGENTS.md
Project-level AGENTS.md
codex /init in a project to generate AGENTS.md recording structure and conventions. To make Codex respond in a specific language by default, add:Protocol fallback: switch wire_api to chat
Protocol fallback: switch wire_api to chat
wire_api = "responses" is Codex’s default and preferred protocol, and most models work directly. If a model returns 404 / unknown endpoint, change that provider’s wire_api to "chat" (uses /chat/completions) and retry.Multiple configs (profiles)
Multiple configs (profiles)
<name>.config.toml under ~/.codex/ (e.g. openai.config.toml for the official setup), then switch at runtime with codex --profile <name>. Handy for jumping between APIYI and other providers.Common flags
Common flags
7. Troubleshooting
1. Missing environment variable: OPENAI_API_KEY (most common in the desktop app / extension)
1. Missing environment variable: OPENAI_API_KEY (most common in the desktop app / extension)
auth.json + config.toml correctly and restarted the app, yet it still shows Missing environment variable: OPENAI_API_KEY — the cause is env_key = "OPENAI_API_KEY" in the provider block (the form an older version of this guide used).env_key means read the Key from the environment variables of the process that launched Codex — it does not fall back to auth.json (which only serves OpenAI’s official login state). And when the desktop app / IDE is launched from the Dock or a launcher, it does not inherit variables you exported in a terminal (export in .zshrc has no effect on GUI apps), so no amount of restarting will make the variable appear.Fix (recommended): edit ~/.codex/config.toml, remove env_key from the provider block (and requires_openai_auth if present), and put the Key directly in the config:env_key): set the variable system-wide — on macOS run launchctl setenv OPENAI_API_KEY "sk-your-key" then restart the app (must be re-run after reboot); on Windows run setx OPENAI_API_KEY "sk-your-key" then restart the app. For CLI-only usage, an export in your shell profile is enough.2. Verify auth.json / config.toml path and contents
2. Verify auth.json / config.toml path and contents
auth.jsonmust be valid JSON, withOPENAI_API_KEYset to your realsk-Key.config.tomlmust parse as valid TOML (mind quotes and indentation).- Paths: Windows
%USERPROFILE%\.codex\, Mac/Linux~/.codex/.
3. Confirm the Key is valid with available credit
3. Confirm the Key is valid with available credit
4. Confirm base_url includes /v1
4. Confirm base_url includes /v1
/v1. Correct: https://api.apiyi.com/v1. Then check local proxy and DNS.5. Restart after every config change
5. Restart after every config change
auth.json / config.toml, always restart the program.6. Still unstable: switch wire_api to chat
6. Still unstable: switch wire_api to chat
responses protocol, change that provider’s wire_api to "chat" and retry.8. FAQ
Why does Codex work with APIYI?
Why does Codex work with APIYI?
https://api.apiyi.com/v1 and https://api.openai.com/v1 are interchangeable in request/response format. Replacing the Base URL is enough.Why does a simple hello consume tens of thousands of input tokens?
Why does a simple hello consume tens of thousands of input tokens?
AGENTS.md, relevant source), and sends them as context along with your prompt. So even a one-word hello can cost thousands of input tokens.How to reduce it?- Test minimal tasks in an empty directory or a very small project, so the context stays small.
- Give a specific small task and name the exact file (e.g. “only look at
app.py, add a hello endpoint”) to limit what Codex scans. - Use a cheaper model (e.g.
gpt-5.4-mini) for these throwaway checks.
`command not found: codex`
`command not found: codex`
npm bin -g is on your PATH.Invalid API Key (401 / Invalid Key)
Invalid API Key (401 / Invalid Key)
- Make sure you’re using an APIYI Key (starts with
sk-), not an OpenAI key. - Confirm the Key in
auth.jsonis correct with no stray spaces. - Restart the program after changing config.
Connection error / timeout / 404
Connection error / timeout / 404
/v1. Correct: https://api.apiyi.com/v1. Then check your local proxy and DNS.Which models are supported?
Which models are supported?
- OpenAI series: ✅ fully supported (recommended
gpt-5.6-sol/gpt-5.6-terra/gpt-5.6-luna/gpt-5.5/gpt-5.4). - Grok series: ✅ native responses protocol support —
grok-4.5works without touchingwire_api; see the Grok API Guide. - Other OpenAI-compatible models: supported by APIYI, e.g.
glm-5.2— just change themodelfield. - Note: Claude / Gemini on APIYI only offer OpenAI-compatible chat mode — no responses endpoint — so in Codex you must switch
wire_apito"chat", and agent behaviors like tool calling may hit incompatibilities. For Claude / Gemini-based coding, use their native tools (e.g. Claude Code / Gemini CLI).
Desktop app / extension not working?
Desktop app / extension not working?
~/.codex/config.toml + auth.json, not environment variables. Confirm those two files are correct, the auth method is set to apikey, and restart the program.Suitable for production?
Suitable for production?
- CLI / app: best for dev-time productivity.
- Production: prefer direct API calls (more control, monitoring, gradual rollout).
How do I uninstall or disable the APIYI setup?
How do I uninstall or disable the APIYI setup?
~/.codex/config.toml and auth.json (for the desktop app / extension, manage it in their respective UIs).9. Summary
The whole integration is one sentence:Replace OpenAI’s endpoint with APIYIThe core is configuring
~/.codex/ once: put the Key in auth.json, and point base_url to https://api.apiyi.com/v1 in config.toml. After that, the desktop app, IDE extensions, and CLI all work. Everything else — model picking, prompts, instructions.md, AGENTS.md — is just polish.