Files
directus/docs/configuration/flows.md
2022-06-09 11:32:46 -04:00

5.5 KiB

Flows

Flows enable custom, event-driven data processing and task automation within Directus. Each Flow is composed of one Trigger, followed by a series of Operations.

toc

What's a Flow?

What's a Flow?

Each Flow begins with one Trigger, which defines the actions that start the Flow. Triggers can be some event or action within the app, an incoming webhook, a cron job, or manual click of a button. Please see the documentation on Triggers for more details.

Operations are the actions performed after the Trigger, which include things like creating new Items in a Collection, sending off emails, pushing in-app notifications, or sending webhooks, just to name a few options. You can even set up divergent chains of Flow Operations, which execute conditionally, based on whether one Operation passed or failed. To ensure data is passed on as expected, a console log is also included to help design and troubleshoot your Flows. Please see the documentation on Operations for more details.

Once a Flow is triggered, a Flow JSON Object is created which stores data from the Trigger event. Then as each Operation in the flow executes, the data generated is added onto this Flow Object. Every Operation in a Flow has access to this Flow Object.

Create a Flow

To create a Flow, follow these steps:

  1. Navigate to "Settings > Flows" and click add in the Page Header. A side menu will appear.
  2. Under Flow Setup, fill in a name for the Flow.
    Optional: Set a status, material icon, description or color to help remember the Flow.
  3. Select your Activity and Logs Tracking preference as desired.
    To learn more, please see Activity and Logs.
  4. Click arrow_forward to navigate to Trigger Setup, select a Trigger type and configure as desired.
  5. Click done in the Menu Header and you will be taken to the Flow Grid Area.
    A Trigger Panel will be on the Flow Grid.
  6. On the Trigger Panel, click add and the Create Operation side menu will open.
  7. Set a name, select the Operation type, and configure as desired.
    The unique name will be converted into an Operation Key, used on the Flow Object.
    If no name is set, a name and key will be auto-generated.
  8. When done, click done in the Page Header to confirm and return to the Flow Grid Area.
  9. On the newly created Operation Panel:
    • Click add to add Operation after successful execution of the current Operation.
    • Click remove to add Operation after a failed execution of the the current Operation.
  10. Repeat steps 8-10 to create your Flow as desired.
  11. Optional: To edit a Trigger or Operation Panel, click edit and make edits as desired.
  12. Optional: To delete an Operation Panel, click more_vert and click delete.

The Flow Object

When a Flow is triggered, a JSON object is created to store all data generated within the Flow. When you create an Operation, a key is generated. This key is appended to the Flow Object when the Operation executes. It is used to add the associated Operation's data onto the Flows Object. As each Operation in the Flow executes, it has access to the Flow Object and therefore the data generated from preceding Operations.

The following JSON object is a simple example of a Flow with two Operations. The $trigger, $last, and $accountability keys are included on every Flow. An Operation key will be generated for each Operation that executes successfully.

{
   "$trigger": {}, // Data generated by the Flow's Trigger.
   "$last": null, // Data from the last Operation in the flow, for easy access!
   "$accountability": {}, // Provides details on who/what tripped the Trigger and generated this Flow Object.
   "operation_key": { // The data (if any) generated by the first Operation.
			"some_nested_key": "Some nested value.",
    },
	"operation_key_2": null, // Value is null if no data was generated during an Operation.
}

Flow Variables

The Flow Object keys serve as variables, allowing data access from every Flow Operation. Variables must be passed using the following double-moustache syntax. You can even use dot-notation to extract sub-nested values.

{
   "operation_key_3": {
			"user": "{{ $accountability.user }}",
	 }
}

Logs

Logs, accessed from the Sidebar, store a log of information for each Flow execution. Each log will display information from Triggers as well as each Operation.