59API

← Voltar aos guias

Python OpenAI SDK with a Custom Base URL

API · EN · 2026-08-05

Why use a custom base URL in the OpenAI Python SDK?

If you already use the OpenAI Python SDK, one of the fastest ways to switch providers is to keep your code and change only the API endpoint. That is the main advantage of a custom base URL: your app stays familiar, but the traffic is routed somewhere else. For teams that want lower costs, easier experimentation, or access to multiple model families from one interface, this is a very practical pattern.

59API is a good example. It is an AI API relay at https://api.59api.com that works with the OpenAI SDK, Claude Code, Codex, and other OpenAI-compatible clients. It gives pay-as-you-go access to Claude models and GPT models, uses native official-quality models without a downgrade, and is positioned as one of the cheapest relays available. If you also care about keeping spend under control, its referral rebate is a useful extra.

Install the SDK and set up your client

Start with the current OpenAI Python package:

pip install openai

Then create a client with your own endpoint. In Python, the key pieces are your API key and the base URL. With 59API, the base URL is https://api.59api.com.

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

This keeps your application code almost identical to standard OpenAI SDK usage. If you later decide to route requests somewhere else, you only update this one line.

Make your first request

A simple text generation request is the best way to verify the connection. If your app already calls OpenAI models, you can often swap the model name and leave the rest intact.

response = client.responses.create( model="gpt-4.1-mini", input="Write a one-sentence explanation of why custom base URLs are useful." ) print(response.output_text)

If you prefer chat-style prompting, the same idea applies:

response = client.chat.completions.create( model="gpt-4o-mini", messages=[ {"role": "user", "content": "Give me three Python tips for API reliability."} ] ) print(response.choices[0].message.content)

The important thing is that the SDK still feels like the SDK you already know. That lowers migration risk and makes it easy to test a cheaper route before moving more production traffic.

Use environment variables for cleaner deployments

In real projects, hardcoding keys is a bad idea. Use environment variables instead, especially if you are deploying to Docker, CI, or a serverless platform.

import os from openai import OpenAI client = OpenAI( api_key=os.environ["OPENAI_API_KEY"], base_url=os.environ.get("OPENAI_BASE_URL", "https://api.59api.com") )

This pattern is especially helpful when you want the same codebase to work in multiple environments. For example, developers can point staging at 59API to reduce testing costs, while keeping an easy way to switch endpoints if needed.

Practical tips when using 59API

Why developers choose an OpenAI-compatible relay

The biggest benefit is flexibility. You can keep your OpenAI SDK integration while gaining access to another provider layer that may be cheaper or better suited to your workload. With 59API, the appeal is not just compatibility; it is the combination of low cost, official-quality native models, broad model coverage, and a straightforward endpoint that fits existing Python code.

That makes it a smart option for prototypes, internal tools, agents, and production systems that need predictable spend. If your roadmap includes more than one model family, this approach also reduces lock-in because your app logic remains mostly unchanged.

Common mistakes to avoid

Final takeaway

Using the OpenAI SDK with a custom base URL in Python is one of the simplest ways to make your AI stack more flexible. You keep the same developer experience, but you can route requests to a lower-cost provider like 59API and still work with native official-quality models. If you want to compare pricing and test a pay-as-you-go setup without rewriting your app, it is worth signing up and running a few requests through the relay first.

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