---
title: "Ready-to-Run Code Examples"
description: "Complete Chat Completions programs in Python and Node.js, covering environment setup, model selection, error handling, and execution commands."
headingIds: {"1. Prepare Environment Variables":"section-1","2. Choose a Language and Run":"section-2","Python":"section-3","Node.js":"section-4","3. Verify the Results":"section-5","Continue Extending":"section-6"}
---

<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>
The two programs below do the same thing: read environment variables, make a single non-streaming text request, and output the response or an identifiable error. The examples disable automatic retries so you can confirm the result of a single request.

## 1. Prepare Environment Variables

Set them in the same terminal where you run the program; `ONEZHE_MODEL` can be replaced with an ID from [models with available keys ](/en/docs/api-models/).

<CodeGroup>
```bash title="macOS / Linux"
export ONEZHE_API_KEY="sk-你的Key"
export ONEZHE_MODEL="gpt-5.6-sol"
```
```powershell title="Windows PowerShell"
$env:ONEZHE_API_KEY = "sk-你的Key"
$env:ONEZHE_MODEL = "gpt-5.6-sol"
```
</CodeGroup>

## 2. Choose a Language and Run

### Python

Install `python -m pip install openai`, save as `chat.py`, and execute `python chat.py`.

```python
import os
import sys
import openai

api_key = os.environ.get("ONEZHE_API_KEY")
if not api_key:
    sys.exit("请先设置 ONEZHE_API_KEY 环境变量。")

client = openai.OpenAI(
    api_key=api_key,
    base_url="https://1zheai.com/v1",
    timeout=120.0,
    max_retries=0,
)
try:
    response = client.chat.completions.create(
        model=os.environ.get("ONEZHE_MODEL", "gpt-5.6-sol"),
        messages=[{"role": "user", "content": "用一句中文解释 DNS。"}],
    )
    if not response.choices:
        sys.exit("响应没有 choices，请检查响应协议。")
    choice = response.choices[0]
    print(choice.message.content or "本次响应没有普通文本。")
    print("finish_reason:", choice.finish_reason)
    print("response_id:", response.id)
except openai.APIStatusError as exc:
    print("HTTP:", exc.status_code, "request_id:", exc.request_id, file=sys.stderr)
    print("请查看控制台日志和错误码文档；不要公开密钥。", file=sys.stderr)
    sys.exit(1)
except openai.APIConnectionError:
    sys.exit("连接失败或超时：检查网络与代理，再核对调用日志。")
```

### Node.js

Install `npm install openai`, save as `chat.mjs`, and execute `node chat.mjs`. Using the `.mjs` extension directly enables ES Modules.

```javascript
import OpenAI from "openai";

if (!process.env.ONEZHE_API_KEY) {
  console.error("请先设置 ONEZHE_API_KEY 环境变量。");
  process.exit(1);
}
const client = new OpenAI({
  apiKey: process.env.ONEZHE_API_KEY,
  baseURL: "https://1zheai.com/v1",
  timeout: 120_000,
  maxRetries: 0,
});
try {
  const response = await client.chat.completions.create({
    model: process.env.ONEZHE_MODEL || "gpt-5.6-sol",
    messages: [{ role: "user", content: "用一句中文解释 DNS。" }],
  });
  const choice = response.choices[0];
  if (!choice) throw new Error("响应没有 choices，请检查响应协议。");
  console.log(choice.message.content || "本次响应没有普通文本。");
  console.log("finish_reason:", choice.finish_reason);
  console.log("response_id:", response.id);
} catch (error) {
  if (error instanceof OpenAI.APIError) {
    console.error("HTTP:", error.status, "request_id:", error.request_id);
    console.error("请检查网络、控制台日志和错误码文档。");
  } else {
    console.error(error instanceof Error ? error.message : "未知错误");
  }
  process.exitCode = 1;
}
```

## 3. Verify the Results

You should see a response and a finish reason. When `finish_reason` is not `stop`, check whether it is a tool call or a truncation. The SDK's request ID may be empty; the server may also place the trace ID in the error body, so supplement it from the redacted raw response when debugging.

Go to [call logs ](/en/docs/usage-logs/) to verify calls. The program's exit status and whether there is plain text do not cover all business success conditions; check structure and content according to your task.

## Continue Extending

- [Chat Completions ](/en/docs/api-chat-completions/): multi-turn history and field descriptions.
- [Messages ](/en/docs/api-messages/): Claude-native content blocks.
- [Responses ](/en/docs/api-responses/): GPT Responses and streaming events.
- [Image Generation ](/en/docs/api-images/): generate and save image files.
- [Timeouts and Retries ](/en/docs/retries/): configure failure handling before going live.
