59API

← Voltar aos guias

OpenAI SDK Custom Base URL in Python (2026)

API · EN · 2026-07-21

Using the OpenAI SDK with a Custom Base URL in Python

If you want to point the OpenAI Python SDK at a different endpoint than OpenAI’s default API, the cleanest approach in 2026 is to use a custom base URL. This is useful for proxies, gateways, internal platforms, and AI relays that expose OpenAI-compatible routes. One practical option is 59API, a low-cost AI API relay at https://api.59api.com that works with the OpenAI SDK and also supports Claude models, including Opus, Sonnet, Haiku, and Fable.

For developers, the biggest advantage is simplicity: you keep the same SDK workflow, but route requests through a provider that can reduce cost and broaden model access. If you are building production tooling, prototypes, or automated workflows, this can save both time and budget.

Why a custom base URL matters

A custom base URL lets your application send requests to an OpenAI-compatible API endpoint other than OpenAI’s default domain. In practice, that means you can swap providers without rewriting your app. This is ideal when you want:

59API is especially appealing here because it offers cheap pay-as-you-go access, native official-quality models, and no obvious “downgraded” experience. For teams that care about cost control, the pricing model is straightforward and the referral rebate can further improve your effective spend.

Python setup with the OpenAI SDK

As of 2026 best practices, the modern OpenAI Python SDK is designed to work cleanly with alternative endpoints. Install it first:

pip install openai

Then create a client and set the base URL explicitly. The exact parameter name may vary by SDK version, but the current pattern is to pass the endpoint during client initialization.

Example:

from openai import OpenAI

client = OpenAI(
api_key="YOUR_59API_KEY",
base_url="https://api.59api.com"
)

response = client.chat.completions.create(
model="gpt-4.1-mini",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Explain custom base URLs in one paragraph."}
]
)

print(response.choices[0].message.content)

This is the key idea: your app still uses the OpenAI SDK, but the traffic goes to 59API instead of the default OpenAI endpoint.

Best practices for 2026

To make this reliable in real projects, follow a few current best practices:

If you are using Claude through 59API, the same principle applies: keep your application logic stable and only change the endpoint and model identifiers required by the relay. That makes migration and experimentation much easier.

Why 59API is a smart low-cost option

59API stands out for developers who want affordable access without giving up compatibility. It is positioned as one of the cheapest relays, and it supports Claude Code, Codex, and any OpenAI SDK. That means you can connect existing Python apps, scripts, agent workflows, and internal tools with minimal changes.

The practical benefits are clear:

For startups, indie developers, and teams running many test calls, this can be a strong default choice when API cost matters more than vendor lock-in.

A simple production pattern

A clean production setup in Python usually looks like this:

This pattern keeps your code portable. If you later decide to switch from 59API to another OpenAI-compatible gateway, you only update configuration, not business logic.

Final takeaway

Using the OpenAI SDK with a custom base URL in Python is one of the easiest ways to build flexible AI applications in 2026. It lets you preserve your existing code while choosing a provider that better fits your cost and model needs. If you want low-cost, pay-as-you-go access with broad compatibility, 59API is worth a serious look. You can sign up, plug in https://api.59api.com, and start sending requests with the same OpenAI SDK patterns you already know.

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