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

# 创建分派策略

> 创建分派策略，定义故障发生时通知谁以及何时通知。

## 限制说明

| 项目   | 说明                             |
| ---- | ------------------------------ |
| 速率限制 | 每个账户 **1,000 次/分钟**；**50 次/秒** |
| 权限要求 | **协作空间管理**（`on-call`）          |


## OpenAPI

````yaml /api-reference/on-call.openapi.zh.json post /channel/escalate/rule/create
openapi: 3.1.0
info:
  title: Flashduty 开放 API
  description: >-
    Flashduty 事件管理平台的公开 HTTP API —— 覆盖故障、通知模板、协作空间、值班排班、监控、RUM、以及平台管理。每次调用都需在
    query 中携带 `app_key`，该 key 在 Flashduty 控制台 账户 → APP Key 中签发。所有响应使用统一结构：成功时为
    `{ request_id, data }`，失败时为 `{ request_id, error }`。
  version: 1.0.0
servers:
  - url: https://api.flashcat.cloud
    description: Flashduty Open API
security:
  - AppKeyAuth: []
tags:
  - name: On-call/故障管理
    description: ''
  - name: On-call/协作空间
    description: ''
  - name: On-call/告警管理
    description: 查询、查看和处理告警，管理卡片视图与告警处理规则。
  - name: On-call/集成中心
    description: ''
  - name: On-call/IM 集成
    description: IM 集成相关查询，例如查看哪些集成开启了作战室。
  - name: On-call/值班排班
    description: ''
  - name: On-call/日历管理
    description: ''
  - name: On-call/通知模板
    description: ''
  - name: On-call/标签增强
    description: 自定义字段、富化规则及数据映射（映射规则、映射数据、映射 API）管理。
  - name: On-call/分析看板
    description: ''
  - name: On-call/状态页
    description: ''
  - name: On-call/变更管理
    description: ''
paths:
  /channel/escalate/rule/create:
    post:
      tags:
        - On-call/协作空间
      summary: 创建分派策略
      description: 创建分派策略，定义故障发生时通知谁以及何时通知。
      operationId: channelEscalateRuleCreate
      requestBody:
        required: true
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/CreateEscalationRuleRequest'
            example:
              channel_id: 3521074710131
              rule_name: On-call escalation
              template_id: 6321aad26c12104586a88916
              description: >-
                Notify primary on-call, then escalate to secondary after 30
                minutes
              layers:
                - target:
                    person_ids:
                      - 3790925372131
                    by:
                      follow_preference: true
                  max_times: 3
                  notify_step: 10
                  escalate_window: 30
                  force_escalate: false
      responses:
        '200':
          description: 成功
          content:
            application/json:
              schema:
                allOf:
                  - $ref: '#/components/schemas/SuccessEnvelope'
                  - type: object
                    properties:
                      data:
                        $ref: '#/components/schemas/RuleCreateResponse'
              example:
                request_id: 01HK8XQE3Z7JM2NTFQ5YJ8P9R4
                data:
                  rule_id: 69db2f72a0fe7db6448b1506
                  rule_name: Test escalation rule
        '400':
          $ref: '#/components/responses/BadRequest'
        '401':
          $ref: '#/components/responses/Unauthorized'
        '429':
          $ref: '#/components/responses/TooManyRequests'
        '500':
          $ref: '#/components/responses/ServerError'
