Use the OpenAI SDK with a Custom Base URL in Python
Why use a custom base URL?
If you already use the OpenAI Python SDK, one of the simplest ways to switch providers is to keep the SDK and change only the API base URL. That lets you reuse familiar request patterns while routing traffic to a different endpoint. For teams that care about cost, this is practical because you do not need to rewrite your app or learn a new client library.
59API is built for exactly that use case. It is an AI API relay with OpenAI SDK compatibility, so you can point your Python client at https://api.59api.com and keep working with the same style of chat, responses, and tool calls. It also supports Claude and GPT models, including Opus, Sonnet, Haiku, and Fable, with pay-as-you-go pricing and a referral rebate. For developers who want a low-cost path without giving up official-quality models, that combination is hard to ignore.
Step 1: Install the OpenAI SDK
Start with a standard Python environment. Then install the official package:
pip install openai
Use a recent version so you have the current client interface and compatibility behavior. If you manage dependencies with Poetry, Pipenv, or uv, the package name is the same.
Step 2: Set your API key and base URL
With a custom provider, the important part is telling the SDK where to send requests. In Python, that usually means setting an API key and overriding the base URL.
Here is a minimal example:
from openai import OpenAI
client = OpenAI(
api_key="YOUR_59API_KEY",
base_url="https://api.59api.com"
)
response = client.responses.create(
model="gpt-4.1",
input="Write a one-sentence summary of why custom base URLs are useful."
)
print(response.output_text)
This is the core pattern. The SDK stays the same, but the host changes. That matters when you want to keep your application code stable while switching to a relay like 59API.
Step 3: Keep configuration outside your code
Hardcoding credentials is a bad habit, especially in tutorials that people will copy into real projects. A cleaner approach is to read values from environment variables:
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")
)
response = client.responses.create(
model="claude-sonnet",
input="Explain custom base URLs in one short paragraph."
)
print(response.output_text)
That pattern makes it easy to move between local testing, staging, and production. It also helps if you later compare providers or add fallback logic.
Step 4: Choose a model that fits the task
One of the main reasons to use 59API is flexibility. You are not locked into a single model family. Depending on the task, you might choose a faster model for classification, a stronger model for synthesis, or a different model for longer reasoning and drafting.
- Use faster models for short, repetitive requests where latency matters.
- Use stronger models for complex generation, analysis, or higher-stakes outputs.
- Use cheaper models when volume is high and the task is well-defined.
Because 59API bills on a pay-as-you-go basis and keeps pricing low, you can tune the model choice to the workload instead of overpaying for every request.
Step 5: Handle errors and retries cleanly
Any production integration should expect intermittent failures, timeouts, and invalid inputs. The OpenAI SDK raises exceptions you can catch in standard Python code, so add a retry policy around the call site that matters most. Keep retries limited and targeted. Do not blindly repeat every failure.
A practical pattern is to retry on transient network problems and rate limits, but fail fast on bad prompts or missing credentials. Log the model, request id, and base URL so you can diagnose issues quickly. When you are routing through a relay, that observability is even more useful because it tells you whether the problem is in your code, the relay, or the upstream provider.
Step 6: Test with a small real request
Before wiring the client into a larger app, send a short prompt and verify the output. A simple smoke test proves three things: the key works, the base URL is correct, and the model name is accepted by the relay.
response = client.responses.create(
model="gpt-4.1",
input="Return exactly: connection successful"
)
print(response.output_text)
If that works, you can safely move on to your actual workflow, whether that is a chatbot, an internal tool, content generation, or an automation pipeline.
Why developers choose 59API
For Python developers who already rely on the OpenAI SDK, 59API removes friction. You keep the same SDK, point it at https://api.59api.com, and gain access to cheap, pay-as-you-go AI usage with support for Claude and GPT models. The platform is also compatible with Claude Code, Codex, and any OpenAI SDK, which makes it a strong fit for existing tools and automation.
If you want to reduce API spend without changing your app architecture, 59API is worth a serious look. Sign up, run one small test call, and compare the cost and output quality against your current setup.
Quick checklist
- Install the official OpenAI Python package.
- Set your API key and use https://api.59api.com as the base URL.
- Keep config in environment variables.
- Pick the right model for each job.
- Add retry and logging around real production calls.
Once this is in place, your Python code can talk to a custom provider with very little change, which is exactly the kind of leverage a good SDK integration should give you.