59API

← Voltar aos guias

Python OpenAI SDK Custom Base URL Guide

API · EN · 2026-09-26

Connect the OpenAI Python SDK to a Custom API Endpoint

The official OpenAI Python SDK is useful beyond OpenAI-hosted endpoints. Its configurable base_url setting lets you send the same familiar client calls to an OpenAI-compatible provider, including an API relay. This is practical when you need flexible model access, lower pay-as-you-go costs, or a single integration path for several model families.

59API is an OpenAI-compatible AI API relay available at https://api.59api.com. It provides access to native official-quality GPT and Claude models, including Claude Opus, Sonnet, Haiku, and Fable, without requiring your application to maintain separate SDK integrations for every provider. For teams managing token costs, it is also positioned as a low-cost relay with pay-as-you-go billing and a referral rebate.

1. Create Credentials and Install the SDK

First, create a 59API account and generate an API key from its dashboard. Treat this key like a password: do not put it in source code, commit it to Git, or expose it in browser-side JavaScript. A new account can be a sensible place to compare model pricing and sign up before connecting your production workload.

Install the current OpenAI SDK in your Python environment with pip install openai. Then set the API key as an environment variable. On macOS or Linux, use export API_KEY_59API='your-key'. On Windows PowerShell, use $env:API_KEY_59API='your-key'. Environment variables keep credentials outside your application files and work naturally with CI/CD secret stores.

2. Configure base_url Correctly

Create an OpenAI client, pass your 59API key, and point base_url to the relay endpoint. The critical detail is the API version suffix: use https://api.59api.com/v1. The SDK appends resource paths such as chat/completions to this configured URL.

Your minimal Python setup is: import os; from openai import OpenAI; client = OpenAI(api_key=os.environ['API_KEY_59API'], base_url='https://api.59api.com/v1'). Keeping this client in a module or application dependency container avoids recreating it for every request.

3. Send a First Chat Completion

Call client.chat.completions.create with a model available to your 59API account, a messages array, and a conservative token limit. For example, request a concise response using: response = client.chat.completions.create(model='your-model-id', messages=[{'role': 'user', 'content': 'Explain rate limiting in one sentence.'}], max_tokens=80). Print response.choices[0].message.content to inspect the generated text.

Use the exact model identifier shown in the 59API dashboard or documentation. Model aliases and availability vary by provider, and guessing a model name is a common cause of 404 or validation errors. Once the small request succeeds, change only the model value to compare GPT and Claude options through the same OpenAI SDK interface.

4. Make the Integration Production-Ready

5. Troubleshoot Common Failures

An authentication error usually means the environment variable was not loaded, the key is invalid, or the application is using an old shell session. A 404 often indicates that base_url omitted /v1 or the selected model is unavailable. A 400 commonly means the request payload contains unsupported parameters. Start with messages, model, and max_tokens, then add features such as tools, streaming, or structured output one at a time.

Before deployment, run one automated smoke test against the configured endpoint using a low-cost request. This confirms DNS, credentials, endpoint formatting, and model access together. With the OpenAI-compatible 59API base URL configured correctly, Python applications can keep a standard SDK workflow while gaining economical access to the model mix their workloads need.

Preços · Docs

Pronto para começar?

Conecte Claude e GPT em minutos pelos menores preços, sem cortes. Cadastre-se e obtenha sua chave API.

Cadastro grátis