Integrate Dify applications

Updated at:

The MagicPen platform provides a powerful feature for integrating Dify applications. With simple configurations, you can seamlessly connect to Dify applications and exchange data. This document describes how to create and use Dify application integrations.

Features

The Dify application integration feature on the MagicPen platform supports the following:

  • Stream support: You can enable Server-Sent Events (SSE) in the configuration panel to support streaming data.

  • Custom trigger methods: You can set integration operations to trigger manually or run automatically to suit different business scenarios.

  • Callbacks and data transformation: You can execute specific integration operation events after a query succeeds or fails. You can also apply custom transformations to the returned data.

  • Advanced settings: You can use advanced configurations for debouncing and conditional execution to optimize integration operations.

For more information about events, see Integration operation events.

Using a Dify application integration

Step 1: Create a Dify application integration

  1. Log on to the MagicPen platform and go to the console.

  2. Navigate to Resources > Integration and click Create Integration.

  3. Select Dify and enter a name and description for the integration.

  4. Configure the Dify application connection information for different environments. This includes the Dify Base URL and API-KEY.

  5. Click OK to create the Dify application integration.

Note

For more information about how to create a Dify application, see Build Dify applications. You must configure a different API-KEY for each application.

Step 2: Create an integration operation within the application

  1. Open the Code panel. Click the + icon for the relevant scope and select Integration Operation.

  2. Select the Dify application integration that you created as the resource. Then, configure the parameters, trigger method, and other settings as required.

  3. Click Run to preview the query results.

Configuration panel details

General configurations

  • The parameters that you configure depend on the operation type. The following operation types are available:

    • Chat application - Send chat message: This operation calls the send message API of a Dify chat application, such as a Chatbot and Agent. The parameters are as follows:

      • Question content: The question sent to the Dify application.

      • Input variables: The input variables configured in the Dify application. For example, if the Dify application has an input variable named city, pass it in the format shown in the following figure. If no input variables are configured, leave this field blank.image

      • User identity: This parameter identifies the user for the conversation. Dify applications require a user identity to record logs.

      • sse: Specifies whether to enable streaming responses.

        Important

        You must enable the SSE streaming switch for Dify chat applications. Otherwise, the response will fail.

    • Workflow application - Execute workflow: This operation calls a Dify workflow application to execute a workflow. The parameters are as follows:

      • Input variables: The input variables configured in the Dify application. For example, if the Dify application has an input variable named city, pass it in the format shown in the following figure. If no input variables are configured, leave this field blank.image

      • User identity: This parameter identifies the user for the conversation. Dify applications require a user identity to record logs.

      • sse: Specifies whether to enable streaming responses.

        Important

        For Dify workflow applications, you must set the callback data transformation to return data to return the complete workflow response. Do not enable the SSE streaming switch when you execute workflows.

    • Text generation application - Send message: This operation calls a Dify text generation application to send a message. The parameters are as follows:

      • Input variables: The input variables configured in the Dify application. For example, if the Dify application has an input variable named city, pass it in the format shown in the following figure. If no input variables are configured, leave this field blank.image

      • User identity: This parameter identifies the user for the conversation. Dify applications require a user identity to record logs.

      • sse: Specifies whether to enable streaming responses.

Callback configuration

  • Data transformation: This setting processes the data returned by the API. The default value is return data, which means no processing is performed.

  • Stream reception callback: This setting configures the event that is triggered when a data chunk is received from a streaming API call.

  • Success callback: This setting configures the event that is triggered when the API call is successful.

  • Failure callback: This setting configures the event that is triggered when the API call fails.

Advanced configuration

  • Debounce: This setting sets a debounce expression to prevent frequent API calls.

  • Execution prevention condition: This setting determines whether to execute this integration operation based on a conditional expression.

Manually triggering an integration

Event trigger

  1. In the component that triggers the integration operation, configure an event handler.

  2. Set the action type to Integration Operation and select the corresponding integration.

  3. Enter the configured parameters as an object.

API trigger

If the name of the integration operation is action1, you can use the action1.trigger() method to trigger the operation from any location where scripts can be run. If the integration operation has parameters, pass them as an object.