---
title: 创建 API 密钥
url: https://runapi.ai/zh-CN/docs/api/management/keys/create.md
canonical: https://runapi.ai/zh-CN/docs/api/management/keys/create
locale: zh-CN
---

# 创建 API 密钥

创建 Standard API 密钥。请保存此操作返回的明文密钥，因为它只会显示一次。

## 端点

POST /api/v1/keys

基础 URL: https://runapi.ai

API 版本: v1

身份验证: Authorization: Bearer YOUR_MANAGEMENT_API_KEY


授权: 需要账户管理员的 Management API 密钥。新建的 Standard API 密钥归调用者所有。

## 请求

只能在列出的请求位置发送值。

### JSON 请求体

```json
{
  "allowed_models": {
    "description": "Optional allowlist of model identifiers.",
    "required": false,
    "type": "array | null"
  },
  "credit_limit_cents": {
    "description": "Optional spending cap in USD cents.",
    "required": false,
    "type": "integer | null"
  },
  "credit_limit_reset_interval": {
    "description": "Optional reset cadence for the key spending cap.",
    "enum": [
      "daily",
      "weekly",
      "monthly"
    ],
    "required": false,
    "type": "string | null"
  },
  "enabled": {
    "default": true,
    "description": "Whether requests using this key are accepted.",
    "required": false,
    "type": "boolean"
  },
  "guardrail_id": {
    "description": "Optional guardrail prefix ID to attach at creation.",
    "required": false,
    "type": "string"
  },
  "name": {
    "description": "Friendly name for the API key.",
    "required": false,
    "type": "string"
  }
}
```

## 成功响应

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",
  "enabled": true,
  "guardrail_id": "guardrail_123",
  "id": "token_123",
  "key": "runapi_plaintext_example",
  "last_used_at": null,
  "masked_token": "runapi_abc...wxyz",
  "name": "Production"
}
```

## 错误响应 (401)

HTTP 401

```json
{
  "error": "Authentication required"
}
```

## 错误响应 (403)

HTTP 403

```json
{
  "error": "Management API key required"
}
```

## 错误响应 (403)

HTTP 403

```json
{
  "error": "Account admin required"
}
```

## 错误响应 (400)

HTTP 400

```json
{
  "error": "page must be an integer"
}
```

## 错误响应 (422)

HTTP 422

```json
{
  "errors": {
    "base": [
      "Validation failed"
    ]
  }
}
```

## cURL 示例



### 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","allowed_models":["gpt-5.4"],"guardrail_id":"guardrail_123"}'
```

## 相关参考

- [API 密钥](/docs/api/management/keys)
- [API 密钥对象](/docs/api/management/keys/object)
