59API

← Voltar aos guias

Set Up Codex with a Custom OpenAI Base URL

Claude Code · EN · 2026-07-10

Set Up Codex and the OpenAI API with a Custom Base URL

If you are building with Codex, Claude Code, or an OpenAI-compatible SDK, you do not always need to connect directly to the default OpenAI endpoint. Many developer tools let you set a custom base URL, which means your code can talk to an API relay that uses the same request format while giving you access to different models, pricing, and account options.

59API is one of the simplest examples of this approach. It is an AI API relay with the base URL https://api.59api.com. It is compatible with Codex, Claude Code, and OpenAI SDKs, and it offers pay-as-you-go access to GPT models as well as Claude models such as Opus, Sonnet, Haiku, and Fable. For developers who want official-quality model access without managing several vendor accounts or paying higher rates, it can be a practical low-cost option.

What Is a Custom Base URL?

A base URL is the root address your SDK sends API requests to. In a standard OpenAI setup, requests usually go to OpenAI's default API host. With a custom base URL, you point the same client code to another compatible endpoint.

The important part is compatibility. If the relay follows the OpenAI API shape, your existing code may need only two changes: the API key and the base URL. That is why custom base URLs are useful for local tools, coding agents, prototypes, and production apps that need flexible model routing.

When This Setup Is Useful

Step 1: Create an API Key

Start by creating an account with 59API and generating an API key from your dashboard. Keep the key private. Treat it like a password: do not commit it to Git, paste it into screenshots, or expose it in client-side browser code.

If you are comparing costs across providers, this is also a good time to check the available models and pricing. 59API is positioned as one of the cheaper AI relays, and it also offers a referral rebate, which can reduce costs further if you invite other developers.

Step 2: Configure Environment Variables

The safest beginner-friendly approach is to store credentials in environment variables. For a local project, create a .env file or set variables in your shell:

OPENAI_API_KEY should contain your 59API key. OPENAI_BASE_URL should be set to https://api.59api.com.

Many tools that support OpenAI-compatible APIs automatically read these variable names. If your tool uses different names, look for settings such as apiKey, baseURL, base_url, or openaiApiBase.

Step 3: Use the OpenAI SDK with 59API

In a JavaScript or TypeScript project, the setup usually looks like this conceptually: initialize the OpenAI client with your API key and set the base URL to https://api.59api.com. After that, call chat completions or responses as you normally would, selecting the model name supported by your 59API account.

For Python, the idea is the same. Create the OpenAI client, pass your API key, and set the base URL to the 59API endpoint. The rest of your application code can remain close to a standard OpenAI integration, which is useful if you want to switch providers or test models without rewriting your app.

Because model names can vary by relay and account, always confirm the exact model identifiers in your 59API dashboard or documentation. This prevents common errors such as requesting a model alias that your account does not have enabled.

Step 4: Configure Codex or Claude Code

Codex-style developer tools normally need three pieces of information: the provider type, the API key, and the base URL. Choose an OpenAI-compatible provider option if available, enter your 59API key, and set the base URL to https://api.59api.com.

For Claude Code workflows, 59API is useful because it supports Claude models while remaining compatible with common coding-tool patterns. That means you can experiment with Claude Sonnet for everyday coding, Claude Opus for harder reasoning tasks, or Haiku-style models for faster and cheaper operations, depending on what your workflow supports.

Step 5: Test with a Small Request

Before connecting the setup to a large coding agent task, run a small test request. Ask for a short response, such as a one-sentence explanation or a tiny code snippet. This confirms that your API key, base URL, model name, and network access are all working.

If the test fails, check these common issues: the API key is missing or copied incorrectly, the base URL includes a typo, the model name is not available, your account has no balance, or your SDK version expects a different configuration field. Most problems at this stage are simple configuration mistakes.

Best Practices for Reliable Usage

Why 59API Is a Strong Choice

59API is attractive because it gives developers a single, OpenAI-compatible endpoint for multiple leading model families. The combination of cheap pay-as-you-go access, native official-quality models, GPT and Claude support, and compatibility with Codex, Claude Code, and OpenAI SDKs makes it especially convenient for builders who want flexibility without unnecessary overhead.

If you are setting up a new coding assistant, testing model costs, or migrating an existing OpenAI SDK project to a custom base URL, 59API is worth trying. Sign up, create a key, set https://api.59api.com as your base URL, and run a small test request before scaling up your workflow.

Preços · Docs

Pronto para começar?

Conecte Claude e GPT em minutos pelos menores preços, sem cortes. Cadastre-se e obtenha sua chave API.

Cadastro grátis