Published on

Integrate Stripe to Directus' flows.

Authors

Integrate Stripe to Directus' flows

Integrating Stripe to a Directus' flow is quite simple. However, it is not obvious since it is not explained (yet?) how to make use of nodejs modules inside flows.

The answer is in this github discussion. Directus makes use of vm2 which: "is a sandbox that can run untrusted code with whitelisted Node's built-in modules. Securely!"

So, you just have to allow stripe to run in Flows by adding: FLOWS_EXEC_ALLOWED_MODULES=stripe, in the .env file of directus.

Once you have done this, you can make use of the Run Script operation in Flow like that:

module.exports = async function (data) {
  const stripe = require('stripe')('sk_test_something')

  // create a customer in Stripe
  const customer = await stripe.customers.create({
    email: data.read_user.email,
    name: data.read_user.firstName,
    metadata: { directusId: data.read_user.id },
  })
}

In this example, I use stripe to create a stripe's customer. After the flow is triggered, I have created an operation Read data that I have named read_user. Then you can access it in the next operation using data.read_user.

img example-of-flow

Tips

For information, when you use data as input for the function in a Run script operation, it gives you all the payloads from the previous operations of the actual flow.

Something like that:

{
  "$trigger": {
    payload: {
	   variable: "something"
	},
    keys: [0],
  },
  "$last": {
    // informations of the last operation
  },
  read_user: {
    // the payload of the operation named "read_user"
  },
  ...
}

Notice that for the trigger, you have to access it with data["$trigger"].