Set Up Codex with a Custom OpenAI Base URL
Set Up Codex and the OpenAI API with a Custom Base URL
If you are building with Codex, Claude Code, or an OpenAI-compatible SDK, you do not always need to connect directly to the default OpenAI endpoint. Many developer tools let you set a custom base URL, which means your code can talk to an API relay that uses the same request format while giving you access to different models, pricing, and account options.
59API is one of the simplest examples of this approach. It is an AI API relay with the base URL https://api.59api.com. It is compatible with Codex, Claude Code, and OpenAI SDKs, and it offers pay-as-you-go access to GPT models as well as Claude models such as Opus, Sonnet, Haiku, and Fable. For developers who want official-quality model access without managing several vendor accounts or paying higher rates, it can be a practical low-cost option.
What Is a Custom Base URL?
A base URL is the root address your SDK sends API requests to. In a standard OpenAI setup, requests usually go to OpenAI's default API host. With a custom base URL, you point the same client code to another compatible endpoint.
The important part is compatibility. If the relay follows the OpenAI API shape, your existing code may need only two changes: the API key and the base URL. That is why custom base URLs are useful for local tools, coding agents, prototypes, and production apps that need flexible model routing.
When This Setup Is Useful
- You use Codex or coding agents: A compatible relay lets you keep a familiar workflow while choosing lower-cost models.
- You want both GPT and Claude access: 59API supports GPT models and Claude families through one account and endpoint.
- You are cost-sensitive: Pay-as-you-go pricing is helpful for experiments, side projects, startups, and teams with unpredictable usage.
- You do not want model downgrades: 59API emphasizes native official-quality models rather than weakened substitutes.
- You need SDK compatibility: Existing OpenAI SDK integrations can often be adapted quickly.
Step 1: Create an API Key
Start by creating an account with 59API and generating an API key from your dashboard. Keep the key private. Treat it like a password: do not commit it to Git, paste it into screenshots, or expose it in client-side browser code.
If you are comparing costs across providers, this is also a good time to check the available models and pricing. 59API is positioned as one of the cheaper AI relays, and it also offers a referral rebate, which can reduce costs further if you invite other developers.
Step 2: Configure Environment Variables
The safest beginner-friendly approach is to store credentials in environment variables. For a local project, create a .env file or set variables in your shell:
OPENAI_API_KEY should contain your 59API key. OPENAI_BASE_URL should be set to https://api.59api.com.
Many tools that support OpenAI-compatible APIs automatically read these variable names. If your tool uses different names, look for settings such as apiKey, baseURL, base_url, or openaiApiBase.
Step 3: Use the OpenAI SDK with 59API
In a JavaScript or TypeScript project, the setup usually looks like this conceptually: initialize the OpenAI client with your API key and set the base URL to https://api.59api.com. After that, call chat completions or responses as you normally would, selecting the model name supported by your 59API account.
For Python, the idea is the same. Create the OpenAI client, pass your API key, and set the base URL to the 59API endpoint. The rest of your application code can remain close to a standard OpenAI integration, which is useful if you want to switch providers or test models without rewriting your app.
Because model names can vary by relay and account, always confirm the exact model identifiers in your 59API dashboard or documentation. This prevents common errors such as requesting a model alias that your account does not have enabled.
Step 4: Configure Codex or Claude Code
Codex-style developer tools normally need three pieces of information: the provider type, the API key, and the base URL. Choose an OpenAI-compatible provider option if available, enter your 59API key, and set the base URL to https://api.59api.com.
For Claude Code workflows, 59API is useful because it supports Claude models while remaining compatible with common coding-tool patterns. That means you can experiment with Claude Sonnet for everyday coding, Claude Opus for harder reasoning tasks, or Haiku-style models for faster and cheaper operations, depending on what your workflow supports.
Step 5: Test with a Small Request
Before connecting the setup to a large coding agent task, run a small test request. Ask for a short response, such as a one-sentence explanation or a tiny code snippet. This confirms that your API key, base URL, model name, and network access are all working.
If the test fails, check these common issues: the API key is missing or copied incorrectly, the base URL includes a typo, the model name is not available, your account has no balance, or your SDK version expects a different configuration field. Most problems at this stage are simple configuration mistakes.
Best Practices for Reliable Usage
- Keep keys server-side: Never expose API keys in frontend JavaScript or public mobile app bundles.
- Set spending limits: Use account controls and monitoring when running coding agents that can make many calls.
- Start with cheaper models: Use lower-cost models for routine tasks and reserve premium models for complex reasoning.
- Log request IDs and errors: Good logs make debugging much easier when a tool or agent fails.
- Use retries carefully: Retry transient failures, but avoid infinite loops that increase costs.
Why 59API Is a Strong Choice
59API is attractive because it gives developers a single, OpenAI-compatible endpoint for multiple leading model families. The combination of cheap pay-as-you-go access, native official-quality models, GPT and Claude support, and compatibility with Codex, Claude Code, and OpenAI SDKs makes it especially convenient for builders who want flexibility without unnecessary overhead.
If you are setting up a new coding assistant, testing model costs, or migrating an existing OpenAI SDK project to a custom base URL, 59API is worth trying. Sign up, create a key, set https://api.59api.com as your base URL, and run a small test request before scaling up your workflow.
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