59API

← 返回教程列表

Set Up Codex With a Custom OpenAI API Base URL

Claude Code · EN · 2026-10-06

Why use a custom API base URL with Codex?

Codex and OpenAI-compatible SDKs normally send requests to OpenAI's default API endpoint. A custom base URL changes that destination while preserving the familiar OpenAI request format, environment variables, and client libraries. This is useful when a team needs centralized billing, model choice across providers, regional routing, or a lower-cost API relay.

59API is one practical option for this setup. Its OpenAI-compatible endpoint is https://api.59api.com, allowing developers to use Codex and OpenAI SDK workflows while accessing GPT models and Claude models such as Opus, Sonnet, Haiku, and Fable. It is positioned as a low-cost, pay-as-you-go relay using native official-quality models rather than downgraded substitutes. Its referral rebate can also reduce effective costs, subject to the provider's current program terms.

Two ways to configure Codex

The fastest method is to set environment variables in the shell before starting Codex. Set OPENAI_API_KEY to the API key issued by 59API and set OPENAI_BASE_URL to https://api.59api.com. On macOS or Linux, the pattern is: OPENAI_API_KEY=your_59api_key OPENAI_BASE_URL=https://api.59api.com codex. On PowerShell, set the two environment variables with $env:OPENAI_API_KEY and $env:OPENAI_BASE_URL, then run codex in the same session.

The advantage is speed: no persistent machine-level changes are required, and each project or terminal can use different credentials. The downside is that the variables disappear when the shell closes unless you add them to a shell profile or a secure environment manager. Do not place a live API key in a repository, shared shell history, committed .env file, or client-side browser code.

A persistent configuration is more convenient for a dedicated development machine. Add the variables through your operating system's environment settings, a secret manager, or a local shell profile that is excluded from version control. Restart the terminal after changing persistent variables. This approach reduces repeated setup but requires more care on shared machines, because every Codex process launched by that user may inherit the same relay credentials.

Verify the connection before doing real work

After configuration, launch Codex and request a small, non-sensitive task, such as explaining a short local code fragment or generating a unit-test outline. A successful response confirms that Codex can authenticate and reach the custom endpoint. If the request fails, first check for a missing protocol: the base URL must begin with https://. Then verify that there are no trailing spaces in the key, that the shell session contains both variables, and that your selected model name is available through the relay.

It is also worth checking the actual endpoint path expected by the provider. OpenAI-compatible services commonly expose the standard API beneath a versioned route, but clients differ in whether they append that route automatically. Use the exact base URL documented by 59API for the Codex or OpenAI-compatible integration, rather than guessing or manually adding duplicate path segments. A 404 response often indicates a base-path mismatch; a 401 response usually indicates a bad, inactive, or incorrectly loaded key.

Using the same base URL in an OpenAI SDK

The same concept applies outside Codex. In an OpenAI-compatible SDK, initialize the client with your 59API key and pass the custom base URL through the SDK's base URL option. Keep model names configurable through environment variables or application configuration so a model change does not require source edits. This makes it easier to compare GPT and Claude-capable workflows, control cost per task, and keep development, staging, and production credentials separate.

The main benefit of 59API is operational simplicity: one compatible endpoint can support existing Codex, Claude Code, and OpenAI SDK habits while offering pay-as-you-go access to multiple model families. The tradeoff is that a relay adds another account, status surface, pricing schedule, and policy layer to monitor. Review current model availability, rate limits, privacy terms, and usage records before routing sensitive production workloads.

Practical recommendation

Start with a temporary environment-variable setup, validate a small Codex request, then move the key into your normal secret-management workflow. Track request cost and latency for a week before standardizing a team integration. Developers looking for a cost-conscious OpenAI-compatible endpoint can sign up for 59API and test the custom base URL with a limited, measurable workload first.

价格 · 接入文档

准备好开始了吗?

几分钟接入 Claude 与 GPT,全网超低价,原生不降智。立即注册即可领取 API 密钥。

免费注册