Functions
> Functions are reusable flows with defined inputs and outputs that can be called from any route or other function.
A function in Brickr is a self-contained flow that encapsulates a piece of logic you want to reuse. Instead of duplicating the same sequence of nodes across multiple routes, you extract that logic into a function and call it wherever you need it.
Why use functions
Functions solve several common problems:
- Avoid duplication -- Write logic once, use it in many routes.
- Simplify complex flows -- Break large flows into smaller, manageable pieces.
- Maintain consistency -- Update the function in one place and every caller gets the change.
- Improve readability -- A single "call function" node is easier to understand than a sprawling inline flow.
If you find yourself copying the same group of nodes between routes, that is a strong signal to extract them into a function.
Creating a function
1. Navigate to Dashboard > Functions. 2. Click Create Function. 3. Enter a name for your function. 4. Click Create to open the function in the Builder.
The function opens with a Start Node, just like a route. The difference is that a function's Start Node represents the function being called, not an HTTP request arriving.
Defining inputs
Function inputs are the values that callers pass in when they invoke the function. You define them on the function's Start Node.
1. Click the Start Node to open its configuration panel. 2. Add input pins by specifying a name and type for each one. 3. These inputs appear as output pins on the Start Node, making them available throughout the flow.
For example, a function that formats a user greeting might have two inputs:
| Input name | Type | Description | |------------|------|-------------| | firstName | String | The user's first name | | lastName | String | The user's last name |
Input names should be descriptive and follow a consistent naming convention. This makes the function easier to use for anyone on your team.
Defining outputs
Function outputs are the values that the function returns to the caller. You define them using a Return Node at the end of your flow.
1. Add a Return Node to your function's sequence. 2. Configure the output pins with names and types. 3. Connect the values you want to return to the Return Node's input pins.
A function can have multiple outputs. For example, a function that validates user data might return:
| Output name | Type | Description | |-------------|------|-------------| | isValid | Boolean | Whether the data passed validation | | errors | Array | List of validation error messages |
Calling a function
To use a function inside a route or another function, use the Call Function node.
1. Open the quick menu (drag from a pin or right-click the canvas). 2. Search for Call Function or find it in the node list. 3. Select the function you want to call. 4. The node will display input pins matching the function's defined inputs and output pins matching its defined outputs. 5. Connect the input values and use the output values downstream in your flow.
When you update a function's inputs or outputs, the Call Function nodes that reference it will update automatically to reflect the changes.
Functions vs. routes
Both functions and routes contain flows built with nodes, but they serve different purposes:
| | Routes | Functions | |---|--------|-----------| | Triggered by | HTTP requests, cron, form submissions | Being called from another flow | | Has a URL | Yes | No | | Can return HTTP responses | Yes | No (returns data to caller) | | Can be reused | Only via HTTP call | Directly, with no network overhead | | Inputs | Request data (body, params, headers) | Explicitly defined pins | | Outputs | HTTP response | Explicitly defined return values |
Functions execute within the calling flow's context. A function called from a route shares the same execution time budget as that route. Deeply nested function calls can add up.
Best practices
- Keep functions focused -- Each function should do one thing well. If a function is getting complex, consider splitting it into smaller functions.
- Name functions clearly -- Use names that describe what the function does, such as
validate-emailorcalculate-order-total. - Document inputs and outputs -- Use descriptive names and consistent types so callers understand the interface without inspecting the internals.
- Avoid circular calls -- A function should not call itself or create a cycle through other functions. This will cause execution to fail.
What's next?
| Topic | Description | |-------|-------------| | Routes | Configure API routes that call your functions | | Visual Editor | Master the editor interface | | Nodes | Explore available node types |