> ## Documentation Index
> Fetch the complete documentation index at: https://docs.molesignal.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 新建路由规则

> 新建一条告警路由规则，排在最后。

```http theme={null}
POST /api/v1/alerts/routing/rules
```

新建一条[路由规则](https://docs.molesignal.com/zh-Hans/alerting/routing.md#路由规则)，需要 `alerts.routing.manage`。新规则排在最后，用[调整顺序](https://docs.molesignal.com/zh-Hans/api/alerting/reorder-routing-rules.md)移到前面。一个组织最多 200 条路由规则。

| 字段 | 类型 | 说明 |
| - | - | - |
| `name` | string | 必填，去掉首尾空白后 1 到 128 个字符。 |
| `enabled` | boolean | 默认 `true`。 |
| `conditions` | array | 最多 16 个条件，默认为空。`key` 为 1 到 512 个字符；`equals`、`not_equals` 和 `matches` 各带一个值，`in` 带 1 到 100 个值，每个值最多 512 个字符；`matches` 的正则要能编译。 |
| `min_severity` | string \| null | `info`、`warning`、`error` 或 `critical`，默认不限。 |
| `origins` | array | `rule`、`external`、`synthetic` 中的若干个，默认为空，即不限。 |
| `target` | object | 必填。`{"kind": "team", "team_id": …}` 须是本组织的团队，`{"kind": "policy", "policy_id": …}` 须是本组织的升级策略。 |

```bash cURL theme={null}
curl -X POST http://localhost:5080/api/v1/alerts/routing/rules \
  -H "authorization: Bearer $MS_TOKEN" \
  -H "content-type: application/json" \
  -d '{"name": "Payments in production", "conditions": [{"key": "env", "operator": "equals", "values": ["prod"]}], "min_severity": "error", "target": {"kind": "team", "team_id": "<team-id>"}}'
```

响应是新建的规则，字段见[规则字段](https://docs.molesignal.com/zh-Hans/api/alerting/list-routing-rules.md#规则字段)。

**错误**

| 状态码 | 原因 |
| - | - |
| `400` | 字段不对，例如名称为空、条件的取值个数不对、正则无法编译、去向的团队或策略不存在，或组织已有 200 条规则。 |
| `403` | Token 缺少 `alerts.routing.manage`。 |
| `422` | 缺少 `name` 或 `target`，或取值无法识别，例如未知的 `operator`。响应为纯文本。 |


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.