OpenAI SDK Custom Base URL in Python: A 59API Guide
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
- 401 authentication error: Check that the key belongs to 59API, is active, and contains no accidental whitespace.
- 404 response: Inspect the full request URL and documented endpoint path. Check for a missing or duplicated /v1 segment, or an unsupported route.
- Model unavailable: Verify the exact model identifier, account access, and endpoint compatibility. A dashboard display name may not be a valid API identifier.
- 429 response: Check the error details, account balance, and request limits. Reduce concurrency and use bounded retries with backoff where appropriate.
- Unsupported parameter: Start with model and messages, then add options individually. OpenAI-compatible services and upstream models can support different parameter sets.
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.