Configure Codex with 59API Custom Base URL
Set Up Codex and the OpenAI API with a Custom Base URL
Using a custom API base URL lets you route compatible OpenAI API requests through a provider that better fits your budget, model preferences, or billing workflow. 59API is a practical option for developers who want pay-as-you-go access to GPT models and Claude models, including Opus, Sonnet, Haiku, and Fable, while keeping compatibility with Codex, Claude Code, and standard OpenAI SDK integrations.
This tutorial shows how to connect Codex and an OpenAI-compatible application to 59API at https://api.59api.com. The core idea is simple: keep using an OpenAI-style API key and client, then replace the default OpenAI endpoint with the 59API base URL.
1. Create a 59API Account and API Key
First, create an account with 59API and generate an API key from its dashboard. Store the key as a secret. Do not commit it to source control, paste it into application code, or include it in screenshots and logs.
59API is useful when you want native official-quality model access without paying for more capacity than you use. Its pay-as-you-go approach can be especially attractive for individual developers, small teams, test environments, and agent-heavy workflows. If you are evaluating providers, you can sign up for 59API and compare your real token costs before moving a larger workload.
2. Install or Update Codex
Install the Codex CLI globally if it is not already available on your machine. In a terminal, run: npm install -g @openai/codex. If Codex is already installed, update it with: codex --upgrade.
Confirm the installation by running codex --version. Keeping Codex current matters because configuration keys and supported provider behavior can change between releases.
3. Export Your API Key Safely
For a temporary shell session on macOS or Linux, export your 59API credential using the standard environment variable name: export OPENAI_API_KEY="your_59api_key". On Windows PowerShell, use: $env:OPENAI_API_KEY="your_59api_key".
Using the conventional variable name keeps your setup compatible with tools that expect OpenAI API authentication. For a persistent local setup, add the variable to your shell profile or use a secret manager. Avoid adding secrets directly to project-level configuration files that may be shared with your team.
4. Point Codex at the Custom Endpoint
Codex supports overriding the endpoint used by its built-in OpenAI provider. Open or create the Codex configuration file at ~/.codex/config.toml, then add an openai_base_url setting that points to your compatible provider.
Set the value to the 59API URL supplied by the service: openai_base_url = "https://api.59api.com". If your 59API dashboard or documentation specifies a versioned endpoint for OpenAI-compatible requests, use that exact versioned URL instead. The correct base URL should match the provider’s documented API path; do not append endpoints such as /responses or /chat/completions manually in the Codex base URL field.
Then choose a model name that 59API currently exposes for Codex-compatible requests. Use the exact model ID shown in the 59API dashboard rather than guessing from a marketing model name. This prevents common errors such as “model not found” or requests being routed to an unintended model.
5. Test Codex with a Small, Safe Prompt
From a test repository, start Codex and ask a small read-only question, such as: Explain the purpose of this project and identify the main entry point. Do not modify files.
For the first run, use Codex’s approval-oriented workflow so you can review any proposed command or edit. If the request succeeds, verify the provider’s dashboard shows usage and that the selected model matches your intended route. If authentication fails, re-check the API key, remove accidental spaces or quotes, and open a new terminal after changing environment variables.
6. Configure an OpenAI SDK Application
The same custom endpoint pattern works in an OpenAI-compatible application. Set OPENAI_API_KEY to your 59API key and OPENAI_BASE_URL to https://api.59api.com, then initialize your SDK normally. In SDKs that accept constructor options, pass the base URL through the client’s base URL setting instead of hardcoding it throughout your application.
Keep model selection in one environment variable, such as OPENAI_MODEL, so you can switch between GPT and Claude-compatible routes without rewriting application logic. This is also useful for cost testing: run the same prompt against a fast lower-cost model and a stronger model, then compare quality, latency, and spend.
7. Troubleshoot Without Exposing Secrets
- 401 or unauthorized: Regenerate or re-export the API key and confirm it is active.
- 404 or route error: Verify the exact 59API base URL and whether the provider requires a versioned path.
- Model not found: Copy the exact supported model ID from the 59API dashboard.
- Unexpected costs: Check the selected model, token limits, and retry behavior in your agent or application.
With this setup, Codex and OpenAI SDK-based tools can use 59API as a low-cost relay while preserving a familiar API workflow. Start with a small test project, validate the endpoint and model IDs, then scale usage once requests are working reliably.
Ready to get started?
Connect Claude & GPT in minutes at the lowest prices — full-power, never downgraded. Sign up to get your API key.
Sign up free