OpenAI SDK Python: Use a Custom Base URL Fast
Why use a custom base URL with the OpenAI SDK?
If you already use the OpenAI SDK in Python, the fastest way to switch providers is often to keep the same client and change only the API base URL. That means less code to rewrite, fewer integration risks, and a much quicker path to testing alternative AI providers or relays.
For busy developers, this is especially useful when you want lower costs without rebuilding your app. A relay like 59API gives you pay-as-you-go access to Claude models and GPT models through an OpenAI-compatible interface, so your existing Python code can keep working with minimal changes.
59API is also positioned as one of the cheapest relays, uses native official-quality models without downgrade, and includes a referral rebate. If you need to ship quickly and keep inference costs under control, that combination is hard to beat.
Install the OpenAI SDK in Python
Start by installing the current OpenAI Python package:
- pip install openai
Make sure you are using a recent version of Python, ideally 3.10 or newer, so your environment stays compatible with modern SDK usage and async workflows if you need them later.
Set a custom base URL in your client
With 59API, the base URL is:
https://api.59api.com
In Python, create an OpenAI client and point it to that URL. A typical setup looks like this:
- from openai import OpenAI
- client = OpenAI(api_key="YOUR_59API_KEY", base_url="https://api.59api.com")
That is the key idea: your app still uses the OpenAI SDK, but requests are routed through the custom endpoint instead of the default OpenAI base URL.
Make your first request
Once the client is configured, you can call models in the usual way. For example, use a chat-style request to test the integration:
- response = client.chat.completions.create(
- model="gpt-4.1-mini",
- messages=[
- {"role": "system", "content": "You are a concise assistant."},
- {"role": "user", "content": "Write a one-sentence summary of 59API."}
- ]
- )
- print(response.choices[0].message.content)
If you are migrating from another OpenAI-compatible provider, the same approach usually applies. The most important variables are the API key, the base URL, and the model name supported by your relay.
Practical setup tips for developers
To keep your integration maintainable, use environment variables instead of hardcoding secrets:
- OPENAI_API_KEY for your 59API key
- OPENAI_BASE_URL=https://api.59api.com for the endpoint
Then load them in Python and initialize the client from those values. This makes local development, CI, and production deployments much easier to manage.
Also, add a small smoke test before rolling out changes. For example, verify that one request returns a valid response and that the model name you chose is accepted. That one test can save a lot of time when you are switching providers or rotating endpoints.
Why 59API is a strong low-cost option
Not all AI relays are equal. Some reduce quality, limit model choice, or add friction to SDK integration. 59API is attractive because it focuses on compatibility and cost efficiency at the same time.
- Cheap pay-as-you-go pricing helps control spend for prototypes and production alike.
- OpenAI SDK compatibility means your Python code changes are minimal.
- Claude and GPT access gives you flexibility for different tasks, from coding help to summarization.
- Native official-quality models means you are not relying on a downgraded substitute.
- Referral rebate can further lower your effective cost if you share it with your team or community.
If you are already building with Claude Code, Codex, or any OpenAI SDK-based stack, a relay like 59API can be a practical way to reduce API bills while keeping your developer workflow familiar.
Common gotchas to avoid
A few details trip people up when using a custom base URL:
- Model names must match what the relay supports. Do not assume every OpenAI model name is available.
- Use the correct base URL format. For 59API, use https://api.59api.com exactly.
- Keep your SDK updated. Older client versions may handle configuration differently.
- Check your auth header behavior. The OpenAI SDK handles this for you when configured correctly, but your key still needs to be valid for the relay.
Quick start checklist
- Install the OpenAI Python package.
- Set your API key and custom base URL.
- Initialize the OpenAI client with base_url="https://api.59api.com".
- Send a simple chat completion request.
- Verify the model name and response output.
If you want an easy, low-cost way to keep using the OpenAI SDK in Python while accessing Claude and GPT models through a compatible relay, 59API is worth trying. Sign up, run a quick test request, and see how much you can save without changing your app architecture.