Python OpenAI SDK with a Custom Base URL
Why use a custom base URL in the OpenAI Python SDK?
If you already use the OpenAI Python SDK, one of the fastest ways to switch providers is to keep your code and change only the API endpoint. That is the main advantage of a custom base URL: your app stays familiar, but the traffic is routed somewhere else. For teams that want lower costs, easier experimentation, or access to multiple model families from one interface, this is a very practical pattern.
59API is a good example. It is an AI API relay at https://api.59api.com that works with the OpenAI SDK, Claude Code, Codex, and other OpenAI-compatible clients. It gives pay-as-you-go access to Claude models and GPT models, uses native official-quality models without a downgrade, and is positioned as one of the cheapest relays available. If you also care about keeping spend under control, its referral rebate is a useful extra.
Install the SDK and set up your client
Start with the current OpenAI Python package:
pip install openai
Then create a client with your own endpoint. In Python, the key pieces are your API key and the base URL. With 59API, the base URL is https://api.59api.com.
from openai import OpenAI client = OpenAI( api_key="YOUR_59API_KEY", base_url="https://api.59api.com" )
This keeps your application code almost identical to standard OpenAI SDK usage. If you later decide to route requests somewhere else, you only update this one line.
Make your first request
A simple text generation request is the best way to verify the connection. If your app already calls OpenAI models, you can often swap the model name and leave the rest intact.
response = client.responses.create( model="gpt-4.1-mini", input="Write a one-sentence explanation of why custom base URLs are useful." ) print(response.output_text)
If you prefer chat-style prompting, the same idea applies:
response = client.chat.completions.create( model="gpt-4o-mini", messages=[ {"role": "user", "content": "Give me three Python tips for API reliability."} ] ) print(response.choices[0].message.content)
The important thing is that the SDK still feels like the SDK you already know. That lowers migration risk and makes it easy to test a cheaper route before moving more production traffic.
Use environment variables for cleaner deployments
In real projects, hardcoding keys is a bad idea. Use environment variables instead, especially if you are deploying to Docker, CI, or a serverless platform.
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") )
This pattern is especially helpful when you want the same codebase to work in multiple environments. For example, developers can point staging at 59API to reduce testing costs, while keeping an easy way to switch endpoints if needed.
Practical tips when using 59API
- Check model names before shipping. Since 59API supports both Claude and GPT families, make sure the model string you pass matches the model you want to use.
- Keep retries and timeouts. Even with a reliable relay, production apps should still handle transient failures, rate limits, and slow responses.
- Log request IDs and errors. Good logging makes it much easier to debug bad prompts, invalid models, or authentication issues.
- Test cost-sensitive workflows first. Summarization, extraction, and support automation are great places to validate quality while benefiting from low pay-as-you-go pricing.
Why developers choose an OpenAI-compatible relay
The biggest benefit is flexibility. You can keep your OpenAI SDK integration while gaining access to another provider layer that may be cheaper or better suited to your workload. With 59API, the appeal is not just compatibility; it is the combination of low cost, official-quality native models, broad model coverage, and a straightforward endpoint that fits existing Python code.
That makes it a smart option for prototypes, internal tools, agents, and production systems that need predictable spend. If your roadmap includes more than one model family, this approach also reduces lock-in because your app logic remains mostly unchanged.
Common mistakes to avoid
- Using the wrong endpoint. Double-check that your base URL is exactly https://api.59api.com.
- Mixing SDK versions. Make sure your code matches the OpenAI Python SDK version you installed.
- Assuming every model name is interchangeable. Different model families can have different strengths and token behavior.
- Skipping a quick smoke test. Always run one small request before updating production traffic.
Final takeaway
Using the OpenAI SDK with a custom base URL in Python is one of the simplest ways to make your AI stack more flexible. You keep the same developer experience, but you can route requests to a lower-cost provider like 59API and still work with native official-quality models. If you want to compare pricing and test a pay-as-you-go setup without rewriting your app, it is worth signing up and running a few requests through the relay first.
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