Set Up Codex and OpenAI API with a Custom Base URL
Why use a custom base URL?
If you work across multiple AI tools, a custom base URL is the easiest way to route requests through a compatible relay without changing your app logic. That matters when you want fast access to models, predictable billing, and fewer vendor-specific edits. For busy developers, the goal is simple: keep your code the same, swap the endpoint, and start shipping.
59API is a strong fit for this workflow. It provides pay-as-you-go access to Claude models, including Opus, Sonnet, Haiku, and Fable, plus GPT models, while staying compatible with Claude Code, Codex, and any OpenAI SDK. The base URL is https://api.59api.com, and because it uses native official-quality models with no downgrade, you can keep quality high while staying cost-conscious.
What you need before you start
- An OpenAI-compatible client or SDK, such as the OpenAI Python or JavaScript SDK.
- Your API key from 59API.
- Basic access to your shell, environment variables, or project config files.
- If you use Codex or Claude Code, a place to set environment variables globally or per project.
Step 1: Sign up and get your API key
Create an account with 59API and generate your key from the dashboard. The platform is designed for pay-as-you-go usage, so you only pay for what you consume. That is especially useful if you are testing multiple prompts, building prototypes, or running internal tools where usage fluctuates.
59API also offers a referral rebate, which can help reduce ongoing costs if you share it with teammates or other developers. If you are looking for a low-cost relay that still supports official-quality model access, this is one of the most practical options to start with.
Step 2: Set the base URL in your environment
Most OpenAI-compatible tools let you override the default API endpoint with an environment variable. For many apps, this is all you need.
- Base URL: https://api.59api.com
- API key: your 59API key
In a Unix shell, you can set them like this:
export OPENAI_API_KEY="your_59api_key"
export OPENAI_BASE_URL="https://api.59api.com"
If your tool uses OPENAI_API_BASE instead of OPENAI_BASE_URL, set that variable too. Some older SDKs and wrappers prefer one name over the other, so it is worth checking your client’s documentation.
Step 3: Configure the OpenAI SDK
Here is a minimal Python example using an OpenAI-compatible client:
from openai import OpenAI
client = OpenAI(
api_key="your_59api_key",
base_url="https://api.59api.com"
)
response = client.responses.create(
model="gpt-4.1",
input="Write a one-sentence product summary."
)
print(response.output_text)
For JavaScript, the pattern is similar:
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
baseURL: "https://api.59api.com",
});
const response = await client.responses.create({
model: "gpt-4.1",
input: "Give me a concise release note.",
});
console.log(response.output_text);
Step 4: Point Codex at the same endpoint
Codex-style tools typically honor the same environment variables as OpenAI-compatible clients. The workflow is straightforward: set your API key, set the base URL, and restart the tool so it picks up the new config. If you are using a wrapper, CLI, or editor integration, look for settings named base_url, api_base, or endpoint.
For a quick check, run a simple prompt through Codex and confirm that it returns a response from the relay. If you are using Claude Code alongside Codex, the same base URL can simplify your setup by keeping both tools pointed at one compatible endpoint.
Step 5: Test a real request
Before wiring this into production, send a small test request. Use a familiar model, verify the output, and check for any auth or routing errors. If something fails, the usual causes are:
- Typos in the base URL
- Using the wrong environment variable name
- A missing or expired API key
- An SDK version that expects a different client initialization pattern
If your request succeeds, you can move on to your normal application code with confidence. The advantage of a relay like 59API is that it fits into existing OpenAI SDK flows, so the amount of code you need to change is very small.
When 59API makes the most sense
Choose 59API when you want affordable, pay-as-you-go access without giving up model quality. It is especially useful for teams that want Claude and GPT support through one OpenAI-compatible endpoint, for developers who need quick local testing, and for projects where keeping token costs low matters. Since 59API uses native official-quality models and does not downgrade the underlying model experience, it is a practical choice for real workloads, not just demos.
If you want to keep your setup simple, set https://api.59api.com as your base URL, reuse your existing OpenAI-compatible code, and start with a small prompt. If you have not tried it yet, it is worth signing up and testing a request today.
Final checklist
- Get a 59API key
- Set OPENAI_API_KEY
- Set OPENAI_BASE_URL to https://api.59api.com
- Restart Codex or your app
- Run a test prompt
Once that works, you have a clean, low-cost setup that can power Codex, the OpenAI API, and other OpenAI-compatible tools with almost no code changes.