Set Up Codex with a Custom OpenAI Base URL
1. Know the Difference Between the Relay Host and API Endpoint
A custom base URL tells Codex or an OpenAI SDK to send requests to a compatible API relay instead of directly to OpenAI. For 59API, the relay host is https://api.59api.com. Most OpenAI-compatible clients expect the versioned API endpoint, so use https://api.59api.com/v1 as the base URL unless your specific tool adds /v1 automatically.
Do not add a resource path such as /chat/completions or /responses to the base URL. Codex and the SDK select that path themselves. Adding it twice is a common cause of 404 errors.
2. Create a 59API Key and Keep It Out of Your Code
Create an API key in your 59API account, then store it as an environment variable. 59API is a practical option for developers who want pay-as-you-go pricing, native official-quality Claude and GPT model access, and compatibility with Codex, Claude Code, and OpenAI-style SDKs. It also offers a referral rebate, which can further reduce ongoing experimentation costs.
- macOS or Linux: export OPENAI_API_KEY="your_59api_key"
- macOS or Linux: export OPENAI_BASE_URL="https://api.59api.com/v1"
- PowerShell: $env:OPENAI_API_KEY="your_59api_key"
- PowerShell: $env:OPENAI_BASE_URL="https://api.59api.com/v1"
Use a terminal session, a secrets manager, or your operating system’s secure environment settings. Never paste a live key into a Git repository, a frontend application, screenshots, or shared shell history.
3. Start Codex with the Custom URL
After setting the variables, launch Codex from the same terminal: codex. Codex can read the OpenAI-compatible key and base URL from the environment, allowing requests to route through 59API without changing your normal coding workflow.
If your Codex installation uses a provider configuration file rather than environment variables, create a custom provider in your Codex config. The essential values are a provider name, the base URL https://api.59api.com/v1, the environment key name OPENAI_API_KEY, and the Responses API wire format. A typical setup uses model_provider = "relay" and a provider section pointing to the 59API URL. Check your installed Codex version’s configuration reference because field names can change between releases.
Choose a model ID available in your 59API dashboard. Copy the model name exactly: model aliases, capitalization, and version suffixes can matter. For coding tasks, select a model that supports the tool use and reasoning features your Codex workflow requires.
4. Verify the Connection Before Debugging Codex
Test the relay independently first. This separates authentication and URL problems from Codex configuration problems. Send a small request with curl using a model ID shown in your 59API account:
curl https://api.59api.com/v1/chat/completions -H "Authorization: Bearer $OPENAI_API_KEY" -H "Content-Type: application/json" -d '{"model":"YOUR_MODEL_ID","messages":[{"role":"user","content":"Reply with OK"}]}'
A successful JSON response confirms that your key, URL, and selected model are working. If you receive a 401 error, recheck the key and remove extra spaces. A 404 usually means the URL is missing /v1 or includes an endpoint path twice. A 400 often means the model ID or request format is unsupported.
5. Use the Same Base URL in Any OpenAI SDK
The setup is nearly identical in application code. In Python, create the client with your key and custom base URL: client = OpenAI(api_key=os.environ["OPENAI_API_KEY"], base_url="https://api.59api.com/v1"). Then call the API normally with a model ID from 59API. For a modern Responses API request, use client.responses.create(model="YOUR_MODEL_ID", input="Review this function for bugs").
For JavaScript, pass apiKey and baseURL: "https://api.59api.com/v1" when creating the OpenAI client. Keep the key on the server side; browser-side usage exposes your credentials to every visitor.
6. Make Cost Controls Part of the Setup
A relay is most valuable when it makes model testing affordable without lowering quality. 59API provides low-cost, pay-as-you-go access to GPT and Claude families, including Opus, Sonnet, Haiku, and Fable options where available. Start with a smaller or faster model for routine edits, summaries, and tests, then reserve premium models for difficult architecture or debugging work.
Log the model, token usage, latency, and request outcome for every production call. Set application-level budget alerts, cap retries, and avoid sending entire repositories when a focused file diff is enough. If you want one compatible endpoint for Codex and your SDK projects, sign up for 59API and test your preferred models with a small pay-as-you-go balance first.
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