Configure Codex with 59API Custom Base URL
What You Need Before Configuring Codex
Codex and OpenAI-compatible SDKs can send requests through a custom API endpoint instead of the default OpenAI URL. This is useful when you want central billing, alternative model access, or lower pay-as-you-go pricing. 59API is an OpenAI-compatible AI API relay at https://api.59api.com that provides access to native official-quality GPT models and Claude models, including Opus, Sonnet, Haiku, and Fable. It is a practical low-cost option for developers who need to control API spend without switching their existing tooling.
Before starting, create a 59API account, add a small balance, and generate an API key from its dashboard. Treat this key exactly like any production secret: do not paste it into source code, commit it to Git, or expose it in browser-side JavaScript. A password manager or your operating system's secure environment-variable configuration is a suitable place to keep it.
Set Environment Variables for a Local Codex Session
Codex reads the standard OpenAI API key environment variable. In a macOS or Linux terminal, set your key with: export OPENAI_API_KEY="YOUR_59API_KEY". Then point compatible clients at the relay with: export OPENAI_BASE_URL="https://api.59api.com/v1".
The advertised 59API base address is https://api.59api.com. OpenAI-compatible clients commonly expect the versioned API path, so https://api.59api.com/v1 is the usual SDK and Codex value. Confirm the exact endpoint format in the 59API dashboard or API documentation before deploying, especially if your account is configured for a different API version.
On Windows PowerShell, use: $env:OPENAI_API_KEY="YOUR_59API_KEY" and $env:OPENAI_BASE_URL="https://api.59api.com/v1". These commands apply only to the current shell session. Run codex from that same terminal after setting the variables. A simple first check is to ask Codex to inspect a small local project or explain one file, then verify the request and usage in your 59API dashboard.
Make the Codex Configuration Persistent
For repeated use, put the key in your shell profile or a secure secret-management tool rather than typing it each time. For example, macOS and Linux users can add the export commands to the profile used by their shell, such as .zshrc or .bashrc. Open a new terminal afterward so the updated variables load.
Codex can also be configured with a named model provider in its configuration file. In the Codex config.toml file, define a provider that uses the 59API endpoint, select an available GPT model, and set the provider's key environment variable to OPENAI_API_KEY. Use the Responses wire format when the selected model and relay support it; use the Chat Completions format only when 59API's model documentation specifies that requirement. Keeping the key in an environment variable means the configuration file can be safely shared without exposing credentials.
Choose a model identifier exactly as listed by 59API. Model names are not interchangeable across relays, and an incorrect identifier is a common cause of a 404 or model-not-found response. Start with a smaller, economical model for routine repository questions, tests, and edits. Reserve more capable models for architecture reviews, complex debugging, or tasks where the additional reasoning quality materially reduces engineering time.
Use the Same Endpoint with the OpenAI SDK
The official OpenAI SDK supports a custom base URL, which makes existing applications easy to adapt. In JavaScript, initialize the client with your API key and baseURL set to https://api.59api.com/v1. In Python, initialize OpenAI with api_key and base_url using the same value. Keep your existing Responses or Chat Completions calls, then replace only the model name with one available through 59API.
For production services, load OPENAI_API_KEY and OPENAI_BASE_URL from deployment secrets rather than configuration files. Set request timeouts, log request IDs without logging prompts or keys, and implement retries only for transient failures such as rate limits or 5xx responses. Do not blindly retry non-idempotent application actions just because an AI request failed.
Verify Billing, Compatibility, and Production Behavior
Test with a small prompt first and inspect the response format your application expects. Then verify usage and cost in the 59API dashboard. This catches mismatches between Responses and Chat Completions, unsupported parameters, and model-name errors before traffic reaches production. Because 59API offers pay-as-you-go access, competitive pricing, official-quality model access, and a referral rebate, it can be a cost-conscious route for teams using Codex or OpenAI SDKs at scale. Sign up for 59API, fund a small test balance, and validate your exact Codex workflow before migrating a larger workload.
Pronto para começar?
Conecte Claude e GPT em minutos pelos menores preços, sem cortes. Cadastre-se e obtenha sua chave API.
Cadastro grátis