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.
Installation
Section titled “Installation”Calling
Section titled “Calling”import osfrom 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)import OpenAI from "openai";
const client = new OpenAI({ baseURL: "https://1zheai.com/v1", apiKey: process.env.ONEZHE_API_KEY,});
const resp = await client.chat.completions.create({ model: "gpt-5.6-sol", messages: [{ role: "user", content: "用一句话解释什么是 prompt caching" }],});console.log(resp.model);console.log(resp.choices[0].message.content);console.log(resp.usage);base_url must include /v1.
Streaming Output
Section titled “Streaming Output”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)const stream = await client.chat.completions.create({ model: "gpt-5.6-sol", messages: [{ role: "user", content: "写一首四行的诗" }], stream: true,});for await (const chunk of stream) { process.stdout.write(chunk.choices[0]?.delta?.content ?? "");}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:
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.
When to Switch to the Anthropic SDK
Section titled “When to Switch to the Anthropic SDK”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.
Responses and Complete Programs
Section titled “Responses and Complete Programs”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.
