---
title: "OpenAI SDK Integration (Python / Node)"
description: "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."
headingIds: {"Installation":"section-1","Calling":"section-2","Streaming Output":"section-3","Using Environment Variables Without Changing Code":"section-4","When to Switch to the Anthropic SDK":"section-5","FAQ":"section-6","Responses and Complete Programs":"section-7"}
---

<aside className="docs-publication" data-pagefind-ignore>

Documentation maintained by [1ZheAI](https://1zheai.com/en/editorial/#publisher). [Sources and corrections](https://1zheai.com/en/editorial/).

Found an error? [Contact support](mailto:support@1zheai.com) with the page address and a description. Do not send API keys or account credentials.

</aside>
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

<CodeGroup>
```bash title="Python"
pip install openai
```
```bash title="Node.js"
npm install openai
```
</CodeGroup>

## Calling

<CodeGroup>
```python title="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)
```
```javascript title="Node.js"
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);
```
</CodeGroup>

`base_url` must include `/v1`.

## Streaming Output

<CodeGroup>
```python title="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)
```
```javascript title="Node.js"
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 ?? "");
}
```
</CodeGroup>

## 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.

## 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](/en/docs/quickstart-anthropic-sdk/) to go through the native interface is more appropriate.

## FAQ

<AccordionGroup>
<Accordion title="404？">

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

</Accordion>
<Accordion title="Can I call GPT models?">

Yes. OpenAI / GPT models are already live. Please use the model ID from [supported models](/en/docs/models/) that your key can call.

</Accordion>
</AccordionGroup>

## Responses and Complete Programs

When you need the Responses protocol, see the [Responses API](/en/docs/api-responses/). If you are using Python or Node.js for the first time, you can directly run the [complete example](/en/docs/code-examples/); before going live, configure [timeouts and limited retries](/en/docs/retries/).
