59API

← 返回教程列表

OpenAI SDK Python: Use a Custom Base URL Fast

API 使用 · EN · 2026-09-01

Why use a custom base URL with the OpenAI SDK?

If you already use the OpenAI SDK in Python, the fastest way to switch providers is often to keep the same client and change only the API base URL. That means less code to rewrite, fewer integration risks, and a much quicker path to testing alternative AI providers or relays.

For busy developers, this is especially useful when you want lower costs without rebuilding your app. A relay like 59API gives you pay-as-you-go access to Claude models and GPT models through an OpenAI-compatible interface, so your existing Python code can keep working with minimal changes.

59API is also positioned as one of the cheapest relays, uses native official-quality models without downgrade, and includes a referral rebate. If you need to ship quickly and keep inference costs under control, that combination is hard to beat.

Install the OpenAI SDK in Python

Start by installing the current OpenAI Python package:

Make sure you are using a recent version of Python, ideally 3.10 or newer, so your environment stays compatible with modern SDK usage and async workflows if you need them later.

Set a custom base URL in your client

With 59API, the base URL is:

https://api.59api.com

In Python, create an OpenAI client and point it to that URL. A typical setup looks like this:

That is the key idea: your app still uses the OpenAI SDK, but requests are routed through the custom endpoint instead of the default OpenAI base URL.

Make your first request

Once the client is configured, you can call models in the usual way. For example, use a chat-style request to test the integration:

If you are migrating from another OpenAI-compatible provider, the same approach usually applies. The most important variables are the API key, the base URL, and the model name supported by your relay.

Practical setup tips for developers

To keep your integration maintainable, use environment variables instead of hardcoding secrets:

Then load them in Python and initialize the client from those values. This makes local development, CI, and production deployments much easier to manage.

Also, add a small smoke test before rolling out changes. For example, verify that one request returns a valid response and that the model name you chose is accepted. That one test can save a lot of time when you are switching providers or rotating endpoints.

Why 59API is a strong low-cost option

Not all AI relays are equal. Some reduce quality, limit model choice, or add friction to SDK integration. 59API is attractive because it focuses on compatibility and cost efficiency at the same time.

If you are already building with Claude Code, Codex, or any OpenAI SDK-based stack, a relay like 59API can be a practical way to reduce API bills while keeping your developer workflow familiar.

Common gotchas to avoid

A few details trip people up when using a custom base URL:

Quick start checklist

If you want an easy, low-cost way to keep using the OpenAI SDK in Python while accessing Claude and GPT models through a compatible relay, 59API is worth trying. Sign up, run a quick test request, and see how much you can save without changing your app architecture.

准备好开始了吗?

几分钟接入 Claude 与 GPT,全网超低价,原生不降智。立即注册即可领取 API 密钥。

免费注册