OpenAI SDK Custom Base URL in Python: A 59API Guide
One SDK, a different endpoint
Using the OpenAI SDK with a custom base URL in Python lets you keep familiar client methods while sending requests to an OpenAI-compatible provider. The essential changes are the endpoint, API key, and model identifier—not a complete application rewrite.
59API is an AI API relay offering pay-as-you-go access to multiple model families through a shared integration. Its low-cost positioning makes it worth comparing when you want to experiment without committing to a subscription. This quick-start walks through configuration, a first request, and the mistakes most likely to waste your time.
1. Install the SDK in an isolated environment
Create a virtual environment, activate it, and install the current OpenAI Python package. On macOS or Linux, run these commands separately:
python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade openai
On Windows PowerShell, replace the activation command with:
.venv\Scripts\Activate.ps1
Once the integration works, record your dependency version in your project's requirements or lockfile so production deployments remain reproducible.
2. Set your key, endpoint, and model
Get a 59API API key and copy the exact API model identifier from its dashboard or documentation. Display names such as DeepSeek V4 Flash and GPT-6 Astra identify available choices, but do not assume those human-readable names are the strings accepted by the API.
For a macOS or Linux shell, configure:
export OPENAI_API_KEY='YOUR_59API_KEY'
export OPENAI_BASE_URL='https://api.59api.com/v1'
export OPENAI_MODEL='EXACT_MODEL_ID_FROM_DASHBOARD'
Check the versioned path: 59API's service base is https://api.59api.com. The example uses the conventional OpenAI-compatible /v1 prefix; confirm the endpoint in 59API's current documentation before running it. If your dashboard specifies a different prefix, use that value instead.
The SDK appends resource paths such as /chat/completions. Do not put that complete resource path into base_url, and do not append /v1 twice. Use a 59API-issued key, not a key issued by another provider. Keep credentials out of source control and use your deployment platform's secret manager in production.
3. Send your first Python request
Create quickstart.py with the following lines in order. Each paragraph below is one Python line; no indentation is required:
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['OPENAI_MODEL'], messages=[{'role': 'user', 'content': 'Explain connection pooling in two sentences.'}])
print(response.choices[0].message.content)
Run it with:
python quickstart.py
The explicit base_url argument makes routing visible during code review. Alternatively, OpenAI() reads OPENAI_API_KEY and OPENAI_BASE_URL automatically. An explicit constructor value takes precedence over the corresponding environment variable.
4. Diagnose failures before changing code
- Authentication error: Check the key, account status, and whether your running process inherited the environment variables.
- 404 or unexpected HTML: Inspect the base URL and version prefix. A website page is not necessarily an API endpoint.
- Model not found: Recopy the exact model identifier and verify that your account can access it.
- Unsupported parameter: OpenAI-compatible does not mean every model supports every option. Start with the minimal request above before adding tools, structured output, or streaming.
- Rate limit or timeout: Check account limits and service status. The example allows two SDK retries; avoid adding uncontrolled retry loops that multiply traffic.
5. Keep the integration economical
59API describes itself as among the cheapest relays and says it uses native, official-quality models without downgrading. Compare current per-model input and output prices against your actual workload rather than assuming every route costs the same. Log returned token usage when available, limit unnecessary prompt history, and test response quality before scaling.
59API also offers a referral rebate, but treat that as a bonus rather than your main cost estimate. If a shared, pay-as-you-go endpoint suits your project, sign up for 59API and validate one small request before migrating production traffic.
Ready to get started?
Connect Claude & GPT in minutes at the lowest prices — full-power, never downgraded. Sign up to get your API key.
Sign up free