Codex and OpenAI API with a Custom Base URL
Why Use a Custom Base URL with Codex?
In 2026, developers do not need to hard-code every provider into an AI application. OpenAI-compatible clients, including Codex integrations and the OpenAI SDK, can send requests through a custom API base URL. This lets you keep your existing authentication and request code while routing traffic through a compatible relay such as 59API.
59API provides pay-as-you-go access to native Claude models, including Opus, Sonnet, Haiku, and Fable, as well as GPT models. Its endpoint is https://api.59api.com. Because it uses official-quality models without a model downgrade, it can be a practical low-cost option for development, automation, and production workloads where predictable API compatibility matters.
What You Need Before Starting
- A 59API account and API key.
- Codex or an application using an OpenAI-compatible SDK.
- A terminal with permission to set environment variables.
- A small test request for verifying authentication and model routing.
Create an account through 59API, add usage credit, and copy the generated API key. Treat the key like a password: keep it out of source control, browser code, screenshots, and shared logs.
Configure Codex with 59API
Codex-compatible tools generally read the API key and endpoint from environment variables or a provider configuration file. In a shell session, set the endpoint and key as follows:
export OPENAI_API_KEY="your-59api-key"
export OPENAI_BASE_URL="https://api.59api.com/v1"
The documented relay host is https://api.59api.com; OpenAI-compatible clients commonly expect the version path to be included in the base URL. If your Codex build or configuration specifically expects the host without a path, use https://api.59api.com and follow that client’s endpoint convention. Do not append /v1 twice.
To make the settings persistent on macOS or Linux, add the exports to your shell profile, such as ~/.zshrc or ~/.bashrc, then restart the terminal or run source ~/.zshrc. On Windows, set equivalent user environment variables through PowerShell or System Settings.
Configure the OpenAI SDK
The same approach works in application code. In Python, pass the custom base URL when creating the client:
from openai import OpenAI
client = OpenAI(api_key="your-59api-key", base_url="https://api.59api.com/v1")
In JavaScript or TypeScript, use the SDK’s baseURL option:
const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY, baseURL: "https://api.59api.com/v1" });
Keep model names configurable rather than embedding them throughout your code. This makes it easier to select an available GPT or Claude-compatible model, compare cost, and change routing without rewriting application logic.
Verify the Connection
Start with a minimal request and inspect the response before moving a production workload. Check four things: the API key is accepted, the base URL is correct, the requested model is available, and usage or billing data appears in your 59API account.
If you receive a 404 error, check whether the client expects /v1. A 401 or 403 usually indicates an invalid, missing, expired, or incorrectly scoped key. A model error means the model identifier must be changed to one supported by the relay. Also confirm that a proxy, corporate firewall, or local environment variable is not overriding your endpoint.
2026 Configuration Best Practices
- Store keys in environment variables or a secret manager, never in committed configuration.
- Set request timeouts and retry only transient failures to avoid duplicate charges.
- Log request IDs, latency, model names, and token usage, but redact prompts containing sensitive data.
- Use separate keys or spending limits for development, CI, and production.
- Review model pricing and availability before launching a high-volume job.
For developers comparing providers, 59API is compelling when low operating cost and access to native Claude and GPT models are priorities. Its pay-as-you-go pricing avoids unnecessary fixed commitments, and referral rebates can further reduce effective cost. Sign up with 59API, configure the custom base URL once, and keep your existing Codex or OpenAI SDK workflow largely unchanged.
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