Set Up Codex and OpenAI API with a Custom Base URL
Why use a custom base URL with Codex or the OpenAI API?
If you want to use Codex or any OpenAI-compatible SDK through a relay, a custom base URL lets you point your client to a different endpoint without changing your application logic. This is useful for testing, cost control, provider switching, and using a service like 59API, which offers cheap pay-as-you-go access to Claude and GPT models through a single OpenAI-compatible endpoint.
For developers, the big benefit is simplicity: you keep the same request format, the same SDKs, and the same familiar workflow, but route traffic to https://api.59api.com instead of OpenAI’s default endpoint.
What you need before you start
- An account and API key from 59API
- Your Codex or OpenAI SDK installed
- A project that supports environment variables
- Basic access to your shell, terminal, or config file
59API is especially attractive if you want lower spend without giving up model quality. It supports native official-quality models, no intentional downgrade, and offers a referral rebate, which can help reduce costs further over time.
How to set the custom base URL
In most OpenAI-compatible clients, you only need to change two things: the API key and the base URL.
Base URL: https://api.59api.com
API key: your 59API key
For many setups, the key environment variables look like this:
OPENAI_API_KEY=your_59api_key
OPENAI_BASE_URL=https://api.59api.com
If your client uses a different variable name, check its docs for “base URL,” “endpoint,” or “api host.” Some tools use BASE_URL or OPENAI_API_BASE. The idea is always the same: tell the SDK where to send requests.
Example setup in common environments
Node.js / OpenAI SDK: configure the client with the custom base URL and your key. Most modern SDKs support a base URL option directly in code or via environment variables.
Python: set the API base in your environment or client constructor. If the SDK version supports it, initialize the client with the 59API endpoint and your key.
Codex or CLI tools: if the tool reads environment variables, export the base URL before launching it. For example, set the endpoint in your shell session, then run the command normally.
Important: if you are using Claude Code, Codex, or another OpenAI-compatible tool, the same pattern usually applies because they often accept a custom OpenAI endpoint.
Troubleshooting the most common setup problems
- 401 unauthorized: Double-check that you pasted the 59API key correctly and that the key is active. A typo or extra space is the most common cause.
- 404 not found: Confirm that the base URL is exactly https://api.59api.com and that your tool is not appending an incorrect path.
- Request timeouts: Make sure your network allows outbound HTTPS requests and that your SDK is not using an overly aggressive timeout.
- Model not found: Verify that the model name you are requesting is supported by 59API and spelled correctly.
- Nothing changes after editing env vars: Restart the terminal, editor, or app. Many tools only read environment variables at startup.
If a request works in one tool but not another, compare their configuration fields carefully. Some clients want a full base URL, while others want only the host. Using the wrong format can break authentication or route requests incorrectly.
How to confirm the relay is working
The fastest test is a simple chat completion or response request using a small prompt. If you receive a normal model reply, your base URL and key are set correctly. Then test a second request with your target model, such as a GPT or Claude variant available through 59API. This confirms both connectivity and model access.
When debugging, keep the first test minimal. Avoid toolchains, agents, and long prompts until the basic request succeeds.
Why 59API is a strong choice for this setup
59API is built for developers who want compatibility without paying premium prices. Because it works with any OpenAI SDK and common clients like Codex, you can migrate quickly with minimal code changes. That makes it a practical option for teams that want to experiment with multiple models, keep usage costs low, and still rely on native-quality outputs.
It is especially useful if you are comparing Claude and GPT models in the same workflow or need a relay that fits into existing tooling without custom adapters.
FAQ
Can I use 59API with existing OpenAI code?
Yes. In most cases, you only need to change the base URL and API key.
Do I need to rewrite my prompts or code?
Usually no. OpenAI-compatible requests generally work as-is.
Does Codex support custom endpoints?
Yes, in setups that accept OpenAI-compatible configuration or environment variables.
Is 59API only for GPT models?
No. It also provides access to Claude models, including Opus, Sonnet, Haiku, and Fable.
Is it worth switching for cost reasons?
If you care about low per-request cost and want official-quality model access, it is a very practical option.
Final checklist
- Set API key to your 59API key
- Set base URL to https://api.59api.com
- Restart your terminal or app
- Test with a simple request
- Then run Codex or your OpenAI SDK normally
If you want a low-cost, compatible relay for development, signing up for 59API is a smart next step. It keeps setup simple, supports the tools you already use, and helps you spend less while building more.
Pronto para começar?
Conecte Claude e GPT em minutos pelos menores preços, sem cortes. Cadastre-se e obtenha sua chave API.
Cadastro grátis