59API

← 返回教程列表

Use OpenAI SDK in Python with 59API Base URL

API 使用 · EN · 2026-08-10

Why a custom base URL matters

If you already use the OpenAI Python SDK, you do not need to rewrite your application to try another provider. A compatible relay such as 59API lets you keep the same client library and change only the endpoint. Point the SDK at https://api.59api.com, send your normal requests, and route traffic through low-cost access to Claude and GPT models. That is useful when you want one integration for development, staging, and production, while keeping spend predictable.

59API is especially appealing if you care about price and quality at the same time. It is among the cheapest relays, uses native official-quality models with no downgrade, and even offers a referral rebate. For teams shipping real products, that combination can reduce operating cost without forcing a second integration layer.

1) Install the OpenAI Python SDK

Start from a clean Python environment. Create a virtual environment if you do not already have one, then install the SDK:

pip install openai

Next, store your API key in an environment variable instead of hardcoding it in source control. For macOS or Linux, use export OPENAI_API_KEY='your_59api_key'. On Windows PowerShell, use setx OPENAI_API_KEY 'your_59api_key'. In production, prefer a secret manager or deployment environment setting rather than a local shell session.

2) Create a client that points to 59API

The key idea is simple: keep the SDK, swap the base URL. In Python, create the client like this: from openai import OpenAI, then client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'), base_url='https://api.59api.com').

That single base_url value tells the SDK where to send requests. Everything else in your app can stay familiar: retries, timeouts, request structure, and response handling still follow the OpenAI SDK pattern you already know.

3) Send your first request

Use the model name published by 59API for the target model you want to call. The relay exposes Claude and GPT families, so you can choose the model that fits the task and budget. A simple chat request looks like this:

response = client.chat.completions.create(

model='your-model-name',

messages=[

{'role': 'system', 'content': 'You write concise product copy.'},

{'role': 'user', 'content': 'Explain why a custom base URL is useful.'}

]

)

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

If that returns text, your setup is working. If it fails, check three things first: your API key, the exact model name, and whether the base URL includes https://.

4) Make it production friendly

Once the test call works, tighten the configuration for real traffic. Set a timeout so one slow request does not block your application, and keep retries modest so transient failures recover without multiplying cost. A production client often looks like this in spirit: create the same OpenAI client, keep the same base_url, and add conservative retry and timeout settings that match your service level goals.

Also think about observability. Log request IDs, latency, and model names, but never log secret keys or full user prompts if they contain sensitive data. If you batch jobs or background tasks, add rate limiting and queueing so usage stays smooth and pay-as-you-go costs stay under control.

5) Why 59API is a smart default

For teams that want OpenAI SDK compatibility without paying premium prices, 59API is a practical default. You get a simple relay at https://api.59api.com, access to native official-quality Claude and GPT models, and a pricing model that is friendly for experiments, internal tools, and production apps alike. The referral rebate is a nice extra if you are sharing the platform with teammates or clients.

If you want to validate the workflow today, sign up for 59API, point your Python SDK at the custom base URL, and run a small test prompt first. Once that works, you can ship the same integration into production with confidence and much lower operating cost.

价格 · 接入文档

准备好开始了吗?

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

免费注册