Introduction to Directus Flows
Hi, Bryant here for Directus. In this video, we're going to give you a quick overview of Directus Flows, which is a tool that a lot of our community members are very excited about.
Let's get into it.
What is Directus Flows?
Data is the lifeblood of any digital project and Directus makes it super easy to browse, manage, update, and visualize your data with all the tools our platform provides. But today we're talking about automating your manual data tasks that could slow you down or stop you.
So your first question is, what is Directus Flows? Directus Flows is a tool that enables custom event-driven data processing and task automation. It eliminates a lot of the boilerplate and custom code that you have to write for repetitive development tasks like sending email notifications to users, sending or receiving data from other systems. Using flows, you can automate simple tasks or even complex automations inside your Directus project. You could say it's similar to having your own Zapier or Make.com, right inside your Directus instance.
Common Use Cases for Flows
So how can I use flows? What are some of the common use cases for it? Here's a few that might ring a bell for you. You might want to, uh, alert your team when a business metric passes a certain limit. Maybe you receive a thousand stars on GitHub. You want to update data in one collection when data in another collection changes. If you want to set up content workflows with emails and notifications for reviews or approvals, let's say you're deploying a static site whenever your content is created or updated or approved by a content manager.
And Directus Flows is also great for scheduled jobs, like running reports on a daily or weekly basis, or ingesting data from another service's API on the hour, every hour.
How to Create a Flow: Triggers and Operations
How do I create flows? Flows features a simple drag and drop workflow builder for creating your automations, and Flows is located within the Settings section of your Directus instance. Each flow consists of a single trigger, the event or action that kicks off the automated process, and one or more operations.
Operations are simply the actions or processes that are performed on your data after the flow has been triggered.
Exploring Different Trigger Types
Let's cover triggers first. There are lots of potential triggers that you might use to kick off one of your flows. Let's cover a few examples.
You can use the event hook trigger anytime you want to start a flow, whenever a certain event happens inside Directus, like whether a new user logs in or a new file is uploaded, if items within a specific collection are created, updated, or deleted. To start a flow based on events that happen in a third party system, use the webhook trigger to get a URL where you can make a request to. A schedule or cron trigger allows you to kick off a flow based on a specific time interval, whether that's every 10 or 15 minutes, once a day, or only Mondays at noon. You can also chain flows together by triggering one flow based on another.
And if you prefer to trigger a flow manually, you can do that as well by choosing a specific collection and then opening up that collection. And you'll find that on the right hand side where you can trigger that flow.
Core Concepts of Operations
So let's move on to our operations. They're the actions that our flow is performing. At the core, operations do three things. Number one, get data from Directus or another service. Number two, process data, like transforming it or validating it. Number three, sending data to Directus or some other third party service.
So here's a few examples of some of the operations. If you need to change the path of a particular flow based on a specific piece of data, use the condition operation to set up some if-else logic. This follows the standard Directus filter rules syntax. If you want or need the power of JavaScript, use the run script operation to write arbitrary code to transform your data however you'd like.
You've also got an access to perform any CRUD operations against any of the items inside your database. So you can create data, delete data, update data, do whatever you want with any of the items inside the collections of your database.
Advanced Operations
The log to console operation is useful in debugging your flows so you can log any message. And even use the variables inside your JSON object. One of the handiest operations is the ability to send emails or notifications based on certain events that happen. Directus makes this super simple where it might take you 25, 30 minutes to code this up previously.
The webhook operation allows you to send any type of HTTP request to whatever third party system you specify at a specific URL. You can pass along any headers or any body information that you choose to include. If you're trying to format data for a third party, the transform payload operation is a great tool to use. And again, you can chain flows together using the trigger flow operation to select the flow that you would like to trigger and pass along your data.
Robust Error Handling in Flows
Operations may not always execute successfully. Unlike other platforms or tools, this does not immediately stop your workflow. Flows gives you an easy way to implement control flow by adding success and failure paths to each operation. This allows you to build more robust flows that can handle errors and error from a third party API or service.
Managing Data with the Flow JSON Object
Each flow creates its own JSON object to store the data that's generated. There are three keys appended to the JSON object whenever a flow begins. $trigger, which holds the data that is generated by the Flow's trigger event. $accountability, which provides details on who started the flow and what permissions they have access to. And $last but not least, which is a handy shortcut that stores the data of the last operation executed.
Once an operation finishes, the data generated by that operation is appended to the JSON object under its operation key. When you're creating operations, you can access the data generated by using those keys as variables. All you have to do is wrap the variable with quotes and double mustaches like this. One quick tip: you cannot pass any type of computation though. For that, you'll want to use the script.
Debugging with Flow Logs
Logs are generated and stored in the database each time your flow runs, and the logs are there to help you understand how a flow was triggered, what operations were ran, and if there were any errors during the execution of that flow. Each trigger and operation will show as its own dropdown where you can see all the relevant data.
Conclusion and Next Steps
All right, so that is the overview of Directus Flows. The possibilities of what you can build with this awesome tool are pretty limitless. Flows is intentionally broad and flexible to support whatever use cases you may come up with.
Now that you've got a good understanding of what flows are and how they work, make sure you check out the Advanced Flows videos that show you how to actually build your own automated workflows step by step in detail. And I'd also recommend reviewing the documentation for Directus Flows because there's tons of helpful tips that you can get right now. Thanks for watching.