Source: https://ailoot.ai/docs/zh-hant-tw/integrations/openai-node-js-sdk/
Canonical: https://ailoot.ai/docs/zh-hant-tw/integrations/openai-node-js-sdk/
Language: zh-hant-tw

[SDK與框架](https://ailoot.ai/docs/zh-hant-tw/integrations/#sdk) 

# OpenAI SDK (Node.js)

[Markdown](https://ailoot.ai/docs/zh-hant-tw/integrations/openai-node-js-sdk/index.md)

SDK · OpenAI Chat, OpenAI Responses

| 格式 | 設定位置 |
| --- | --- |
| OpenAI Chat, OpenAI Responses | 建構函式選項 new OpenAI({ baseURL, apiKey })；SDK 預設讀取 OPENAI\_BASE\_URL 環境變數 |

## [](https://ailoot.ai/docs/zh-hant-tw/integrations/openai-node-js-sdk/#setup)設定

複製設定，並將`sk-...`替換為您的 Ailoot 控制台金鑰。若應用程式支援，最好將金鑰存放在環境變數`AILOOT_API_KEY`中。

執行前，請使用 Ailoot 金鑰呼叫 GET /v1/models。選擇支援方法包含 POST /v1/chat/completions, POST /v1/responses 的確切模型 ID，並填入設定。使用代理程式時，也請確認工具支援及客戶端版本所需的功能。

```js
import OpenAI from "openai";
const client = new OpenAI({maxRetries: 0,
  baseURL: "https://api.ailoot.ai/v1",
  apiKey: process.env.AILOOT_API_KEY // sk-...
});
const r = await client.chat.completions.create({
  model: "claude-sonnet-5-5",
  messages: [{ role: "user", content: "你好" }]
});
console.log(r.choices[0].message.content);
```

此設定會從環境變數讀取金鑰：`AILOOT_API_KEY`。請在啟動應用程式前設定：

```bash
export AILOOT_API_KEY="sk-..."   # 加入 ~/.zshrc 或 ~/.bashrc
```

```powershell
setx AILOOT_API_KEY "sk-..."   # 然後重新啟動終端機
```

模型範例：`claude-sonnet-5-5, gpt-6.1-sol, grok-4.7`。若要使用其他模型，請從此處選取：[目錄](https://ailoot.ai/models/zh-hant-tw/) —請確認模型支援所需格式。

## [](https://ailoot.ai/docs/zh-hant-tw/integrations/openai-node-js-sdk/#notes)重要須知

README 顯示 baseURL 選項；用戶端原始碼 (src/client.ts) 可看到它會讀取 OPENAI\_BASE\_URL，預設值為 https://api.openai.com/v1。Ailoot 位址需包含 /v1，可使用 chat.completions與responses。

## [](https://ailoot.ai/docs/zh-hant-tw/integrations/openai-node-js-sdk/#check)驗證

- OpenAI 格式的位址為`https://api.ailoot.ai/v1` (若應用程式要求完整位址，則使用完整路徑)。
- 提出一個簡短問題。若收到錯誤，請查看代碼與Request ID，並對照此頁面：[錯誤與限制](https://ailoot.ai/docs/zh-hant-tw/errors/)。
- 剛完成設定就遇到 404，幾乎總是位址不正確，可能多加或漏加了`/v1`。
- 402 表示餘額不足以涵蓋保留金額。代理需要保留的金額比聊天更多：[原因](https://ailoot.ai/docs/zh-hant-tw/billing/reserve/)。

應用程式說明文件：[https://github.com/openai/openai-node](https://github.com/openai/openai-node)

還有疑問嗎？請提供 Request ID與問題描述，我們會協助釐清。[聯絡支援](mailto:support@ailoot.ai)

## Ailoot · 公司及其專案組合





Ailoot以累積的營運經驗、廣大的客戶群、穩定的服務及持續的KPI管理為基礎。





公司及其專案組合：數百萬客戶，每月銷售額達數十億。





Ailoot將這些營運經驗用於API存取，讓客戶的產品、代理、應用程式及自動化流程使用AI功能。
