快速调用Dify工作流API(附完整代码)

调用Dify工作流API实战

Dify中已经被创建好的工作流如何调用?如何起一个简单的服务器和前端页面跑起来?

步骤如下:

1. 首先需要在Dify中发布工作流,然后点击访问API。进入访问API页面后,可以看到Base URL,这个url就是你要调用的地址。

2. 获取密钥。点击页面左侧的“监测”,进入监测页面。点击这里就可以获取API钥。

3. 在服务器后端中调用Dify工作流端点。

// Proxy endpoint to call Dify workflow
app.post('/api/run', async (req, res) => {
  try {
    const { event, main_point } = req.body || {};
    if (!event || typeof event !== 'string') {
      return res.status(400).json({ error: 'Missing required field: event' });
    }
    if (!main_point || typeof main_point !== 'string') {
      return res.status(400).json({ error: 'Missing required field: main_point' });
    }

    const difyUrl = process.env.DIFY_WORKFLOW_URL || 'http://你的地址/v1/workflows/run';
    const difyApiKey = process.env.DIFY_API_KEY || '你的密钥';

    const payload = {
      inputs: { event, main_point },
      response_mode: 'blocking',
      user: 'web-user'
    };

    const response = await axios.post(difyUrl, payload, {
      headers: {
        'Content-Type': 'application/json',
        'Authorization': `Bearer ${difyApiKey}`
      },
      timeout: 60000
    });

项目的完整代码在这里:

https://github.com/Ljxn/Dify_workflow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值