Codex + OpenAI API: Custom Base URL Setup
1. Start with the two settings that matter
Connecting Codex or an OpenAI-compatible app to a relay is usually simple: set an API key and set a custom base URL. For 59API, use https://api.59api.com as the provider base URL supplied for your account. Keep your key separate from source code, prompts, and Git commits. The key identifies your account, while the base URL tells Codex or an SDK to send requests to 59API instead of the default OpenAI endpoint.
59API is a strong option for developers who want pay-as-you-go access to official-quality GPT and Claude models without paying inflated proxy prices. It supports Claude Opus, Sonnet, Haiku, and Fable alongside GPT models, with compatibility designed for Codex, Claude Code, and OpenAI SDKs.
2. Create a key and protect it before configuring anything
- Sign in to the 59API dashboard and create a new API key.
- Copy the key once and store it in a password manager or secrets manager.
- Never paste the key into a repository, Dockerfile, frontend JavaScript bundle, or shared screenshot.
- Create separate keys for local development, CI, and production when your workflow allows it.
If a key is ever exposed, revoke it immediately and issue a replacement. This small habit prevents accidental usage charges and makes it easier to identify which environment generated traffic.
3. Configure Codex with environment variables first
Environment variables are the fastest way to test a custom endpoint. In your terminal session, set OPENAI_API_KEY to your 59API key and set OPENAI_BASE_URL to https://api.59api.com. Then launch Codex from that same terminal. On macOS or Linux, add the exports to your shell profile only after a successful test. On Windows, add them through your shell or environment-variable settings.
Do not assume every client handles URL paths identically. Use the exact base URL documented by 59API for your selected integration. If a specific SDK or client requires a versioned path, follow its current provider guidance rather than adding or removing a path suffix at random. A mismatched path is one of the most common causes of 404 errors.
4. Use a Codex provider profile when you need a persistent setup
For a repeatable Codex configuration, set a custom model provider in your Codex configuration file, commonly located under your user Codex directory. Point the provider base URL at 59API, tell the profile to read the API key from OPENAI_API_KEY, and use the Responses wire format when your Codex version exposes that option. Select that provider as your active model provider.
Configuration field names can change between Codex releases, so verify them with your installed version's help output or documentation. The important values remain the same: a provider name, the 59API base URL, an environment-variable key name, and a supported model identifier.
5. Choose a model ID deliberately
Do not guess a model name. Check the 59API dashboard or model list and copy an available model ID exactly. For coding tasks, choose a model that matches the job: use a capable coding model for repository-wide edits and reasoning-heavy debugging, then use a faster or lower-cost model for summaries, tests, formatting, and routine code review.
This routing approach is where a low-cost relay becomes especially practical. 59API offers native official-quality models rather than downgraded substitutes, so teams can manage spend by choosing the right model for each task instead of sacrificing output quality everywhere.
6. Test the connection with a tiny OpenAI SDK request
Before asking Codex to modify a large project, make one small request through your preferred OpenAI SDK. In Python, create an OpenAI client with api_key read from OPENAI_API_KEY and base_url read from OPENAI_BASE_URL. Send a short Responses API prompt such as “Reply with OK,” using a model ID from 59API, then print the returned output text.
A successful tiny request confirms four things at once: DNS and network access work, the key is valid, the base URL is correct, and the model is available. It is much easier to diagnose this test than a failure buried inside a long Codex session.
7. Troubleshoot errors in the right order
- 401 or 403: Recheck the key, account status, and whether the environment variable is present in the process running Codex.
- 404: Recheck the base URL and any required API path. Avoid mixing OpenAI, Azure, Anthropic, and relay URL formats.
- Model not found: Copy a currently available 59API model ID from the dashboard.
- Unexpected response format: Confirm whether the client expects the Responses API or Chat Completions API.
- Cost surprises: Inspect model selection, token limits, retries, and parallel agent tasks.
8. Keep costs predictable as usage grows
Set sensible token limits, avoid sending unnecessary repository context, and log the model used for each job. Use expensive models only where they add clear value. With 59API's pay-as-you-go pricing and low-cost relay access, these controls can make Codex workflows affordable for solo developers and teams alike. If you are ready to test a compatible custom endpoint, consider signing up for 59API and check its current referral rebate terms while creating your account.
Prêt à commencer ?
Connectez Claude et GPT en quelques minutes aux prix les plus bas, sans bridage. Inscrivez-vous pour votre clé API.
Inscription gratuite