跳转到内容

工作流

client.workflows 同时暴露渠道自动化(v2/backend 路由)和流程引擎(增删改查流程、运行、文件夹、连接、组件、MCP 服务器、表格、邀请)。

首先初始化客户端(参见安装快速入门)。有关将工作流绑定到 AI 代理的端到端演练,请参见完整流程指南 §2


渠道自动化 — client.workflows

const { data: automations } = await client.workflows.listChannelAutomation();
const { data: whatsappFlows } = await client.workflows.listChannelAutomation({ channelType: "whatsapp" });

流程

增删改查

const { data: flows } = await client.workflows.listFlows();
const flow = await client.workflows.getFlow("flow_id");
const newFlow = await client.workflows.createFlow({
displayName: "New Lead Notification",
projectId: "project_id",
});
await client.workflows.deleteFlow("flow_id");

应用操作

await client.workflows.applyFlowOperation("flow_id", {
type: "UPDATE_TRIGGER",
request: {
name: "trigger",
type: "PIECE_TRIGGER",
valid: true,
displayName: "Webhook",
settings: {
pieceName: "@activepieces/piece-webhook",
pieceVersion: "0.1.24",
triggerName: "catch_webhook",
input: {},
propertySettings: {},
},
},
});

触发流程

// 触发并忘记
await client.workflows.triggerFlow("flow_id", {
contactId: "contact_xxx",
event: "lead_qualified",
});
// 等待结果
const result = await client.workflows.triggerFlowSync("flow_id", {
contactId: "contact_xxx",
event: "lead_qualified",
});

流程运行

const { data: runs } = await client.workflows.listRuns({ flowId: "flow_id", limit: 20 });
const run = await client.workflows.getRun("run_id");

文件夹

const { data: folders } = await client.workflows.listFolders();
const folder = await client.workflows.getFolder("folder_id");
const newFolder = await client.workflows.createFolder({ displayName: "CRM Automations", projectId: "project_id" });
const updated = await client.workflows.updateFolder("folder_id", { displayName: "Updated Name" });
await client.workflows.deleteFolder("folder_id");

应用连接

const { data: connections } = await client.workflows.listConnections();
const connection = await client.workflows.getConnection("connection_id");
await client.workflows.upsertConnection({
name: "slack-integration",
type: "OAUTH2",
value: { access_token: "xoxb-xxx" },
});
await client.workflows.deleteConnection("connection_id");

组件

const pieces = await client.workflows.listPieces({ limit: 20 });

触发器

const status = await client.workflows.getTriggerRunStatus();
await client.workflows.testTrigger({ pieceName: "@activepieces/piece-webhook", flowId: "flow_id" });

表格与记录

const { data: tables } = await client.workflows.listTables();
const table = await client.workflows.getTable("table_id");
const { data: records } = await client.workflows.listRecords({ tableId: "table_id" });

MCP 服务器

const { data: servers } = await client.workflows.listMcpServers("project_id");
const server = await client.workflows.getMcpServer("mcp_server_id");
const newServer = await client.workflows.createMcpServer({ projectId: "project_id", name: "My MCP" });
await client.workflows.deleteMcpServer("mcp_server_id");
await client.workflows.rotateMcpToken("mcp_server_id");

用户邀请

const { data: invitations } = await client.workflows.listInvitations({ type: "PROJECT" });
await client.workflows.deleteInvitation("invitation_id");