59API

← 返回教程列表

Call Claude and GPT from Python: A 59API Quick Start

入门教程 · EN · 2026-10-29

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:

Next, set environment variables in the same terminal, replacing the placeholder values:

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:

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

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.

价格 · 接入文档

准备好开始了吗?

几分钟接入 Claude 与 GPT,全网超低价,原生不降智。立即注册即可领取 API 密钥。

免费注册