components:
  schemas:
    CreateEscalationRuleRequest:
      type: object
      description: 创建分派策略所需的参数。
      required:
        - channel_id
        - template_id
        - rule_name
        - layers
      properties:
        channel_id:
          type: integer
          format: int64
          description: 所属协作空间 ID。
        priority:
          type:
            - integer
            - 'null'
          minimum: 0
          maximum: 200
          description: 匹配优先级,数值越小越优先。
        aggr_window:
          type: integer
          minimum: 0
          maximum: 3600
          description: 延迟窗口,单位秒,0 表示不延迟。
        template_id:
          type: string
          pattern: ^[0-9a-fA-F]{24}$
          description: 通知模板 ID(MongoDB ObjectID)。
        rule_name:
          type: string
          minLength: 1
          maxLength: 39
          description: 策略名称,1 到 39 个字符。
        description:
          type: string
          maxLength: 500
          description: 策略描述,最多 500 个字符。
        layers:
          type: array
          description: 分派环节列表,至少包含一个环节。
          items:
            type: object
            required:
              - target
            properties:
              max_times:
                type: integer
                minimum: 0
                maximum: 6
                description: 当前环节内的最大重复通知次数。
              notify_step:
                type: number
                format: float
                minimum: 0.5
                maximum: 120
                description: 重复通知间隔,单位分钟。
              target:
                type: object
                description: >-
                  通知目标。`person_ids`、`team_ids`、`schedule_to_role_ids`、`emails`
                  至少设置一项,且 `by` 与 `webhooks` 至少设置一项。
                properties:
                  person_ids:
                    type: array
                    items:
                      type: integer
                      format: int64
                    description: 直接通知的成员 ID 列表。
                  team_ids:
                    type: array
                    items:
                      type: integer
                      format: int64
                    description: 通知的团队 ID 列表。
                  schedule_to_role_ids:
                    type: object
                    additionalProperties:
                      type: array
                      items:
                        type: integer
                        format: int64
                    description: 值班表 ID 到角色 ID 列表的映射,用于按值班角色通知。
                  emails:
                    type: array
                    items:
                      type: string
                      format: email
                    description: 邮箱地址列表(推送场景)。
                  by:
                    type: object
                    description: 按告警等级配置的个人通知渠道。未提供 `webhooks` 时必填。
                    properties:
                      follow_preference:
                        type: boolean
                        description: 为 true 时跟随处理人员的个人偏好,忽略下方列表。
                      critical:
                        type: array
                        items:
                          type: string
                        description: Critical 级别使用的通知渠道(如 `voice`、`sms`、`email`、`feishu`)。
                      warning:
                        type: array
                        items:
                          type: string
                        description: Warning 级别使用的通知渠道。
                      info:
                        type: array
                        items:
                          type: string
                        description: Info 级别使用的通知渠道。
                  webhooks:
                    type: array
                    description: 群聊/Webhook 通知目标。未提供 `by` 时必填。
                    items:
                      type: object
                      required:
                        - type
                        - settings
                      properties:
                        type:
                          type: string
                          description: >-
                            Webhook 类型(如
                            `feishu`、`dingtalk_app`、`wecom_app`、`slack`、`teams`、`custom`)。
                        settings:
                          type: object
                          additionalProperties: true
                          description: 类型相关的配置参数(群聊 ID、URL 等)。
              escalate_window:
                type: integer
                minimum: 0
                maximum: 720
                description: 升级到下一环节前的等待时长,单位分钟。
              force_escalate:
                type: boolean
                description: 为 true 时,即便未认领也强制升级。
        time_filters:
          type: array
          items:
            type: object
            description: >-
              周期性时间窗口。`start`/`end` 使用 24 小时制 `HH:MM` 格式;`repeat` 使用星期索引(0=周日 …
              6=周六)。
            properties:
              start:
                type: string
                description: 窗口起始时间,`HH:MM`。
              end:
                type: string
                description: 窗口结束时间,`HH:MM`。
              repeat:
                type: array
                items:
                  type: integer
                description: 生效的星期列表,留空表示每天。
              cal_id:
                type: string
                description: 可选日历 ID,限制窗口仅在日历匹配的日期生效。
              is_off:
                type: boolean
                description: 为 true 时匹配日历中的休息日。
          description: 可选的周期性生效时间窗口。
        filters:
          type: array
          description: 或-与组合条件树。每个外层元素为一组 AND 条件,组内所有条件需同时满足。
          items:
            type: array
            items:
              type: object
              required:
                - key
                - oper
                - vals
              properties:
                key:
                  type: string
                  description: 字段键(如 `alert_severity`、`labels.service`)。
                oper:
                  type: string
                  enum:
                    - IN
                    - NOTIN
                  description: 过滤运算符。
                vals:
                  type: array
                  items:
                    type: string
                  description: 匹配值列表。
    SuccessEnvelope:
      type: object
      description: >-
        成功响应结构。2xx 响应中 `request_id` 标识本次调用（同时出现在 `Flashcat-Request-Id`
        响应头中），`data` 为接口业务 payload。失败响应使用不同结构，参见 `ErrorResponse`。
      properties:
        request_id:
          type: string
          description: 本次请求的唯一 ID，也会在 Flashcat-Request-Id 响应头中返回。反馈问题时请一并附上。
          example: 01HK8XQE3Z7JM2NTFQ5YJ8P9R4
        data:
          description: 每个接口自己的业务 payload，详见各接口的 200 响应 schema。
      required:
        - request_id
        - data
    RuleCreateResponse:
      type: object
      required:
        - rule_id
        - rule_name
      properties:
        rule_id:
          type: string
          pattern: ^[0-9a-fA-F]{24}$
          description: 新建规则的 ID（MongoDB ObjectID）。
        rule_name:
          type: string
          description: 请求中回显的规则名称。
    ErrorResponse:
      type: object
      description: 错误响应结构。`error` 必填，`data` 不存在。
      properties:
        request_id:
          type: string
          example: 01HK8XQE3Z7JM2NTFQ5YJ8P9R4
        error:
          $ref: '#/components/schemas/DutyError'
      required:
        - request_id
        - error
    DutyError:
      type: object
      description: 响应结构中的错误 payload，仅在非 2xx 响应时出现。
      properties:
        code:
          $ref: '#/components/schemas/ErrorCode'
        message:
          type: string
          description: 用户可读的错误描述，语言会跟随调用方的 Accept-Language。可能包含字段名、ID 等请求上下文。
          example: The specified parameter template_id is not valid.
      required:
        - code
        - message
    ErrorCode:
      type: string
      description: >-
        Flashduty 错误码枚举。每个失败响应的 `error.code` 都是下列稳定值之一，HTTP 状态码仅作参考。


        | 错误码 | HTTP | 含义 |

        |---|---|---|

        | `OK` | 200 | 保留值，正常错误响应不会返回。 |

        | `InvalidParameter` | 400 | 必填参数缺失或未通过校验。 |

        | `BadRequest` | 400 | 通用的 400 错误，通常是请求本身不合法。 |

        | `InvalidContentType` | 400 | 请求头 `Content-Type` 不是 `application/json`。
        |

        | `ResourceNotFound` | 400 | 目标资源不存在。注意 HTTP 状态码是 400 而非 404（历史设计）。 |

        | `NoLicense` | 400 | 功能需要有效授权，但未找到可用的 license。 |

        | `ReferenceExist` | 400 | 该资源仍被其他实体引用，无法删除。 |

        | `Unauthorized` | 401 | `app_key` 缺失、无效或已过期。 |

        | `BalanceNotEnough` | 402 | 账户余额不足，无法执行需要计费的操作。 |

        | `AccessDenied` | 403 | 身份认证通过，但 RBAC 权限不足以执行该操作。 |

        | `RouteNotFound` | 404 | 请求的 URL 路径不是已知路由。 |

        | `MethodNotAllowed` | 405 | 当前路径不接受所使用的 HTTP 方法。 |

        | `UndonedOrderExist` | 409 | 账户存在未完成的订单，请稍后重试。 |

        | `RequestLocked` | 423 | 因连续失败被临时锁定。 |

        | `EntityTooLarge` | 413 | 请求体超过允许的最大长度。 |

        | `RequestTooFrequently` | 429 | 命中限流（全局、账户级或集成级）。 |

        | `RequestVerifyRequired` | 428 | 操作需要二次验证码，但未提供。 |

        | `DangerousOperation` | 428 | 危险操作，需要进行 MFA 验证。 |

        | `InternalError` | 500 | 服务端未预期错误。反馈问题请附上 `request_id`。 |

        | `ServiceUnavailable` | 503 | 后端依赖不可用，请稍后重试。 |
      enum:
        - OK
        - InvalidParameter
        - BadRequest
        - InvalidContentType
        - ResourceNotFound
        - NoLicense
        - ReferenceExist
        - Unauthorized
        - BalanceNotEnough
        - AccessDenied
        - RouteNotFound
        - MethodNotAllowed
        - UndonedOrderExist
        - RequestLocked
        - EntityTooLarge
        - RequestTooFrequently
        - RequestVerifyRequired
        - DangerousOperation
        - InternalError
        - ServiceUnavailable
      x-enumDescriptions:
        OK: 保留值，正常错误响应不会返回。
        InvalidParameter: 必填参数缺失或未通过校验。
        BadRequest: 通用的 400 错误，通常是请求本身不合法。
        InvalidContentType: 请求头 `Content-Type` 不是 `application/json`。
        ResourceNotFound: 目标资源不存在。注意 HTTP 状态码是 400 而非 404（历史设计）。
        NoLicense: 功能需要有效授权，但未找到可用的 license。
        ReferenceExist: 该资源仍被其他实体引用，无法删除。
        Unauthorized: '`app_key` 缺失、无效或已过期。'
        BalanceNotEnough: 账户余额不足，无法执行需要计费的操作。
        AccessDenied: 身份认证通过，但 RBAC 权限不足以执行该操作。
        RouteNotFound: 请求的 URL 路径不是已知路由。
        MethodNotAllowed: 当前路径不接受所使用的 HTTP 方法。
        UndonedOrderExist: 账户存在未完成的订单，请稍后重试。
        RequestLocked: 因连续失败被临时锁定。
        EntityTooLarge: 请求体超过允许的最大长度。
        RequestTooFrequently: 命中限流（全局、账户级或集成级）。
        RequestVerifyRequired: 操作需要二次验证码，但未提供。
        DangerousOperation: 危险操作，需要进行 MFA 验证。
        InternalError: 服务端未预期错误。反馈问题请附上 `request_id`。
        ServiceUnavailable: 后端依赖不可用，请稍后重试。
      example: InvalidParameter
  responses:
    BadRequest:
      description: 请求非法 — 通常是参数缺失或格式不正确。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          examples:
            missingParameter:
              value:
                request_id: 01HK8XQE3Z7JM2NTFQ5YJ8P9R4
                error:
                  code: InvalidParameter
                  message: The specified parameter is not valid.
    Unauthorized:
      description: app_key 缺失或无效。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          examples:
            missingAppKey:
              value:
                request_id: 01HK8XQE3Z7JM2NTFQ5YJ8P9R4
                error:
                  code: Unauthorized
                  message: You are unauthorized.
    TooManyRequests:
      description: 命中限流。可能是全局 API 限流、账户级限流或集成级限流。限流按账户聚合。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          examples:
            rateLimited:
              value:
                request_id: 01HK8XQE3Z7JM2NTFQ5YJ8P9R4
                error:
                  code: RequestTooFrequently
                  message: Request too frequently.
    ServerError:
      description: 服务端未预期错误。反馈问题时请携带 request_id。
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ErrorResponse'
          examples:
            internal:
              value:
                request_id: 01HK8XQE3Z7JM2NTFQ5YJ8P9R4
                error:
                  code: InternalError
                  message: >-
                    We encountered an internal error, and it has been reported.
                    Please try again later.
  securitySchemes:
    AppKeyAuth:
      type: apiKey
      in: query
      name: app_key
      description: >-
        在 Flashduty 控制台 账户 → APP Key 中签发的 app_key。调用任何公开 API
        时都必须携带。它等同于所属账户的身份凭证，请妥善保管。

````