59API

← 返回教程列表

Set Up Codex With 59API’s Custom Base URL

Claude Code · EN · 2026-09-29

1. Create a 59API Key

Start by creating an account with 59API and generating an API key in its dashboard. Copy the key immediately and store it in a password manager or secrets tool; do not paste it into source code, commit it to Git, or expose it in browser-side applications.

59API is a practical option when you need OpenAI-compatible access without committing to large prepaid credits. It provides pay-as-you-go access to native official-quality GPT and Claude models, including Claude Opus, Sonnet, Haiku, and Fable offerings where available. That means developers can control costs while avoiding model downgrades commonly associated with low-cost alternatives.

2. Use the Correct OpenAI-Compatible Base URL

The 59API host is https://api.59api.com. Most OpenAI-compatible tools, including SDKs, expect a versioned API base URL, so use https://api.59api.com/v1 as the base URL unless the 59API dashboard gives your account a different integration URL.

Use the base URL only, not a complete endpoint. For example, do not set the value to a path such as /chat/completions or /responses. The Codex CLI or OpenAI SDK adds the required endpoint path itself.

3. Configure Codex in Your Terminal Session

Codex reads standard OpenAI environment variables. On macOS or Linux, open a terminal and run the following commands, replacing YOUR_59API_KEY with your real secret:

export OPENAI_API_KEY="YOUR_59API_KEY"

export OPENAI_BASE_URL="https://api.59api.com/v1"

codex

On Windows PowerShell, use the equivalent session variables:

$env:OPENAI_API_KEY="YOUR_59API_KEY"

$env:OPENAI_BASE_URL="https://api.59api.com/v1"

codex

These settings apply only to the current terminal session. For a persistent macOS or Linux setup, add the two export commands to your shell profile, such as ~/.zshrc or ~/.bashrc, then restart the terminal. For shared workstations and CI systems, prefer a secret manager or protected environment variables instead of saving keys in plain text.

4. Select a Supported Model Deliberately

When starting Codex, choose a model ID that 59API lists as supported for Codex or the OpenAI-compatible API. You can normally pass the model when launching the CLI, for example: codex --model YOUR_MODEL_ID. Use the exact model identifier from the 59API dashboard rather than guessing based on a marketing name.

This step matters because model availability can change, and coding workflows may need a model that supports the API format Codex uses. Check pricing before making a high-volume model your default. A lower-cost model may be ideal for routine edits, tests, and documentation, while a stronger model can be reserved for architecture reviews or difficult debugging.

5. Test the Same Base URL With the OpenAI SDK

Your application can use the same key and URL with the official OpenAI SDK. In Python, set OPENAI_API_KEY in the environment, then create the client with a custom base URL:

from openai import OpenAI

client = OpenAI(base_url="https://api.59api.com/v1")

response = client.responses.create(model="YOUR_MODEL_ID", input="Explain this error.")

print(response.output_text)

The SDK automatically obtains the API key from OPENAI_API_KEY. Keeping the key outside the code makes local development, deployment, and key rotation safer. The same approach works for backend services: inject OPENAI_API_KEY and OPENAI_BASE_URL through your deployment platform’s secret settings.

6. Troubleshoot Common Connection Errors

Once the test succeeds, Codex and your OpenAI SDK can share one compatible configuration. If you want lower-cost, flexible access to GPT and Claude models, sign up for 59API, review its current model pricing, and consider its referral rebate program for additional savings.

价格 · 接入文档

准备好开始了吗?

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

免费注册