Skip to content

API 运行接口

已发布应用的外部 API 调用入口。支持通过 Deployment ID、Document ID 或 Release 别名三种方式调用。

源码: apps/backend/src/routes/api-run.ts

POST /api/released-app/:deploymentId/run

通过 Deployment ID 运行已发布的应用。

认证方式

API Key / Schedule Token(apiKeyAuth

请求参数

Path 参数

参数类型必填说明
deploymentIdstring部署 UUID

Body 参数

json
{
  "inputs": { "text": "hello world" },
  "version": "^1.0",
  "stream": true,
  "format": "ndjson",
  "timeout": 60
}
参数类型默认值说明
inputsobject{}Input Block 的输入值
versionstring-版本号。"1.0":精确匹配;"^1.0":major=1 的最新 minor;省略:最新版本
streambooleantrue是否流式响应
format"sse" | "ndjson""ndjson"流式响应格式(注意:API 调用默认 NDJSON,与编辑器默认 SSE 不同)
timeoutnumber-超时时间(秒),不超过管理后台配置的最大值

允许空 body(使用默认值)。

响应格式

NDJSON 流式响应(默认)

Content-Type: application/x-ndjson

API 模式下仅输出数据类 Block 的事件,过滤掉 prompt 等内部 Block 信息:

{"type":"run_start","runId":"uuid","documentId":"uuid"}
{"type":"block_start","blockId":"uuid","blockType":"code"}
{"type":"block_chunk","blockId":"uuid","delta":"部分输出"}
{"type":"block_complete","blockId":"uuid","output":"完整输出"}
{"type":"block_end","blockId":"uuid"}
{"type":"run_complete","runId":"uuid","globalCtx":[...],"sessions":[...],"duration":1234}

SSE 流式响应(format: "sse"

Content-Type: text/event-stream

事件类型与 NDJSON 一致,但使用 SSE 格式:

event: run_start
data: {"runId":"uuid","documentId":"uuid"}

event: block_complete
data: {"blockId":"uuid","output":"完整输出"}

event: run_complete
data: {"runId":"uuid","globalCtx":[...],"sessions":[...],"duration":1234}

JSON 一次性响应(stream: false

状态码: 200

json
{
  "runId": "uuid",
  "documentId": "uuid",
  "globalCtx": [
    {
      "blockId": "uuid",
      "blockType": "code",
      "blockName": "处理数据",
      "output": "处理结果"
    }
  ],
  "sessions": [
    {
      "blockId": "uuid",
      "blockType": "code",
      "blockName": "处理数据",
      "status": "completed",
      "output": "处理结果"
    }
  ],
  "duration": 1234,
  "status": "success",
  "timedOut": false
}

权限规则

部署可见性API Key 认证Schedule 认证
public任意有效 API Key已通过 Token 验证
privateAPI Key 所属团队必须与部署所属团队一致已通过 Token 验证(跳过权限检查)

请求示例

bash
# JSON 一次性响应
curl -X POST \
  -H "Authorization: Bearer wn-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{"inputs": {"text": "hello"}, "stream": false}' \
  https://block2-api.wainao.chat/api/released-app/DEPLOYMENT_UUID/run

# NDJSON 流式响应
curl -X POST \
  -H "Authorization: Bearer wn-your-api-key" \
  -d '{"inputs": {"text": "hello"}}' \
  https://block2-api.wainao.chat/api/released-app/DEPLOYMENT_UUID/run

# 指定版本
curl -X POST \
  -H "Authorization: Bearer wn-your-api-key" \
  -d '{"inputs": {"text": "hello"}, "version": "^1.0"}' \
  https://block2-api.wainao.chat/api/released-app/DEPLOYMENT_UUID/run

错误码

状态码错误说明
400Input validation failed输入参数验证失败
401Unauthorized认证失败或认证方式不支持
402insufficient_credits积分余额不足
403Permission denied: This is a private app...无权访问私有应用
403Deployment ID mismatchSchedule Token 中的 deploymentId 不匹配
404Deployment not found部署不存在或版本未找到
500Invalid deployment snapshot: missing content部署快照数据损坏

POST /api/released-app/d/:documentId/run

通过 Document ID 运行已发布的应用。Document ID 是稳定的标识符,不会因重新发布而改变。

认证方式

API Key(仅 API Key,apiKeyAuth

请求参数

Path 参数

参数类型必填说明
documentIdstring文档 UUID(稳定标识)

Body 参数

/:deploymentId/run 一致。

响应格式

/:deploymentId/run 一致。

请求示例

bash
curl -X POST \
  -H "Authorization: Bearer wn-your-api-key" \
  -d '{"inputs": {"text": "hello"}, "stream": false}' \
  https://block2-api.wainao.chat/api/released-app/d/DOCUMENT_UUID/run

错误码

/:deploymentId/run 一致,额外包含:

状态码错误说明
401Unauthorized: Document ID route only supports API key authentication仅支持 API Key 认证

ALL /api/released-app/r/:alias/:version/*

Release 网关路由。通过应用别名 + 版本号 + 路径匹配已发布的端点并执行。

认证方式

API Key(仅 API Key,apiKeyAuth

请求参数

Path 参数

参数类型必填说明
aliasstringRelease 别名
versionstring版本号字符串
*string路由路径(匹配 routerTree 中的端点)

Body / Query 参数

  • 非 GET 请求: 从请求体解析 inputs
  • GET 请求: 从 query 参数构建 inputs

匹配规则

  1. 通过 aliasrelease_aliases 表中查找 release_document_id
  2. 根据版本号解析匹配的已发布版本
  3. 在版本的 routerTree 中按 path + HTTP method 匹配端点
  4. 找到端点关联的 deploymentId 并执行

响应格式

固定使用 JSON 一次性响应(runWithJSON),格式与上述 JSON 响应一致。

请求示例

bash
# GET 请求(参数通过 query 传递)
curl -H "Authorization: Bearer wn-your-api-key" \
  "https://block2-api.wainao.chat/api/released-app/r/my-app/v1/users?name=test"

# POST 请求(参数通过 body 传递)
curl -X POST \
  -H "Authorization: Bearer wn-your-api-key" \
  -d '{"name": "test"}' \
  "https://block2-api.wainao.chat/api/released-app/r/my-app/v1/users"

错误码

状态码错误说明
401Unauthorized认证失败或非 API Key 认证
403Permission denied无权访问私有应用
404Release alias 'xxx' not found别名不存在
404No published version matching 'xxx' found版本不存在
404No endpoint matching METHOD /path端点未匹配
400Endpoint 'xxx' has no deployment configured端点未配置部署
404Deployment not found关联的部署不存在

AI Workflow Editor