|
| 1 | +--- |
| 2 | +title: Dify integration |
| 3 | +description: Connect Apify with Dify to automate workflows by running Actors, extracting data, and responding to Apify events. |
| 4 | +sidebar_label: Dify |
| 5 | +sidebar_position: 7 |
| 6 | +slug: /integrations/dify |
| 7 | +--- |
| 8 | + |
| 9 | +**Connect Apify with Dify to automate workflows by running Actors, extracting structured data, and responding to Actor or task events.** |
| 10 | + |
| 11 | +--- |
| 12 | + |
| 13 | +[Dify](https://dify.ai/) is an intuitive LLM application development platform for creating and operating AI-native apps. With the Apify integration for Dify, you can connect Apify Actors and storage to your Dify Workflows and Chatflows. You can run scrapers, extract data, and trigger workflows based on Apify events. |
| 14 | + |
| 15 | +In this guide, you'll learn how to set up authentication and incorporate the Apify plugin into your Dify applications as either a tool (action) or a trigger. |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +Before you begin, make sure you have: |
| 20 | + |
| 21 | +- An [Apify account](https://console.apify.com/) |
| 22 | +- A [Dify account](https://dify.ai/) (self‑hosted or cloud) |
| 23 | + |
| 24 | +## Authentication |
| 25 | + |
| 26 | +To connect your Apify account, you need to configure the plugin with your Apify API token. |
| 27 | + |
| 28 | +1. In Dify, navigate to **Plugins** from the top menu. |
| 29 | +2. Find the **apify-integration** plugin and click **API Key Authorization Configuration**. |
| 30 | +3. In the pop-up menu, paste your Apify API token. You can find it in the [Apify Console](https://console.apify.com/settings/integrations). |
| 31 | +4. Click **Save**. |
| 32 | + |
| 33 | + |
| 34 | + |
| 35 | +A green indicator light confirms that your credentials are correct. |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | +With authentication set up, you can now add Apify tools to your applications. |
| 40 | + |
| 41 | +## Using Apify Tools in a Workflow |
| 42 | + |
| 43 | +Start by creating a workflow in Dify, then add Apify tools to handle tasks like running Actors or fetching data. |
| 44 | + |
| 45 | +1. In the Dify Studio, create a new **Workflow** application. |
| 46 | +2. Choose **Create from Blank**. |
| 47 | +3. Click the **+** icon on the **Start** node and select an Apify tool to add it to your board. |
| 48 | + |
| 49 | + |
| 50 | + |
| 51 | + |
| 52 | +### Use Apify as an action |
| 53 | + |
| 54 | +Actions allow you to perform operations like running an Actor within a workflow. For instance, you could trigger a scraper and then use its results in subsequent steps. |
| 55 | + |
| 56 | +#### Run an Actor |
| 57 | + |
| 58 | +1. Add the **Run Actor** tool to your workflow. |
| 59 | +2. Configure it: |
| 60 | + - **Actor ID**: The unique ID of the Actor to run. For example, `2Mdma1N6Fd0y3QEjR` for the Google Maps Scraper. |
| 61 | + - **JSON body for input**: The JSON input for the Actor run. |
| 62 | + ```json |
| 63 | + { |
| 64 | + "language": "en", |
| 65 | + "locationQuery": "New York, USA", |
| 66 | + "maxCrawledPlacesPerSearch": 50, |
| 67 | + "searchStringsArray": [ "restaurant" ], |
| 68 | + "skipClosedPlaces": false |
| 69 | + } |
| 70 | + ``` |
| 71 | + - **Wait for finish**: Select `True` to make the workflow wait for the run to complete. |
| 72 | + |
| 73 | +3. Configure an **End** node and configure its output variable to use the `json` result from the **Run Actor** node. |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | +After that we can start the workflow and see the results. |
| 78 | + |
| 79 | + |
| 80 | + |
| 81 | +#### Run an Actor task |
| 82 | + |
| 83 | +1. Add the **Run Actor task** tool to your workflow. |
| 84 | +2. Enter the **Task ID** you want to execute (e.g., `FThsabHGXorVhWbPV`). |
| 85 | +3. Add an **End** node and map its output variable to the `json` result from this tool. |
| 86 | + |
| 87 | + |
| 88 | + |
| 89 | + |
| 90 | +#### Scrape Single URL |
| 91 | + |
| 92 | +1. Add the **Scrape Single URL** tool. |
| 93 | +2. Enter the **URL** to scrape (e.g., `https://en.wikipedia.org/wiki/Deutsche_Bahn`). |
| 94 | +3. Select the **Crawler type**. **Raw HTTP** is recommended for speed to work within Dify's timeout limits. |
| 95 | +4. Add an **End** node and map its output variable to the `json` result. |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +#### Get Dataset Items |
| 101 | + |
| 102 | +1. Add the **Get Dataset Items** tool. |
| 103 | +2. Enter the **Dataset ID** to retrieve items from. (e.g., `58S2Um7hSXWYghZhn`) |
| 104 | +3. Add an **End** node and map its output to the tool's `json` result. |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +#### Get Key-Value Store |
| 110 | + |
| 111 | +1. Add the **Get Key-Value Store** tool. |
| 112 | +2. Enter the **Store ID** and **Key** for the record you want to retrieve. (e.g., Store ID `fC0qZmL5RWIxoJG8z`, Record Key `INPUT`) |
| 113 | +3. Add an **End** node and map its output to the tool's `json` result. |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +## Use Apify as a trigger |
| 119 | + |
| 120 | +Triggers let your Dify application respond automatically to events in Apify, such as when an Actor run finishes. This is ideal for creating event-driven automations. |
| 121 | + |
| 122 | +1. In your Dify app, go to **Overview** > **Endpoints**. |
| 123 | +2. Click the **+** icon to create a new endpoint. |
| 124 | +3. Select the **Workflow** or **Chatflow** application you want to trigger and give it a name. |
| 125 | +4. **Save** the endpoint. Dify will generate two URLs. |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + |
| 130 | +5. Copy the URL that corresponds to your application type (Workflow or Chatflow). |
| 131 | +6. In the Apify Console, open the Actor you want to use as a trigger. |
| 132 | +7. Go to the **Integrations** tab and select the **HTTP webhook** option. |
| 133 | +8. Paste the Dify endpoint URL. |
| 134 | +9. Under **Events**, select **Run succeeded**. |
| 135 | +10. **Save** the integration. |
| 136 | + |
| 137 | + |
| 138 | + |
| 139 | + |
| 140 | +> **IMPORTANT: Publish Required** |
| 141 | +> A Dify Workflow or Chatflow **must be published** for the webhook trigger to work. If the application is not published, the webhook will fail. |
| 142 | +>  |
| 143 | + |
| 144 | +To confirm that your webhook is working, you can create a simple workflow that catches the incoming data from Apify and sends it to a third-party request catcher like [Webhook.site](https://webhook.site/). This provides an easy way to inspect the data we send. |
| 145 | + |
| 146 | + |
| 147 | +Now we can start the Actor in the Apify Console and it will trigger our workflow. |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +The setup for a Chatflow is nearly the same. The only differences are to select your Chatflow app instead of a Workflow and to copy the specific Chatflow endpoint URL. |
| 152 | + |
| 153 | +> **Chatflow Trigger Requirement** |
| 154 | +> Also, when triggering a **Chatflow**, you **must** provide a **Payload template** in the Apify webhook settings. This template needs a `query` field to serve as the first message to the chatflow. |
| 155 | +>  |
| 156 | + |
| 157 | +### Accessing Apify Data in Dify |
| 158 | +When an event in Apify triggers your Dify endpoint, Apify sends a detailed nested response [JSON object](https://docs.apify.com/api/v2/act-runs-post#responses) containing information about the event. We can use this data to power our workflow. |
| 159 | + |
| 160 | +Dify cannot directly use nested syntax like `resource.id` in its variable system. Instead, it requires you to access nested properties using a flattened format (`resource__id`), where each level of nesting is represented by a double underscore (`__`). |
| 161 | + |
| 162 | +To work with a nested property in your workflow, you must first define it as an input variable in your workflow's Start node by clicking `+`. |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + |
| 170 | +We can also use that variable inside a JSON object we send and see the results. |
| 171 | + |
| 172 | + |
| 173 | + |
| 174 | + |
0 commit comments