Skip to content

API 参考: 工作流 (Workflows)

工作流管理端点。创建、执行、监控自动化工作流。

创建工作流

POST /v1/workflows

权限要求: write

请求体

字段类型必填说明
namestring工作流名称
descriptionstring描述
triggerobject触发配置,默认 { "type": "manual" }
trigger.typestring--manual/cron/event
trigger.schedulestring--cron 表达式(type=cron 时)
trigger.eventstring--事件类型(type=event 时)
nodesarray节点数组
nodes[].idstring--节点唯一 ID
nodes[].typestring--节点类型:ai-generate/tool-call/http-request/condition/email-send
nodes[].configobject--节点配置(因类型而异)
edgesarray边数组,定义执行顺序
edges[].fromstring--起始节点 ID
edges[].tostring--目标节点 ID
edges[].conditionstring--条件值(用于 condition 节点出边):"true"/"false"

请求示例

bash
curl -X POST https://api.invoratec.cn/v1/workflows \
  -H "X-API-Key: claw_xxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "每日设备巡检",
    "description": "自动检查设备状态并发送报告",
    "trigger": { "type": "cron", "schedule": "0 9 * * *" },
    "nodes": [
      {
        "id": "fetch",
        "type": "tool-call",
        "config": { "tool": "iot_get_all_devices", "input": {} }
      },
      {
        "id": "analyze",
        "type": "ai-generate",
        "config": { "prompt": "分析设备数据:{{fetch.result}}" }
      },
      {
        "id": "notify",
        "type": "email-send",
        "config": { "to": "ops@example.com", "subject": "巡检报告", "body": "{{analyze.result}}" }
      }
    ],
    "edges": [
      { "from": "fetch", "to": "analyze" },
      { "from": "analyze", "to": "notify" }
    ]
  }'

响应 (201 Created)

json
{
  "id": "665f1a2b3c4d5e6f7a8b9c0d",
  "userId": "user_123",
  "name": "每日设备巡检",
  "description": "自动检查设备状态并发送报告",
  "trigger": { "type": "cron", "schedule": "0 9 * * *" },
  "nodes": [...],
  "edges": [...],
  "enabled": true,
  "source": "api",
  "createdAt": "2026-04-12T08:00:00.000Z",
  "updatedAt": "2026-04-12T08:00:00.000Z"
}

错误

状态码说明
400name 缺失
403缺少 write 权限

列出工作流

返回工作流列表,不包含 nodesedges 字段(减少传输量)。

GET /v1/workflows

请求示例

bash
curl https://api.invoratec.cn/v1/workflows \
  -H "X-API-Key: claw_xxxx"

响应

json
{
  "workflows": [
    {
      "_id": "665f...",
      "name": "每日设备巡检",
      "description": "自动检查设备状态",
      "trigger": { "type": "cron", "schedule": "0 9 * * *" },
      "enabled": true,
      "source": "api",
      "updatedAt": "2026-04-12T08:00:00.000Z"
    }
  ],
  "total": 1
}

获取工作流详情

返回完整的工作流定义,包含 nodesedges

GET /v1/workflows/{id}

路径参数

参数类型说明
idstring工作流 _id

请求示例

bash
curl https://api.invoratec.cn/v1/workflows/665f... \
  -H "X-API-Key: claw_xxxx"

错误

状态码说明
404工作流不存在

更新工作流

PUT /v1/workflows/{id}

权限要求: write

路径参数

参数类型说明
idstring工作流 _id

请求体

可更新字段:name, description, trigger, nodes, edges, enabled

请求示例

bash
curl -X PUT https://api.invoratec.cn/v1/workflows/665f... \
  -H "X-API-Key: claw_xxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "enabled": false,
    "description": "暂停巡检"
  }'

响应

返回更新后的完整工作流文档。

错误

状态码说明
403缺少 write 权限
404工作流不存在

删除工作流

DELETE /v1/workflows/{id}

权限要求: write

请求示例

bash
curl -X DELETE https://api.invoratec.cn/v1/workflows/665f... \
  -H "X-API-Key: claw_xxxx"

响应

json
{ "success": true }

执行工作流

手动触发工作流执行。工作流加入执行队列,异步处理。

POST /v1/workflows/{id}/run

权限要求: write

路径参数

参数类型说明
idstring工作流 _id

请求体

字段类型必填说明
dataobject触发数据,可在节点中通过 {{trigger.data}} 引用

请求示例

bash
curl -X POST https://api.invoratec.cn/v1/workflows/665f.../run \
  -H "X-API-Key: claw_xxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "data": { "building": "A栋" }
  }'

响应

json
{
  "runId": "665f1a2b3c4d5e6f7a8b9c0e",
  "status": "pending",
  "message": "工作流已加入执行队列"
}

错误

状态码说明
403缺少 write 权限
404工作流不存在

查看执行历史

GET /v1/workflows/{id}/runs

路径参数

参数类型说明
idstring工作流 _id

查询参数

参数类型说明
limitinteger返回上限,默认 20

请求示例

bash
curl "https://api.invoratec.cn/v1/workflows/665f.../runs?limit=10" \
  -H "X-API-Key: claw_xxxx"

响应

json
{
  "runs": [
    {
      "_id": "665f...",
      "workflowId": "665f...",
      "status": "completed",
      "triggeredBy": "api",
      "triggerData": { "building": "A栋" },
      "stepResults": [...],
      "createdAt": "2026-04-12T09:00:00.000Z"
    }
  ]
}

执行状态

status说明
pending等待执行
running执行中
completed执行成功
failed执行失败

查看执行详情

GET /v1/workflows/{id}/runs/{runId}

路径参数

参数类型说明
idstring工作流 _id
runIdstring执行记录 _id

请求示例

bash
curl https://api.invoratec.cn/v1/workflows/665f.../runs/665f... \
  -H "X-API-Key: claw_xxxx"

响应

json
{
  "_id": "665f...",
  "workflowId": "665f...",
  "status": "completed",
  "triggeredBy": "api",
  "triggerData": { "building": "A栋" },
  "stepResults": [
    {
      "nodeId": "fetch",
      "status": "success",
      "result": { "devices": [...] },
      "startedAt": "2026-04-12T09:00:01.000Z",
      "completedAt": "2026-04-12T09:00:02.000Z"
    },
    {
      "nodeId": "analyze",
      "status": "success",
      "result": "所有设备运行正常...",
      "startedAt": "2026-04-12T09:00:02.000Z",
      "completedAt": "2026-04-12T09:00:05.000Z"
    }
  ],
  "createdAt": "2026-04-12T09:00:00.000Z"
}

错误

状态码说明
404执行记录不存在

智航云 AI 中台 — 开发者平台