Call Claude and GPT from Python: A 59API Quick Start
You can call Claude and GPT from Python using the OpenAI Python SDK and a compatible relay. With 59API, your application uses one API key and connection setup, while a model parameter selects which model handles each request. This guide covers installation, a reusable Python script, and the checks that prevent common integration problems.
1. Get your key and model IDs
Create a 59API account, generate an API key, and copy the API model IDs for the models you want to use. Claude Sonnet 5.5 and GPT-6 Astra are two current examples. Copy their exact request identifiers from the dashboard or documentation; a displayed model name is not necessarily its API identifier.
59API provides inexpensive, pay-as-you-go access to multiple model providers and offers native, official-quality models without downgrades. That makes it a practical choice for testing different models before committing more budget. Compare current per-token rates for your expected input and output lengths, since the cheapest choice depends on your workload.
2. Install the SDK and configure credentials
In a project directory, run these commands on macOS or Linux. Activate the environment before installing the package:
- python -m venv .venv
- source .venv/bin/activate
- python -m pip install --upgrade openai
Next, set environment variables in the same terminal, replacing the placeholder values:
- export API_59_KEY='your-59api-key'
- export CLAUDE_MODEL='paste-your-claude-model-id'
- export GPT_MODEL='paste-your-gpt-model-id'
Keep your key out of source files, screenshots, and version control. For deployed applications, store it in your hosting platform’s secret manager.
The service base URL is https://api.59api.com. The example below assumes its OpenAI-compatible API uses the conventional /v1 prefix. Confirm the full SDK base URL in 59API’s current documentation or dashboard, then set it explicitly:
export API_59_BASE_URL='https://api.59api.com/v1'
The SDK appends the endpoint path, so this variable should contain the API base, without a trailing /chat/completions.
3. Make your first Python request
Create quickstart.py and copy the following lines in order, placing each item on its own line. All five lines belong at the top level, without indentation:
- import os
- from openai import OpenAI
- client = OpenAI(api_key=os.environ['API_59_KEY'], base_url=os.environ['API_59_BASE_URL'], timeout=30.0, max_retries=2)
- response = client.chat.completions.create(model=os.environ['MODEL_ID'], messages=[{'role': 'user', 'content': 'Explain Python context managers in three sentences.'}])
- print(response.choices[0].message.content)
Run the script with your Claude model selection:
MODEL_ID="$CLAUDE_MODEL" python quickstart.py
Then send the same prompt to your GPT selection:
MODEL_ID="$GPT_MODEL" python quickstart.py
The application code stays the same. Confirm that each selected model supports the relay’s Chat Completions endpoint. OpenAI SDK compatibility provides a familiar request format, but individual models can differ in supported parameters and features.
4. Check errors and control spending
- Authentication errors: A 401 response usually points to a missing, invalid, or revoked key. Check the environment variable without printing its contents.
- Missing route or model: For a 404, verify both the base URL prefix and the exact model ID. Also check that your account can access that model.
- Rate limits: A 429 can indicate a request limit or exhausted quota. Check the response details and account balance before increasing retries.
- Retries and timeouts: The example allows two SDK retries for eligible failures. Retries can extend total execution time beyond the configured 30-second timeout.
- Usage: Inspect response.usage when available. Track input and output tokens alongside the current model price to understand actual request costs.
For a useful comparison, run a small set of representative prompts through both models and record answer quality, latency, and cost. Keep prompts concise and request an appropriate response length.
When you are ready to try this setup, sign up for 59API and start with a small test budget. Its pay-as-you-go pricing supports gradual experimentation, and a referral rebate is available under the program’s current terms.