For the complete documentation index, see llms.txt. Markdown versions of documentation pages are available by appending .md to the page URL.
主要導覽

管理 API

透過程式管理組織資源與管理工作流程。

管理 API 可讓你自動執行組織管理工作流程,包括邀請使用者、審查稽核記錄、管理專案與 API 金鑰、設定支出上限與警示、管理資料保留,以及操作速率限制。你可以使用這些 API,實作需要在儀表板之外執行的後台自動化、安全性工作流程與營運工具。

如需端點的詳細資訊,請參閱管理 API 參考文件,其中包含管理 API 金鑰邀請使用者專案支出上限稽核記錄

在 SDK 中使用管理 API 金鑰

若要存取這些端點,請建立管理 API 金鑰。管理 API 金鑰無法用於非管理用途的端點。

以下 SDK 版本開始支援管理 API,因此你可能需要更新 SDK 版本:

  • Node:6.36.0
  • Python:2.34.0
  • Go:3.34.0
  • Ruby:0.61.0
  • Java:4.34.0

設定 OPENAI_ADMIN_KEY,然後初始化所用程式語言的 SDK。

使用管理 API 金鑰設定 SDK
import OpenAI from "openai";

const client = new OpenAI({
  adminAPIKey: process.env.OPENAI_ADMIN_KEY,
});

限制專案的模型存取權

使用專案模型權限,為專案設定允許清單或封鎖清單。將 mode 設為 allow_list,即可僅允許使用列出的模型;或將 mode 設為 deny_list,以封鎖列出的模型,同時允許使用其他可用模型。模型 ID 必須對組織可見,包括可見的微調模型快照。

設定專案模型允許清單或封鎖清單
const modelPermissions =
  await client.admin.organization.projects.modelPermissions.update("proj_abc", {
    mode: "allow_list",
    model_ids: ["gpt-4.1", "o3"],
  });

console.log(modelPermissions.mode);

設定組織支出上限

使用支出上限端點,建立或取代組織每月的強制支出上限。請以美分為單位設定 threshold_amount。以下範例將每月上限設為 100 美元:

curl -X POST https://api.openai.com/v1/organization/spend_limit \
  -H "Authorization: Bearer $OPENAI_ADMIN_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "threshold_amount": 10000,
    "currency": "USD",
    "interval": "month"
  }'

當追蹤到的支出達到強制上限時,受影響的 API 請求會傳回 429 錯誤。如需詳細資訊,請參閱支出上限指南

管理支出上限警示

使用專案支出警示,在專案支出達到門檻時通知團隊。門檻金額以美分為單位指定。

建立專案支出上限警示
const spendAlert = await client.admin.organization.projects.spendAlerts.create(
  "proj_abc",
  {
    currency: "USD",
    interval: "month",
    notification_channel: {
      recipients: ["billing@example.com"],
      type: "email",
      subject_prefix: "[OpenAI spend]",
    },
    threshold_amount: 50000,
  }
);

console.log(spendAlert.id);

管理資料保留

使用專案資料保留控制項,為專案覆寫或繼承組織的保留政策。將 retention_type 設為 organization_default,即可繼承組織設定。

設定專案資料保留
const dataRetention =
  await client.admin.organization.projects.dataRetention.update("proj_abc", {
    retention_type: "organization_default",
  });

console.log(dataRetention.type);

透過電子郵件邀請使用者

使用邀請端點,向電子郵件地址傳送加入組織的邀請。

透過電子郵件邀請使用者
const invite = await client.admin.organization.invites.create({
  email: "user@example.com",
  role: "reader",
});

console.log(invite.id);

擷取稽核記錄

使用稽核記錄端點,列出組織近期的使用者動作與組態變更。

擷取稽核記錄
const auditLogs = await client.admin.organization.auditLogs.list({
  limit: 10,
});

console.log(auditLogs.data);