Back to blog
tutorialcodex-clitooling

Pointing OpenAI Codex CLI at a third-party gateway

ApiLink Team··6 min read简体中文

OpenAI’s Codex CLI is a useful tool — fast, agentic, ships with a good default model. The only awkward thing is that it’s wired to api.openai.com by default and the auth model assumes you have an OpenAI key. This post walks through pointing it at a third-party gateway, including ApiLink. The mechanism is the same for any gateway that implements the Responses API.

Why do this

Prerequisites

  1. Codex CLI installed (npm install -g @openai/codex or via your package manager).
  2. An API key from a gateway that supports the OpenAI Responses API at /v1/responses. ApiLink does; OpenRouter does not (as of 2026-05).
  3. The gateway’s base URL — for ApiLink it’s https://apilink.io/v1.

The actual config

Codex respects two environment variables: OPENAI_API_KEY and OPENAI_BASE_URL. Set them and Codex treats your gateway exactly like OpenAI:

bash
export OPENAI_API_KEY="al-yourgatewaykey"
export OPENAI_BASE_URL="https://apilink.io/v1"

codex "refactor the User model to remove the legacy isAdmin flag"

That’s it. Codex will hit https://apilink.io/v1/responses instead of the OpenAI default. Streaming, tool use, and the rich CLI UI all work unchanged.

Picking the model

By default Codex uses gpt-5-codex. To point it at a different model you have two options:

bash
# One-off override
codex --model claude-sonnet-4-5 "your prompt"

# Persistent — set in your shell profile
export CODEX_MODEL="claude-sonnet-4-5"
Most gateways accept the upstream model ID directly ( claude-sonnet-4-5, gemini-2.5-pro, etc.). Check your gateway’s /v1/models endpoint for the list of accepted IDs.

Verifying it’s actually using your gateway

Easy test: make a request and check the gateway’s usage dashboard. If the call shows up there with the right model and roughly the right token count, the routing works.

Harder test (recommended): set DEBUG=codex*before running Codex. You’ll see the HTTP requests fly past, complete with the target host. Make sure none of them are reachingapi.openai.com— Codex occasionally has backwards-compat code paths that don’t respect the env vars for certain sub-commands.

Same trick for other developer tools

The OpenAI-compatible base-URL pattern works for many of the same tools:

ToolHow to point it elsewhere
CursorSettings → Models → Override OpenAI Base URL
Aider--openai-api-base flag or OPENAI_API_BASE env var
Cline (VS Code)Provider: OpenAI Compatible → Base URL field
Continue.devEdit config.json, set apiBase under the openai provider
Open WebUISettings → Connections → Add OpenAI API endpoint
LiteLLM proxy usersAlready abstracted — set the gateway as one of the model providers

Common gotchas

  1. Model name mismatches.Codex’s default model name (gpt-5-codex) is OpenAI-specific. If you don’t override the model, you’ll get a 404 from gateways that don’t resell that model.
  2. Rate limits per model. Codex inner loops can hit dozens of requests per task. If your gateway has per-model RPM limits, raise them before scaling Codex usage.
  3. Responses API parity. Not every gateway implements /v1/responses. If yours doesn’t, Codex will fail with a 404. The chat-completions endpoint alone is not enough for Codex.
  4. Auth header format. Some gateways want Authorization: Bearer xxx; others want a custom header. Standard OpenAI clients always send the Bearer form — if your gateway needs something else, Codex won’t work without a proxy in between.

Closing

Codex CLI plus a multi-model gateway is one of the most underrated developer setups of 2026. You get the agentic UX OpenAI invested in, but with the freedom to route to whatever model is best for the task. Spend caps, audit logs, and a single bill come along for free.

ApiLink is one gateway that implements /v1/responsesend-to-end (streaming, tool calls, the works). If you’re shopping, try ours; if you’re already on another gateway and Codex isn’t working, the most likely cause is a missing Responses API.

About ApiLink
ApiLink is an OpenAI-compatible gateway for GPT, Claude, Gemini, DeepSeek and more. One key, transparent streaming-safe billing, RMB invoicing for China-based teams.
Learn more →
Keep reading
ApiLink vs OpenRouter vs ZenMux: an honest gateway comparison
Three AI gateways, side by side. Where each one wins, where each one loses, and the honest answer about using more than one.
Using Claude/GPT/Gemini from China: a compliance checklist
Payment, invoicing, forex, data residency — every wall Chinese teams hit a quarter into using OpenAI or Anthropic, with a concrete checklist.
Anthropic prompt cache: when the 25% write premium actually pays back
Anthropic's prompt cache is 90% cheaper — on reads. The catch is the 25% write premium. Here's the math on when that pays off.