Call and get data from your Functions.

create-event-header

Functions are similar to Event-Workflows but with the significant difference that you can return data from the function to the flow that has called it. Authentication, complex calculations, or error handling are only a few of many great examples within functions to keep API Routes and Workflows smaller and easier to manage.

Overview

Functions can be created and managed like API Routes or Workflows through its navigation menu point. Edit the top most action in a function to give it a name, identifier and description and then use the “Return Data” Action to return data back to the flow that called it.

Note that both the input into a function and the data returned from a function must be a JSON object, otherwise the function call will fail.

Action

Specifying the Function to call

As the first step, you must decide which function you want to call. You can either use the dropdown for this or click on the {} symbol to the right of it and then use the text input field, specifying the id.

The text input field is a powerful way to specify the function to call because you can use it to specify the function id via a variable like {{$var.my-function-to-call}} and then have the flow dynamically decided which function to call based on that variable.

Call Draft Version

By default, the live version of the function is called. However, for development and debugging, you can call the draft version instead.

Infinite Loop

A function can trigger itself directly or indirectly by triggering other workflows and routes that then call the function again. While these loops are powerful, they need to be handled with care, since a function that only and always triggers itself is a loop, that once started, can’t be stopped. This function would then run infinitely until the users usage quota is completely depleted for the month (in the worst case even causing lots of costs).

That’s why, by default, functions that seem like possibly infinite loops are stopped before they can do too much harm.

If, however, you are sure that your function is in fact not a infinite loop, then you can toggle Allow triggering itself.

To be clear: even when Allow triggering itself is false, the function is able to call itself but with the difference that it’ll be monitored and aborted early if it becomes to much.

Example

In the following example, we have many API Routes that all need the same custom header-based authentication. So, we create a function first to check if the required {{$header.secret}} is present and, if so, to decode and verify the value based on our custom authentication logic. We then return data in the format of { "authenticated": false, "error": "missing secret" } or { "authenticated" true } so that the parent flow can use a simple If Action to check if authenticated is true.

Now we have to add the Call Function action to an API route, and since the Functions don’t have access to anything from the parent flow, pass the header secret into the function using { "header-secret": "{{$headers.secret}}" } and access it from within the function with {{$param.header-secret}}.