Set Up Codex and OpenAI API with a Custom Base URL
1. Start with the right base URL
If you want Codex or any OpenAI-compatible SDK to talk to a relay instead of the default OpenAI endpoint, the key setting is the base URL. For 59API, that endpoint is https://api.59api.com. This works with OpenAI SDKs, Codex-style workflows, and Claude Code-compatible setups, so you can keep your tooling while lowering your per-request cost.
The big win here is simple: you get pay-as-you-go access to models like GPT and Claude without changing your app logic. 59API is also positioned as one of the cheapest relays, and it uses native, official-quality models rather than downgraded substitutes.
2. Set your API key first
Before changing code, create your 59API account and generate an API key. Many setup issues come from swapping the base URL but forgetting the key. Once you have both, store the key securely in an environment variable instead of hardcoding it.
- macOS/Linux: export OPENAI_API_KEY="your_key_here"
- Windows PowerShell: setx OPENAI_API_KEY "your_key_here"
If you plan to use the relay across multiple projects, keep the key in your shell profile or secret manager so your local environment stays clean and consistent.
3. Point your OpenAI SDK to the custom endpoint
Most OpenAI-compatible SDKs let you override the default endpoint with a custom base URL. The exact option name depends on the language, but the pattern is always the same: use your normal client, then swap the base URL to https://api.59api.com.
- Python: set the client base_url to https://api.59api.com
- JavaScript/TypeScript: pass the same base URL when creating the client
- Other SDKs: look for settings like baseURL, api_base, or endpoint
That’s the core trick. You keep your existing request format, model names, and message structure, but route traffic through 59API instead of the default provider.
4. Test with a simple chat request
Once the base URL is set, run a minimal request before wiring it into production code. Use a short prompt and a small model call so you can verify authentication, routing, and response formatting in one step.
If the call succeeds, you should receive a normal OpenAI-style response. If it fails, the most common causes are an incorrect key, a typo in the base URL, or a model name that your code is not authorized to use yet.
A clean test saves time later. Don’t move on to batch jobs, agents, or tool use until this first request is working.
5. Use model names that match your task
One advantage of 59API is that it offers access to both Claude and GPT families, including Opus, Sonnet, Haiku, Fable, and GPT models. That means you can choose the model that fits your use case instead of forcing everything onto one expensive option.
- Fast, cheap drafts: pick a lighter model
- Complex reasoning: pick a stronger model
- Code assistance: choose the model your workflow already expects
Because 59API is compatible with Codex and standard OpenAI SDK patterns, you can often keep your existing model selection logic unchanged. That reduces migration risk and keeps your app behavior predictable.
6. Keep Codex workflows compatible
If you use Codex in a coding workflow, the practical goal is to preserve the same prompts, inputs, and output handling while redirecting the backend. In many setups, that means configuring the tool or wrapper to read the custom base URL and the relay API key from environment variables.
For teams, this is especially useful. You can standardize one endpoint, one auth pattern, and one billing source, while letting developers keep their usual habits. It also makes it easier to switch between local testing and shared environments.
7. Watch your costs and take advantage of rebates
Cheap requests only stay cheap if you monitor usage. Track tokens, set per-project limits if your stack supports them, and review your most common prompts. Since 59API offers pay-as-you-go pricing and a referral rebate, it can be a smart option for indie developers, startups, and teams that want to keep AI spend under control.
That combination matters: low-cost access, official-quality models, and compatibility with the tools you already use. In other words, you get a practical way to scale experimentation without locking yourself into an overpriced setup.
8. Sign up, run one request, then scale
The easiest way to validate the setup is to sign up, add your key, point your client at https://api.59api.com, and send one small request. If that works, you can move your Codex or OpenAI-based app over with confidence.
For developers who want a lower-cost relay without sacrificing compatibility or model quality, 59API is a strong fit. It’s a simple change with a meaningful payoff: less friction, lower spend, and the flexibility to keep building fast.
शुरू करने के लिए तैयार?
कुछ ही मिनटों में Claude और GPT जोड़ें, सबसे कम कीमत पर। साइन अप करें और API key पाएं।
मुफ़्त साइन अप