Skip to content
English

OpenAI SDK Integration (Python / Node)

Call 1折AI's GPT and Claude models using the official OpenAI SDK: just change base_url and api_key, with streaming examples and environment variable usage included.

If you already have code written with the OpenAI SDK, switching to 1折AI only requires changing two parameters: base_url and api_key. Fill in the model name with a currently available GPT or Claude model ID.

Python
pip install openai
Python
import os
from openai import OpenAI
client = OpenAI(
base_url="https://1zheai.com/v1",
api_key=os.environ["ONEZHE_API_KEY"],
)
resp = client.chat.completions.create(
model="gpt-5.6-sol",
messages=[{"role": "user", "content": "用一句话解释什么是 prompt caching"}],
)
print(resp.model)
print(resp.choices[0].message.content)
print(resp.usage)

base_url must include /v1.

Python
stream = client.chat.completions.create(
model="gpt-5.6-sol",
messages=[{"role": "user", "content": "写一首四行的诗"}],
stream=True,
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)

Using Environment Variables Without Changing Code

Section titled “Using Environment Variables Without Changing Code”

The official OpenAI SDK (Python and Node) reads OPENAI_BASE_URL and OPENAI_API_KEY. When no parameters are passed in the code, setting these two variables is enough to switch over:

bash
export OPENAI_BASE_URL="https://1zheai.com/v1"
export OPENAI_API_KEY="sk-你的Key"

Note that this affects all programs using the OpenAI SDK in the same environment.

The OpenAI-compatible interface is suitable for “existing code, just want to change models.” If you need Prompt Caching’s cache_control, extended thinking, or Claude’s tool-calling format, using the Anthropic SDK to go through the native interface is more appropriate.

404?

base_url is missing /v1. The OpenAI SDK will only append /chat/completions at the end.

Can I call GPT models?

Yes. OpenAI / GPT models are already live. Please use the model ID from supported models that your key can call.

When you need the Responses protocol, see the Responses API. If you are using Python or Node.js for the first time, you can directly run the complete example; before going live, configure timeouts and limited retries.