Functions

Build backend logic with Gherkin, Visual, Split, and Code views.

Functions

Functions contain your backend logic. Build them with Gherkin, Visual, Split, and Code views. Query your database, send emails, call external APIs, and switch to JavaScript when you need more control.

The Spala function editor showing a readable function outline
The readable outline summarizes the function contract and execution path.

What is a function?

A function is a sequence of steps that executes your backend logic. Functions run when an endpoint is called, a task fires, a database trigger activates, an agent runs, or another function calls them. Steps execute top to bottom. Each step does one thing: query data, set a variable, check a condition, call an API, or return a response.

Quick example

Here is a function that gets a case by ID with a 404 fallback:

1. Database Query — Select from cases where id = params.id, assign to caseRecord
2. If/Else — Condition: vars.caseRecord
   - True: Return vars.caseRecord
   - False: Return { error: "Not found" } with status 404

Attach this to a GET /api/cases/:id endpoint and you have a working API route.

Pro tip

Built-in steps stay connected across visual views and Code view. Custom JavaScript is preserved when it cannot be represented as a built-in step.

Common step types

| Step              | What it does                                    |
|------------------|-------------------------------------------------|
| Set Variable     | Store a value for later steps                   |
| Database Query   | Read, insert, update, or delete database rows   |
| If/Else          | Branch logic based on a condition               |
| Loop             | Repeat steps for each item in a list            |
| Return           | Send a response back to the client              |
| External API Call| Call an outside API (Stripe, OpenAI, etc.)      |
| Try/Catch        | Handle errors gracefully                        |
| Run Function     | Call another function as a subroutine           |

Learn more

Create a Function
Step-by-step guide to building your first function.
/flows/creating-flows
Variables & Data
Set variables and access request data, environment variables, and more.
/flows/variables
Code View
Use JavaScript alongside visual flow views.
/flows/code-view
Try & Runs
Use Try, runs, breakpoints, and variable inspection to test functions.
/flows/debugging
Steps Reference
/reference/steps
Complete reference for available step types
Filters Reference
/reference/filters
Data transformation filters for expressions
API Endpoints
/endpoints
Connect your functions to REST API endpoints