Blog

Codex API Key Setup Guide: Get, Configure, and Troubleshoot

Most developers prefer API Keys for calling AI services — they're flexible, programmable, and don't depend on browser logins. But when getting started with Codex, many tutorials only cover the ChatGPT username-and-password flow, leaving users confused about how API Keys work. This article explains both authentication methods and their differences, and shows how TeamoRouter's unified API Key approach simplifies everything.

Why Many Developers Prefer API Keys over ChatGPT Login

API Keys offer distinct advantages over ChatGPT account login:

  • Programmability: CI/CD pipelines, scripts, and automated tasks can't use a browser login to call APIs.
  • Usage-based billing: Know exactly how much each request costs, with no risk of subscription expiration.
  • Multi-tool sharing: One API Key works across Codex CLI, ChatGPT App (custom endpoint), IDE plugins, and more.
  • Team collaboration: Multiple developers share one billing pool for centralized management.

However, getting an API Key directly from OpenAI faces two real-world hurdles: international payment (requires a foreign credit card) and network restrictions from certain regions.

Two Authentication Methods: ChatGPT Login vs API Key

Aspect ChatGPT Login API Key
Setup codex login with OAuth flow Set OPENAI_API_KEY env var
Applicable entry points CLI (default), Web, App CLI (explicit), custom scripts
Payment ChatGPT Plus/Pro subscription ($20-$200/month) API usage-based billing
Pros Works out of the box, no Key management Flexible, programmable, shareable
Cons Browser-dependent login, OAuth may hang Needs env var config, Key security
Access in restricted regions Needs proxy for OAuth Needs API gateway

ChatGPT Login Method

bash
codex
# Browser opens for OAuth authorization
# Codex automatically gets the access token after login

API Key Method

bash
export OPENAI_API_KEY="sk-xxxxxxxxxxxx"
export OPENAI_BASE_URL="https://api.teamorouter.com/v1"

codex

Who API Key Suits

  • Automation users: Integrating Codex into CI/CD, scripts, or batch jobs
  • Teams: Multiple developers sharing one Key and billing pool
  • Cross-tool users: Sharing API quota across Codex CLI, IDE plugins, and custom apps
  • Cost control: Need precise usage-based billing without subscription lock-in

How to Configure Environment Variables

macOS / Linux

bash
# Temporary (current terminal session)
export OPENAI_API_KEY="sk-teamo-xxxxxxxx"
export OPENAI_BASE_URL="https://api.teamorouter.com/v1"

# Permanent (write to shell profile)
echo 'export OPENAI_API_KEY="sk-teamo-xxxxxxxx"' >> ~/.zshrc
echo 'export OPENAI_BASE_URL="https://api.teamorouter.com/v1"' >> ~/.zshrc
source ~/.zshrc

Windows PowerShell

powershell
# Temporary
$env:OPENAI_API_KEY="sk-teamo-xxxxxxxx"
$env:OPENAI_BASE_URL="https://api.teamorouter.com/v1"

# Permanent (current user)
[Environment]::SetEnvironmentVariable("OPENAI_API_KEY", "sk-teamo-xxxxxxxx", "User")
[Environment]::SetEnvironmentVariable("OPENAI_BASE_URL", "https://api.teamorouter.com/v1", "User")

Using a .env File (Recommended)

Create a .env file for project-level configuration:

bash
OPENAI_API_KEY=sk-teamo-xxxxxxxx
OPENAI_BASE_URL=https://api.teamorouter.com/v1

Then load it in the terminal:

bash
export $(cat .env | xargs)
codex

How to Configure the Base URL

Codex CLI uses the OPENAI_BASE_URL environment variable to determine the API endpoint. The default is https://api.openai.com/v1. For users in restricted networks, point it at TeamoRouter:

bash
export OPENAI_BASE_URL="https://api.teamorouter.com/v1"

Or inline with the codex command:

bash
OPENAI_BASE_URL="https://api.teamorouter.com/v1" OPENAI_API_KEY="sk-teamo-xxxxxxxx" codex

Note: Whether OPENAI_BASE_URL should include /v1 depends on the tool's protocol implementation. Codex CLI uses the Responses API (/v1/responses), so the baseUrl should include the /v1 path.

Common Errors and Troubleshooting

invalid API Key

bash
Error: 401 Unauthorized
{
  "error": {
    "message": "Incorrect API key provided",
    "type": "invalid_request_error"
  }
}

Check:

  • Verify the env var is set: echo $OPENAI_API_KEY
  • Ensure no extra whitespace or newlines around the Key
  • Confirm the API Key hasn't expired

insufficient balance

bash
Error: 429 Too Many Requests
{
  "error": {
    "message": "You exceeded your current quota",
    "type": "insufficient_quota"
  }
}

Check:

  • Log into the TeamoRouter console and check your balance
  • Ensure the account linked to the API Key has sufficient credits

model no permission

bash
Error: 403 Forbidden
{
  "error": {
    "message": "The model `gpt-4o` is not available for your API key",
    "type": "access_terminated"
  }
}

Check:

  • Confirm the API Key has permission for the requested model
  • Check the model authorization list in the TeamoRouter console

baseUrl misconfiguration

bash
Error: 404 Not Found

Check:

  • Verify OPENAI_BASE_URL includes the /v1 path
  • Ensure no trailing slash at the end of the URL

FAQ

Can I use the API credits that come with ChatGPT Plus?

No. ChatGPT Plus subscription ($20/month) and API usage billing are completely independent systems — Plus credits only work on chat.openai.com and cannot be consumed via an API Key. Conversely, API usage doesn't count toward your Plus subscription.

Can one API Key be used across Codex CLI and other tools?

Yes. The same API Key can be used for Codex CLI, ChatGPT custom endpoints, IDE plugins, and more. TeamoRouter's unified API Key supports Codex, Claude Code, and Gemini CLI simultaneously — just configure the baseUrl per each tool's documentation.

Is there a risk of API Key leakage?

Yes, if the Key is hard-coded in source code and committed to GitHub. Use environment variables or .env files, and add .env to your .gitignore.

Does TeamoRouter require a credit card to get an API Key?

No. Sign up to get an API Key, prepay before use, and pay with Alipay or other local methods with a low minimum top-up.

Codex CLI has both OPENAI_API_KEY and ChatGPT login — which takes priority?

If OPENAI_API_KEY is set, Codex CLI uses the API Key for authentication and ignores any logged-in ChatGPT session.

Get Started

  1. Sign up for TeamoRouter and get an API Key
  2. Follow the Codex install guide to configure env vars and baseUrl
  3. Run codex in your terminal and start coding

Get Your Free Codex Setup →

Access Codex, Claude Code, and Gemini CLI stably through TeamoRouter.

Ready to connect?Log in · top up · create an API key — three steps to start.
Codex API Key Setup Guide: Get, Configure, and Troubleshoot · TeamoRouter