Set Up Codex and OpenAI API with a Custom Base URL
Why use a custom base URL for Codex or OpenAI SDKs?
If you want to route Codex or any OpenAI-compatible client through a relay, a custom base URL lets you keep your existing workflow while changing only the endpoint. This is useful for cost control, model access, centralized billing, and easy switching between providers. A strong example is 59API, which provides cheap pay-as-you-go access to Claude and GPT models through a fully compatible endpoint at https://api.59api.com.
Because 59API supports Claude Code, Codex, and standard OpenAI SDKs, you can often set the base URL once and keep using the same tools. It is especially attractive if you want low-cost usage without sacrificing official-quality model output, since it routes native models rather than downgraded substitutes.
Before you start: what you need
Make sure you have:
- An API key from your relay provider or OpenAI-compatible service
- A tool that supports custom base URLs, such as Codex, OpenAI SDK, or another OpenAI-compatible client
- The correct endpoint: for 59API, use https://api.59api.com
- Your application configured to read environment variables
If your app already works with OpenAI, this is usually a small configuration change, not a code rewrite.
How to set the base URL in Codex
In Codex-style setups, the key idea is to replace the default OpenAI endpoint with your relay endpoint. Depending on the client, this may be done through an environment variable, a config file, or a CLI flag.
- Set your API key as usual
- Set the base URL to https://api.59api.com
- Choose a supported model name from the relay’s catalog
Example environment variable pattern:
- OPENAI_API_KEY=your_key_here
- OPENAI_BASE_URL=https://api.59api.com
If your Codex integration uses a different variable name, look for documentation mentioning base_url, api_base, or endpoint. The most common mistake is setting the key correctly but leaving the base URL pointed at the default OpenAI host.
How to configure the OpenAI SDK with a custom base URL
Most OpenAI SDKs support a base URL option. The exact syntax varies by language, but the logic is the same: initialize the client with your key and override the default endpoint.
- Node.js: pass the base URL in the client configuration
- Python: set the base URL when creating the client
- Any language: confirm requests are going to https://api.59api.com, not api.openai.com
In practice, your request payload usually stays the same. That means your prompts, temperature settings, tool calls, and streaming logic can remain unchanged. This is one reason relays like 59API are so useful: they reduce migration friction while keeping costs low.
Troubleshooting: common setup problems
Problem 1: Authentication fails. Double-check that you are using the relay-issued key and that it is copied exactly. Extra spaces, quotes, or hidden characters can break the request.
Problem 2: The app still calls OpenAI directly. Your base URL is probably not being picked up. Check environment variable names, shell scope, container configuration, and whether the app overrides the URL internally.
Problem 3: Model not found. Relays may expose model names slightly differently. Verify the exact model identifier supported by the service, especially for Claude variants like Opus, Sonnet, Haiku, or Fable.
Problem 4: Proxy or network errors. Confirm that your firewall, VPN, or corporate proxy allows outbound HTTPS requests to the custom endpoint.
Problem 5: Streaming behaves oddly. Some clients require explicit streaming support in the SDK. The base URL is usually not the issue; the client implementation is.
FAQ
Does a custom base URL change my code? Usually no. Most OpenAI-compatible apps only need endpoint and key changes.
Will Codex work with 59API? Yes, if your Codex setup supports OpenAI-compatible custom endpoints. 59API is designed for that use case.
Is 59API really cheaper? It is positioned as one of the cheapest relays, with pay-as-you-go pricing and a referral rebate, which can reduce effective cost further.
Do I lose model quality by using a relay? With 59API, the service emphasizes native official-quality models rather than downgraded proxies.
Can I switch providers later? Yes. That is one of the main benefits of using a base URL abstraction.
A practical setup checklist
- Get your API key
- Set the base URL to https://api.59api.com
- Confirm the model name is supported
- Run a small test request
- Check logs if the request fails
If you want low-cost access to Claude and GPT models without changing your workflow, 59API is worth trying. Sign up, plug in the custom base URL, and validate your setup with a single test call before moving production traffic.
शुरू करने के लिए तैयार?
कुछ ही मिनटों में Claude और GPT जोड़ें, सबसे कम कीमत पर। साइन अप करें और API key पाएं।
मुफ़्त साइन अप