# Data Factory API 前端开发说明文档 ## 概述 Data Factory API 提供了与 n8n 工作流引擎的集成接口,支持工作流的查询、状态监控、执行记录查看和工作流触发等功能。 **版本**: v1.0 **基础URL**: `/api/datafactory` **内容类型**: `application/json` **字符编码**: UTF-8 ## 通用响应格式 所有API接口都遵循统一的响应格式: ```json { "code": 200, // HTTP状态码 "message": "操作成功", // 操作结果消息 "data": {} // 返回的具体数据 } ``` ## 状态码说明 | 状态码 | 含义 | 说明 | |--------|------|------| | 200 | 成功 | 请求成功执行 | | 400 | 请求错误 | 请求参数错误或缺失 | | 401 | 认证失败 | n8n API Key 无效 | | 403 | 权限不足 | 无权访问该资源 | | 404 | 未找到 | 请求的资源不存在 | | 500 | 服务器错误 | 服务器内部错误 | | 503 | 服务不可用 | n8n 服务连接失败 | --- ## API 接口详情 ### 1. 获取工作流列表 **接口描述**: 获取 n8n 系统中的工作流列表,支持分页、搜索和状态过滤。 #### 请求信息 - **HTTP方法**: `GET` - **请求路径**: `/workflows` - **请求头**: `Content-Type: application/json` #### 请求参数 | 参数名 | 类型 | 必填 | 默认值 | 说明 | |--------|------|------|--------|------| | page | int | 否 | 1 | 页码,从1开始 | | page_size | int | 否 | 20 | 每页数量 | | active | string | 否 | - | 过滤活跃状态:`true`/`false` | | search | string | 否 | - | 按名称搜索关键词 | | tags | string | 否 | - | 按标签过滤,多个标签用逗号分隔 | #### 响应数据 ```json { "code": 200, "message": "获取工作流列表成功", "data": { "items": [ { "id": "1PCBqwesRXiFcfJ1", "name": "Simple RAG", "active": false, "tags": ["AI", "RAG"], "created_at": "2025-12-01 10:00:00", "updated_at": "2025-12-03 03:14:49" }, { "id": "9w5VhCRlRrjFqDpX", "name": "My workflow 2", "active": true, "tags": [], "created_at": "2025-10-28 11:00:00", "updated_at": "2025-10-28 12:06:12" } ], "total": 3, "page": 1, "page_size": 20, "total_pages": 1 } } ``` #### 响应字段说明 | 字段 | 类型 | 说明 | |------|------|------| | items | array | 工作流列表 | | items[].id | string | 工作流唯一标识 | | items[].name | string | 工作流名称 | | items[].active | boolean | 是否激活状态 | | items[].tags | array | 标签列表 | | items[].created_at | string | 创建时间 | | items[].updated_at | string | 更新时间 | | total | int | 总记录数 | | page | int | 当前页码 | | page_size | int | 每页数量 | | total_pages | int | 总页数 | #### Vue 代码示例 ```vue ``` --- ### 2. 获取工作流详情 **接口描述**: 根据工作流ID获取详细信息,包括节点列表和配置。 #### 请求信息 - **HTTP方法**: `GET` - **请求路径**: `/workflows/{workflow_id}` - **请求头**: `Content-Type: application/json` #### 路径参数 | 参数名 | 类型 | 必填 | 说明 | |--------|------|------|------| | workflow_id | string | 是 | 工作流ID | #### 响应数据 ```json { "code": 200, "message": "获取工作流详情成功", "data": { "id": "1PCBqwesRXiFcfJ1", "name": "Simple RAG", "active": false, "tags": ["AI", "RAG"], "created_at": "2025-12-01 10:00:00", "updated_at": "2025-12-03 03:14:49", "nodes_count": 5, "nodes": [ { "id": "node-1", "name": "Start", "type": "n8n-nodes-base.manualTrigger", "type_version": 1, "position": [250, 300], "disabled": false }, { "id": "node-2", "name": "HTTP Request", "type": "n8n-nodes-base.httpRequest", "type_version": 4, "position": [450, 300], "disabled": false } ], "settings": { "executionOrder": "v1" } } } ``` #### 响应字段说明 | 字段 | 类型 | 说明 | |------|------|------| | id | string | 工作流ID | | name | string | 工作流名称 | | active | boolean | 是否激活 | | tags | array | 标签列表 | | created_at | string | 创建时间 | | updated_at | string | 更新时间 | | nodes_count | int | 节点数量 | | nodes | array | 节点列表 | | nodes[].id | string | 节点ID | | nodes[].name | string | 节点名称 | | nodes[].type | string | 节点类型 | | nodes[].type_version | int | 节点版本 | | nodes[].position | array | 节点位置 [x, y] | | nodes[].disabled | boolean | 是否禁用 | | settings | object | 工作流设置 | #### Vue 代码示例 ```vue ``` --- ### 3. 获取工作流状态 **接口描述**: 获取工作流的运行状态和最近执行情况统计。 #### 请求信息 - **HTTP方法**: `GET` - **请求路径**: `/workflows/{workflow_id}/status` - **请求头**: `Content-Type: application/json` #### 路径参数 | 参数名 | 类型 | 必填 | 说明 | |--------|------|------|------| | workflow_id | string | 是 | 工作流ID | #### 响应数据 ```json { "code": 200, "message": "获取工作流状态成功", "data": { "workflow_id": "9w5VhCRlRrjFqDpX", "name": "My workflow 2", "active": true, "status": "active", "status_label": "运行中", "recent_executions": { "total": 5, "success": 4, "error": 1 }, "last_execution": { "id": "12345", "workflow_id": "9w5VhCRlRrjFqDpX", "workflow_name": "My workflow 2", "status": "success", "status_label": "成功", "mode": "trigger", "started_at": "2025-12-24 10:30:00", "finished_at": "2025-12-24 10:30:05", "retry_of": null, "retry_success_id": null }, "updated_at": "2025-10-28 12:06:12" } } ``` #### 响应字段说明 | 字段 | 类型 | 说明 | |------|------|------| | workflow_id | string | 工作流ID | | name | string | 工作流名称 | | active | boolean | 是否激活 | | status | string | 状态码:active/inactive | | status_label | string | 状态显示文本 | | recent_executions | object | 最近执行统计 | | recent_executions.total | int | 最近执行总数 | | recent_executions.success | int | 成功次数 | | recent_executions.error | int | 失败次数 | | last_execution | object | 最后一次执行信息(可能为null) | | updated_at | string | 更新时间 | #### Vue 代码示例 ```vue ``` --- ### 4. 激活工作流 **接口描述**: 激活指定的工作流。 #### 请求信息 - **HTTP方法**: `POST` - **请求路径**: `/workflows/{workflow_id}/activate` - **请求头**: `Content-Type: application/json` #### 路径参数 | 参数名 | 类型 | 必填 | 说明 | |--------|------|------|------| | workflow_id | string | 是 | 工作流ID | #### 响应数据 ```json { "code": 200, "message": "工作流激活成功", "data": { "workflow_id": "9w5VhCRlRrjFqDpX", "active": true, "message": "工作流已激活" } } ``` --- ### 5. 停用工作流 **接口描述**: 停用指定的工作流。 #### 请求信息 - **HTTP方法**: `POST` - **请求路径**: `/workflows/{workflow_id}/deactivate` - **请求头**: `Content-Type: application/json` #### 路径参数 | 参数名 | 类型 | 必填 | 说明 | |--------|------|------|------| | workflow_id | string | 是 | 工作流ID | #### 响应数据 ```json { "code": 200, "message": "工作流停用成功", "data": { "workflow_id": "9w5VhCRlRrjFqDpX", "active": false, "message": "工作流已停用" } } ``` --- ### 6. 获取工作流执行记录列表 **接口描述**: 获取指定工作流的执行记录列表。 #### 请求信息 - **HTTP方法**: `GET` - **请求路径**: `/workflows/{workflow_id}/executions` - **请求头**: `Content-Type: application/json` #### 路径参数 | 参数名 | 类型 | 必填 | 说明 | |--------|------|------|------| | workflow_id | string | 是 | 工作流ID | #### 请求参数 | 参数名 | 类型 | 必填 | 默认值 | 说明 | |--------|------|------|--------|------| | page | int | 否 | 1 | 页码 | | page_size | int | 否 | 20 | 每页数量 | | status | string | 否 | - | 状态过滤:`success`/`error`/`waiting` | #### 响应数据 ```json { "code": 200, "message": "获取执行记录列表成功", "data": { "items": [ { "id": "12345", "workflow_id": "9w5VhCRlRrjFqDpX", "workflow_name": "My workflow 2", "status": "success", "status_label": "成功", "mode": "trigger", "started_at": "2025-12-24 10:30:00", "finished_at": "2025-12-24 10:30:05", "retry_of": null, "retry_success_id": null }, { "id": "12344", "workflow_id": "9w5VhCRlRrjFqDpX", "workflow_name": "My workflow 2", "status": "error", "status_label": "失败", "mode": "manual", "started_at": "2025-12-24 09:15:00", "finished_at": "2025-12-24 09:15:03", "retry_of": null, "retry_success_id": null } ], "total": 25, "page": 1, "page_size": 20, "total_pages": 2 } } ``` #### 响应字段说明 | 字段 | 类型 | 说明 | |------|------|------| | items[].id | string | 执行记录ID | | items[].workflow_id | string | 工作流ID | | items[].workflow_name | string | 工作流名称 | | items[].status | string | 状态码:success/error/waiting/running | | items[].status_label | string | 状态显示文本 | | items[].mode | string | 执行模式:trigger/manual/webhook等 | | items[].started_at | string | 开始时间 | | items[].finished_at | string | 结束时间 | | items[].retry_of | string | 重试来源执行ID(可能为null) | | items[].retry_success_id | string | 重试成功执行ID(可能为null) | #### Vue 代码示例 ```vue ``` --- ### 7. 获取所有执行记录列表 **接口描述**: 获取所有工作流的执行记录列表(不限定工作流)。 #### 请求信息 - **HTTP方法**: `GET` - **请求路径**: `/executions` - **请求头**: `Content-Type: application/json` #### 请求参数 | 参数名 | 类型 | 必填 | 默认值 | 说明 | |--------|------|------|--------|------| | page | int | 否 | 1 | 页码 | | page_size | int | 否 | 20 | 每页数量 | | workflow_id | string | 否 | - | 按工作流ID过滤 | | status | string | 否 | - | 状态过滤:`success`/`error`/`waiting` | #### 响应数据 响应格式与 `/workflows/{workflow_id}/executions` 相同。 --- ### 8. 获取执行详情 **接口描述**: 获取单次执行的详细信息,包括各节点的执行结果。 #### 请求信息 - **HTTP方法**: `GET` - **请求路径**: `/executions/{execution_id}` - **请求头**: `Content-Type: application/json` #### 路径参数 | 参数名 | 类型 | 必填 | 说明 | |--------|------|------|------| | execution_id | string | 是 | 执行ID | #### 响应数据 ```json { "code": 200, "message": "获取执行详情成功", "data": { "id": "12345", "workflow_id": "9w5VhCRlRrjFqDpX", "workflow_name": "My workflow 2", "status": "success", "status_label": "成功", "mode": "trigger", "started_at": "2025-12-24 10:30:00", "finished_at": "2025-12-24 10:30:05", "retry_of": null, "retry_success_id": null, "node_results": [ { "node_name": "Start", "start_time": "2025-12-24 10:30:00", "execution_time": 10, "source": [], "data": { "main": [[{"json": {"started": true}}]] } }, { "node_name": "HTTP Request", "start_time": "2025-12-24 10:30:01", "execution_time": 3500, "source": [{"previousNode": "Start"}], "data": { "main": [[{"json": {"response": "OK"}}]] } } ], "error": null } } ``` #### 响应字段说明 | 字段 | 类型 | 说明 | |------|------|------| | node_results | array | 各节点执行结果列表 | | node_results[].node_name | string | 节点名称 | | node_results[].start_time | string | 节点开始执行时间 | | node_results[].execution_time | int | 执行耗时(毫秒) | | node_results[].source | array | 数据来源节点 | | node_results[].data | object | 节点输出数据 | | error | object | 错误信息(成功时为null) | #### Vue 代码示例 ```vue ``` --- ### 9. 触发工作流执行 **接口描述**: 通过 Webhook 触发工作流执行。 #### 请求信息 - **HTTP方法**: `POST` - **请求路径**: `/workflows/{workflow_id}/execute` - **请求头**: `Content-Type: application/json` #### 路径参数 | 参数名 | 类型 | 必填 | 说明 | |--------|------|------|------| | workflow_id | string | 是 | 工作流ID | #### 请求参数 | 参数名 | 类型 | 必填 | 说明 | |--------|------|------|------| | webhook_path | string | 是 | Webhook 路径(工作流中配置的路径) | | data | object | 否 | 传递给工作流的数据 | #### 请求体示例 ```json { "webhook_path": "my-webhook-path", "data": { "user_id": 12345, "action": "process", "params": { "key1": "value1", "key2": "value2" } } } ``` #### 响应数据 **成功响应**: ```json { "code": 200, "message": "工作流触发成功", "data": { "success": true, "message": "工作流已通过 Webhook 触发", "workflow_id": "9w5VhCRlRrjFqDpX", "response": { "result": "OK" } } } ``` **缺少 webhook_path 响应**: ```json { "code": 400, "message": "请提供 Webhook 路径以触发工作流", "data": { "success": false, "message": "请提供 Webhook 路径以触发工作流", "workflow_id": "9w5VhCRlRrjFqDpX" } } ``` #### Vue 代码示例 ```vue ``` --- ### 10. 健康检查 **接口描述**: 检查 n8n 服务的连接状态。 #### 请求信息 - **HTTP方法**: `GET` - **请求路径**: `/health` - **请求头**: `Content-Type: application/json` #### 响应数据 **连接正常**: ```json { "code": 200, "message": "n8n 服务连接正常", "data": { "status": "healthy", "connected": true, "api_url": "https://n8n.citupro.com" } } ``` **连接失败**: ```json { "code": 503, "message": "n8n 服务连接失败: n8n API 认证失败,请检查 API Key 配置", "data": { "status": "unhealthy", "connected": false, "error": "n8n API 认证失败,请检查 API Key 配置", "api_url": "https://n8n.citupro.com" } } ``` #### Vue 代码示例 ```vue ``` --- ## API 服务封装 建议将 API 调用封装成独立的服务模块,便于统一管理和维护。 ### api/datafactory.js ```javascript import axios from 'axios' const BASE_URL = '/api/datafactory' export const datafactoryApi = { // 工作流相关 getWorkflows(params) { return axios.get(`${BASE_URL}/workflows`, { params }) }, getWorkflow(workflowId) { return axios.get(`${BASE_URL}/workflows/${workflowId}`) }, getWorkflowStatus(workflowId) { return axios.get(`${BASE_URL}/workflows/${workflowId}/status`) }, activateWorkflow(workflowId) { return axios.post(`${BASE_URL}/workflows/${workflowId}/activate`) }, deactivateWorkflow(workflowId) { return axios.post(`${BASE_URL}/workflows/${workflowId}/deactivate`) }, // 执行记录相关 getWorkflowExecutions(workflowId, params) { return axios.get(`${BASE_URL}/workflows/${workflowId}/executions`, { params }) }, getAllExecutions(params) { return axios.get(`${BASE_URL}/executions`, { params }) }, getExecution(executionId) { return axios.get(`${BASE_URL}/executions/${executionId}`) }, // 触发执行 triggerWorkflow(workflowId, data) { return axios.post(`${BASE_URL}/workflows/${workflowId}/execute`, data) }, // 健康检查 healthCheck() { return axios.get(`${BASE_URL}/health`) } } ``` ### 使用示例 ```javascript import { datafactoryApi } from '@/api/datafactory' // 获取工作流列表 const response = await datafactoryApi.getWorkflows({ page: 1, page_size: 20, active: 'true' }) // 触发工作流 const result = await datafactoryApi.triggerWorkflow('workflow-id', { webhook_path: 'my-webhook', data: { key: 'value' } }) ``` --- ## 路由配置示例 ### router/datafactory.js ```javascript export default [ { path: '/datafactory', name: 'DataFactory', component: () => import('@/views/datafactory/index.vue'), meta: { title: '数据工厂' }, children: [ { path: '', name: 'WorkflowList', component: () => import('@/views/datafactory/WorkflowList.vue'), meta: { title: '工作流列表' } }, { path: 'workflow/:id', name: 'WorkflowDetail', component: () => import('@/views/datafactory/WorkflowDetail.vue'), meta: { title: '工作流详情' } }, { path: 'workflow/:id/status', name: 'WorkflowStatus', component: () => import('@/views/datafactory/WorkflowStatus.vue'), meta: { title: '工作流状态' } }, { path: 'workflow/:id/executions', name: 'WorkflowExecutions', component: () => import('@/views/datafactory/ExecutionList.vue'), meta: { title: '执行记录' } }, { path: 'workflow/:id/trigger', name: 'TriggerWorkflow', component: () => import('@/views/datafactory/TriggerWorkflow.vue'), meta: { title: '触发执行' } }, { path: 'executions/:id', name: 'ExecutionDetail', component: () => import('@/views/datafactory/ExecutionDetail.vue'), meta: { title: '执行详情' } } ] } ] ``` --- ## 注意事项 1. **认证配置**: 后端已配置 n8n API Key,前端无需额外处理认证 2. **跨域问题**: 后端已配置 CORS,支持跨域请求 3. **错误处理**: 建议在 axios 拦截器中统一处理错误响应 4. **状态刷新**: 工作流状态页面建议设置定时刷新(如30秒) 5. **Webhook路径**: 触发工作流时需要提供正确的 Webhook 路径,该路径在 n8n 工作流的 Webhook 节点中配置 --- ## 更新日志 - **v1.0** (2025-12-24): 初始版本,包含工作流查询、状态监控、执行记录和触发执行功能 --- ## 技术支持 如有问题请联系后端开发团队或查看详细的 API 文档。 **文档版本**: v1.0 **最后更新**: 2025-12-24