59API

← 返回教程列表

OpenAI SDK Custom Base URL in Python: A 59API Guide

API 使用 · EN · 2026-10-29

You can use Python’s OpenAI SDK with an OpenAI-compatible provider by changing the API endpoint and credentials. With 59API, that gives you pay-as-you-go access to multiple model providers through a familiar client. This quick-start guide covers installation, configuration, a working request, and the checks that prevent common connection errors.

1. Install the SDK and get your credentials

Use a Python virtual environment for your project, then install or update the official SDK:

python -m pip install --upgrade openai

You need a 59API API key and the exact API identifier for an available model. Copy both from your account or the service’s API documentation. A model’s display name can differ from its request identifier, so do not guess the value passed to the SDK.

For example, you might choose GPT-6 Astra or Claude Sonnet 5.5, depending on your task. Confirm that your chosen model supports the OpenAI-compatible endpoint you intend to call.

2. Configure the custom base URL

The SDK’s base_url setting controls where requests go. 59API’s API host is https://api.59api.com. Check its current documentation for the complete OpenAI-compatible base path: use the host alone if documented, or append /v1 if that is the specified path.

For a macOS or Linux shell, set these environment variables, replacing the placeholder values:

export OPENAI_API_KEY='your-59api-api-key'

export OPENAI_BASE_URL='https://api.59api.com'

export MODEL_ID='exact-model-id-from-59api'

If the documented base path includes /v1, change OPENAI_BASE_URL to https://api.59api.com/v1 before continuing. In Windows PowerShell, use assignments such as $env:OPENAI_API_KEY='your-59api-api-key' instead.

Use a 59API-issued key for 59API requests. Environment variables keep credentials out of your Python source, although you should also avoid exposing them in shell history, logs, or shared screenshots.

3. Make your first Python request

Run the following command in the same terminal. It explicitly passes the environment settings into the client, sends a short Chat Completions request, and prints the answer:

python -c 'import os; from openai import OpenAI; client = OpenAI(api_key=os.environ["OPENAI_API_KEY"], base_url=os.environ["OPENAI_BASE_URL"], timeout=30.0, max_retries=2); response = client.chat.completions.create(model=os.environ["MODEL_ID"], messages=[{"role": "user", "content": "Explain Python virtual environments in three sentences."}]); print(response.choices[0].message.content)'

The same client initialization works inside a regular Python script. Create the client once and reuse it for subsequent requests. The SDK also reads OPENAI_API_KEY and OPENAI_BASE_URL automatically when you omit those constructor arguments; passing them explicitly makes the configuration easier to inspect.

The timeout is measured in seconds. Setting max_retries=2 allows up to two retries for eligible failures, so a request’s total elapsed time can exceed one timeout interval.

4. Troubleshoot the right setting

5. Keep integration costs predictable

59API positions itself among the cheapest AI relays, with pay-as-you-go pricing and native, official-quality models without downgrades. A shared API interface also makes it convenient to compare models without maintaining a separate Python client for every provider.

Check current model pricing, measure token usage, and test representative prompts before increasing traffic. Compatibility with the SDK does not guarantee support for every OpenAI endpoint or feature. 59API also offers a referral rebate; review its current terms if relevant. To try this setup, sign up for 59API and start with a small test budget.

价格 · 接入文档

准备好开始了吗?

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

免费注册