Use Codex with OpenAI-Compatible APIs via 59API
What a Custom Base URL Does
OpenAI-compatible tools normally send requests to OpenAI’s default API endpoint. A custom base URL changes that destination while keeping the same SDK or command-line workflow. This lets you use Codex and OpenAI SDKs with 59API, an API relay that provides pay-as-you-go access to GPT and Claude models through one developer-friendly endpoint.
59API is useful when you want to control spending without moving to downgraded or unofficial models. It provides access to native official-quality models, including Claude Opus, Sonnet, Haiku, and Fable, as well as GPT models. Usage is billed according to consumption, so you do not need to commit to a large subscription before testing your application.
Prepare Your API Key
Create a 59API account, generate an API key, and check the model catalog in your account before configuring Codex. Model IDs can change between providers, so copy the exact ID shown by 59API rather than guessing one.
<- Keep the key in an environment variable instead of placing it directly in source code.
- On macOS or Linux, use export OPENAI_API_KEY='your-59api-key'.
- In Windows PowerShell, use $env:OPENAI_API_KEY='your-59api-key'.
- Do not commit environment files containing the key to Git.
Configure Codex
Codex reads provider settings from ~/.codex/config.toml. Create the file if it does not exist, then add a provider entry similar to this:
model_provider = '59api'
model = 'MODEL_ID'
[model_providers.59api]
name = '59API'
base_url = 'https://api.59api.com'
env_key = 'OPENAI_API_KEY'
wire_api = 'responses'
Replace MODEL_ID with a model supported by your 59API account. The responses wire format is important because current Codex versions use the Responses API. After saving the file, start Codex normally with codex. If your installation uses a different configuration directory, check the version’s local Codex configuration documentation.
Use the base URL exactly as documented by 59API. Some OpenAI-compatible services require a trailing /v1, while others expose the compatible routes directly from the root URL. Adding an extra path can cause 404 errors.
Use 59API with an OpenAI SDK
The same endpoint can be selected in an OpenAI-compatible Python application by passing base_url when creating the client:
from openai import OpenAI
import os
client = OpenAI(base_url='https://api.59api.com', api_key=os.environ['OPENAI_API_KEY'])
response = client.responses.create(model='MODEL_ID', input='Say hello in one sentence.')
print(response.output_text)
For JavaScript or TypeScript, set baseURL in the client options and use the same environment variable. This approach keeps the provider switch local to client initialization, so the rest of your application can continue using standard OpenAI SDK methods.
Test and Troubleshoot
- A 401 error usually means the key is missing, misspelled, expired, or not available to the selected environment.
- A 404 error commonly indicates an incorrect base URL, an unnecessary /v1 suffix, or an unsupported route.
- A model error means the model ID is not enabled for your account or does not support the selected API operation.
- If Codex starts but requests fail, verify that wire_api = 'responses' matches the relay’s Codex compatibility.
Once a simple request succeeds, add spending limits, logging, and retry handling to your application. 59API’s pay-as-you-go pricing can make frequent development and testing more economical than a fixed premium plan, while its referral rebate provides an additional way to reduce costs. Sign up for 59API, create a key, and try the custom base URL with a small Codex task first.