Codex Custom Base URL: 59API Setup and Error Fixes
What changes when you use a custom API base URL?
A custom base URL routes requests to an alternative API service instead of OpenAI directly. With 59API, you use a 59API-issued key, its endpoint, and a model identifier supported by the relay. Your OpenAI account key and ChatGPT subscription do not authenticate requests to 59API.
59API offers pay-as-you-go access to GPT and Claude models and compatibility with Codex, Claude Code, and OpenAI SDKs. It positions itself among the cheapest relays, offering native, official-quality models without downgrading. For developers running frequent coding tasks, that combination can make API spending easier to control.
1. Confirm the endpoint, key, and model
The service address is https://api.59api.com. OpenAI-compatible clients generally need the versioned API root: https://api.59api.com/v1. Confirm this against 59API’s current documentation before configuring your client.
- Create a 59API API key and check that your account has sufficient balance.
- Copy an available model identifier exactly from the dashboard or documentation. Display names and API identifiers may differ.
- Store the key in an environment variable rather than source code.
On macOS or Linux, set the key in the terminal where you will launch Codex:
export API_59_KEY='YOUR_59API_KEY'
On Windows PowerShell, use:
$env:API_59_KEY = 'YOUR_59API_KEY'
These commands affect the current shell session. A new terminal may require setting the variable again.
2. Configure Codex with a custom provider
For a current Codex CLI installation, edit ~/.codex/config.toml. Add the following lines in order, replacing the model placeholder with an actual supported identifier:
model = "MODEL_ID_FROM_59API"
model_provider = "relay59"
[model_providers.relay59]
name = "59API"
base_url = "https://api.59api.com/v1"
env_key = "API_59_KEY"
wire_api = "responses"
Keep the first two settings at the top level, before any table header. If your file already contains them, update the existing entries instead of creating duplicate keys.
Launch codex from the same terminal. This configuration selects API-key authentication through the provider’s environment variable; it does not require using an OpenAI key for the relay.
Important: Codex relies on the Responses API and coding-related capabilities, including streaming and tool calls. Select a model and route that support those features. A successful chat-completions request alone does not establish Codex compatibility.
3. Point the OpenAI SDK at 59API
For the Python SDK, install or update the package with python -m pip install --upgrade openai. Then initialize the client explicitly:
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["API_59_KEY"], base_url="https://api.59api.com/v1")
For a Responses API test, use:
response = client.responses.create(model="MODEL_ID_FROM_59API", input="Reply with OK.")
print(response.output_text)
Explicit configuration avoids relying on environment-variable behavior that can differ between applications. Do not put /responses or /chat/completions into base_url; the SDK appends the resource path.
Troubleshooting: common errors and practical fixes
- 401 Unauthorized: Check that API_59_KEY exists in the launching terminal and contains a 59API key. Remove accidental whitespace and confirm the key has not been revoked.
- 403 Forbidden: Check account permissions, model access, and any key restrictions. Read the response body before assuming the balance is the issue.
- 404 Not Found: Inspect the requested URL for missing or duplicated /v1. Also distinguish an unavailable endpoint from an unknown model.
- Model not found: Replace the placeholder with an exact supported model ID. Do not assume another provider’s model aliases work.
- 429 or quota errors: Check balance and rate limits. Reduce concurrency and use bounded exponential backoff for retryable failures.
- Streaming or tool-call failures: Update Codex and the SDK, then confirm Responses API support for the selected model. Check whether a proxy buffers streaming responses.
FAQ: costs, compatibility, and key safety
Can I use Claude models in Codex? Not automatically. Claude Code compatibility and Codex compatibility are separate; Codex needs a route with its expected Responses behavior.
Will a lower price mean lower-quality models? 59API states that it provides native, official-quality models without downgrading. Compare current model-specific pricing and test representative tasks before moving a production workload.
What about referrals and security? 59API offers a referral rebate; check current eligibility and terms. Keep keys out of Git and screenshots, and remember that a relay processes your API traffic.
If you want to compare costs firsthand, sign up for 59API and start with a small, controlled Codex workload.
Prêt à commencer ?
Connectez Claude et GPT en quelques minutes aux prix les plus bas, sans bridage. Inscrivez-vous pour votre clé API.
Inscription gratuite