跳到主要內容
API 參考
API 參考

建立 API 金鑰

建立標準 API 金鑰。請儲存此操作返回的明文金鑰,因為它只會顯示一次。

建立標準 API 金鑰。請儲存此操作返回的明文金鑰,因為它只會顯示一次。

端點

POST /api/v1/keys
基礎 URL
https://runapi.ai
API 版本
v1
身分驗證
Authorization: Bearer YOUR_MANAGEMENT_API_KEY
授權
需要帳戶管理員等級的 Management API 金鑰。新的標準 API 金鑰歸呼叫者所有。
02

請求

JSON 請求內文

僅在列出的請求位置中傳送值。

JSON 主體7 個欄位
allowed_modelsarray | null
選填

模型識別碼的選用允許清單。

credit_limit_centsinteger | null
選填

選用的消費上限(以美分為單位)。

credit_limit_reset_intervalstring | null
選填

金鑰消費上限的選用重置週期。

允許的值: daily, weekly, monthly
credit_limit_windowsobject
選填

以 1h、1d 或 7d 為索引鍵的滾動消費上限;每個值包含正整數 limit_cents。

enabledboolean
選填

使用此金鑰的請求是否被接受。

預設值: true
guardrail_idstring
選填

建立時附加的選用防護欄前綴 ID。

namestring
選填

API 金鑰的易讀名稱。

03

成功回應

HTTP 201

POST /api/v1/keys

JSON
{
  "allowed_models": [
    "gpt-5.4"
  ],
  "created_at": "2026-07-27T10:00:00.000Z",
  "credit_limit_cents": 1000,
  "credit_limit_reset_interval": "daily",
  "credit_limit_windows": {
    "1d": {
      "limit_cents": 500
    },
    "1h": {
      "limit_cents": 100
    },
    "7d": {
      "limit_cents": 2000
    }
  },
  "enabled": true,
  "guardrail_id": "guardrail_123",
  "id": "token_123",
  "key": "runapi_plaintext_example",
  "last_used_at": null,
  "masked_token": "runapi_abc...wxyz",
  "name": "Production"
}
04

錯誤回應 (401)

HTTP 401
回應綱要
JSON
{
  "properties": {
    "error": {
      "description": "人類可讀的錯誤訊息。",
      "type": "string"
    }
  },
  "required": [
    "error"
  ],
  "type": "object",
  "unevaluatedProperties": false
}

回應範例

JSON
{
  "error": "Authentication required"
}
05

錯誤回應 (403)

HTTP 403
回應綱要
JSON
{
  "properties": {
    "error": {
      "description": "人類可讀的錯誤訊息。",
      "type": "string"
    }
  },
  "required": [
    "error"
  ],
  "type": "object",
  "unevaluatedProperties": false
}

回應範例

JSON
{
  "error": "Management API key required"
}
06

錯誤回應 (403)

HTTP 403
回應綱要
JSON
{
  "properties": {
    "error": {
      "description": "人類可讀的錯誤訊息。",
      "type": "string"
    }
  },
  "required": [
    "error"
  ],
  "type": "object",
  "unevaluatedProperties": false
}

回應範例

JSON
{
  "error": "Account admin required"
}
07

錯誤回應 (400)

HTTP 400
回應綱要
JSON
{
  "oneOf": [
    {
      "properties": {
        "error": {
          "description": "人類可讀的錯誤訊息。",
          "type": "string"
        }
      },
      "required": [
        "error"
      ],
      "type": "object",
      "unevaluatedProperties": false
    },
    {
      "properties": {
        "error": {
          "description": "人類可讀的驗證摘要。",
          "type": "string"
        },
        "errors": {
          "additionalProperties": {
            "items": {
              "type": "string"
            },
            "type": "array"
          },
          "description": "以公開請求欄位為索引鍵的驗證訊息,每個欄位包含一組人類可讀的字串陣列。",
          "type": "object"
        }
      },
      "required": [
        "error",
        "errors"
      ],
      "type": "object",
      "unevaluatedProperties": false
    }
  ]
}

回應範例

JSON
{
  "error": "page must be an integer"
}
08

錯誤回應 (422)

HTTP 422
回應綱要
JSON
{
  "properties": {
    "error": {
      "description": "人類可讀的驗證摘要。",
      "type": "string"
    },
    "errors": {
      "additionalProperties": {
        "items": {
          "type": "string"
        },
        "type": "array"
      },
      "description": "以公開請求欄位為索引鍵的驗證訊息,每個欄位包含一組人類可讀的字串陣列。",
      "type": "object"
    }
  },
  "required": [
    "error",
    "errors"
  ],
  "type": "object",
  "unevaluatedProperties": false
}

回應範例

JSON
{
  "error": "Validation failed",
  "errors": {
    "base": [
      "Validation failed"
    ]
  }
}
09

cURL 範例

CURL
curl -X POST https://runapi.ai/api/v1/keys \
  -H 'Authorization: Bearer YOUR_MANAGEMENT_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{"name":"Production","credit_limit_cents":1000,"credit_limit_reset_interval":"daily","credit_limit_windows":{"1h":{"limit_cents":100},"1d":{"limit_cents":500},"7d":{"limit_cents":2000}},"allowed_models":["gpt-5.4"],"guardrail_id":"guardrail_123"}'