---
title: 创建 Guardrail
url: https://runapi.ai/zh-CN/docs/api/management/guardrails/create.md
canonical: https://runapi.ai/zh-CN/docs/api/management/guardrails/create
locale: zh-CN
---

# 创建 Guardrail

创建可复用的模型、提供方和预算策略。

## 端点

POST /api/v1/guardrails

基础 URL: https://runapi.ai

API 版本: v1

身份验证: Authorization: Bearer YOUR_MANAGEMENT_API_KEY


授权: 需要账户管理员的 Management API 密钥。

## 请求

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

### JSON 请求体

```json
{
  "allowed_models": {
    "description": "Model allowlist.",
    "required": false,
    "type": "array | null"
  },
  "allowed_providers": {
    "description": "Provider-name allowlist.",
    "required": false,
    "type": "array | null"
  },
  "blocked_models": {
    "description": "Model blocklist, which takes precedence over the allowlist.",
    "required": false,
    "type": "array | null"
  },
  "blocked_providers": {
    "description": "Provider-name blocklist.",
    "required": false,
    "type": "array | null"
  },
  "credit_limit_cents": {
    "description": "Total spending cap in USD cents.",
    "required": false,
    "type": "integer | null"
  },
  "credit_limit_reset_interval": {
    "description": "Reset cadence for the total spending cap.",
    "enum": [
      "daily",
      "weekly",
      "monthly"
    ],
    "required": false,
    "type": "string | null"
  },
  "description": {
    "description": "Optional free-form description.",
    "required": false,
    "type": "string"
  },
  "enabled": {
    "default": true,
    "description": "Whether the guardrail is enforced.",
    "required": false,
    "type": "boolean"
  },
  "model_budgets": {
    "description": "Per-model budget rules.",
    "required": false,
    "type": "array | null"
  },
  "name": {
    "description": "Guardrail name unique within the account.",
    "required": true,
    "type": "string"
  }
}
```

## 成功响应

HTTP 201 - POST /api/v1/guardrails

```json
{
  "allowed_models": [
    "gpt-5.4"
  ],
  "allowed_providers": [],
  "assignment_count": 1,
  "blocked_models": [],
  "blocked_providers": [],
  "created_at": "2026-07-27T10:00:00.000Z",
  "credit_limit_cents": 50000,
  "credit_limit_reset_interval": "monthly",
  "description": "Production policy",
  "enabled": true,
  "id": "guardrail_123",
  "model_budgets": [
    {
      "credit_limit_cents": 10000,
      "credit_limit_reset_interval": "monthly",
      "match_mode": "include",
      "models": [
        "gpt-5.4"
      ]
    }
  ],
  "name": "Production budget"
}
```

## 错误响应 (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/guardrails \
  -H 'Authorization: Bearer YOUR_MANAGEMENT_API_KEY' \
  -H 'Content-Type: application/json' \
  -d '{"name":"Production budget","description":"Production policy","enabled":true,"credit_limit_cents":50000,"credit_limit_reset_interval":"monthly","allowed_models":["gpt-5.4"],"model_budgets":[{"match_mode":"include","models":["gpt-5.4"],"credit_limit_cents":10000,"credit_limit_reset_interval":"monthly"}]}'
```

## 相关参考

- [Guardrails](/docs/api/management/guardrails)
- [Guardrail 对象](/docs/api/management/guardrails/object)
