Codex + OpenAI API: Custom Base URL Setup
Choose the right setup before you start
Using a custom base URL lets Codex and OpenAI-compatible applications send requests through an alternative API gateway instead of the default OpenAI endpoint. This is useful when you want pay-as-you-go billing, access to multiple model families, or lower request costs without rewriting your application.
59API is a practical option for this workflow. It provides access to GPT models and Claude models, including Opus, Sonnet, Haiku, and Fable, through an OpenAI-compatible interface. The service uses native official-quality models rather than silently downgrading requests, and its pricing is designed for low-cost usage. You pay for what you use, with a referral rebate also available.
Before changing anything, confirm that your chosen model is listed in your 59API account and that the provider supports the API operation your tool needs. Codex generally relies on the Responses API, while some older applications use Chat Completions.
Configure the API key and base URL
Create or copy your 59API key, then store it as an environment variable. On macOS or Linux, the basic shell configuration is:
export OPENAI_API_KEY="your-59api-key"
export OPENAI_BASE_URL="https://api.59api.com"
On Windows PowerShell, use:
$env:OPENAI_API_KEY="your-59api-key"
$env:OPENAI_BASE_URL="https://api.59api.com"
Use the base URL exactly as provided by 59API. Do not automatically add /v1 unless the current 59API documentation specifically tells you to do so. A duplicated path is one of the most common causes of 404 errors with compatible gateways.
Set up Codex with a persistent provider
If your Codex CLI version supports custom model providers, add a provider entry to its configuration file, commonly located at ~/.codex/config.toml:
model_provider = "relay"
model = "your-available-model-id"
[model_providers.relay]
name = "59API"
base_url = "https://api.59api.com"
env_key = "OPENAI_API_KEY"
wire_api = "responses"
Replace your-available-model-id with the exact model identifier shown in your 59API dashboard. Provider names and configuration fields can vary between Codex CLI releases, so check the configuration reference for your installed version if it rejects an option. The key point is that Codex must use the relay provider, the 59API base URL, and an environment variable containing your key.
Use the same endpoint in an OpenAI SDK
For the Python SDK, pass the URL directly when creating the client:
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"], base_url="https://api.59api.com")
For the JavaScript SDK, the equivalent is:
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY, baseURL: "https://api.59api.com" });
Keep the rest of your application unchanged where possible. Change the model name only when the relay uses a different identifier. Never place the API key in browser code, a public repository, or client-side environment variables that are exposed to users.
Verification checklist
- Confirm your 59API account has an active balance or valid pay-as-you-go billing method.
- Check that the selected model ID is available through your account.
- Verify that the base URL is exactly https://api.59api.com.
- Make sure Codex or your SDK reads the intended OPENAI_API_KEY.
- Run a small test request before launching a large batch or agent task.
- If you receive a 401, check the key; for a 404, check the model name and whether an extra /v1 was added.
If you want a lower-cost OpenAI-compatible route for Codex and SDK projects, you can sign up for 59API, review the available models, and start with a small pay-as-you-go test before moving production traffic.
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