Skip to content

Expression Node Context

Expression nodes execute JavaScript code inside a secure sandbox. This page documents all variables, objects, and libraries available within the expression runtime.

Integration inputs are different

Integration nodes also accept expressions on their inputs, but with a much smaller runtime: no return, no libraries, no io, no $ globals and no async/await. See Integration Inputs for that contract.


Global Variables

These variables are injected into every expression execution and provide access to the workflow trigger context, input data, and environment information. In expression nodes, read the event payload and upstream node data from io.input.

$models

An array of Model instances built from the model collection that triggered the workflow. Each entry wraps a raw document from the component; its fields sit directly on the entry ($models[0].status, not $models[0].data.status).

For model CRUD events (Create, Update, Delete), this contains the documents involved in the operation. For manually triggered workflows, it contains whatever documents were selected.

const first = $models[0];
const name = first.name;

models is an alias for $models.

$args

Contains contextual arguments passed from the model operation that triggered the workflow.

Event Contents
Create / Before::Create null
Update / Before::Update { current_revision: {...} } — the document state before the update
Delete / Before::Delete null
Subsequent nodes Merged results from all previously completed nodes
// In an Update workflow — access the previous document revision
const previousDoc = $args?.current_revision;

How $args evolves between nodes

On the trigger node, $args reflects the original model operation arguments. On subsequent expression nodes, upstream data is available through io.input and helpers such as io.getPipedDataOf(nodeName).

$context

An object containing metadata about the event that triggered the workflow.

Field Type Description
event string The event name (e.g. "Create", "Before::Update", "Delete")
initiator object The user or service that triggered the operation
const eventName = $context.event;       // "Create"

$event

A shorthand for the event name. Equivalent to $context.event.

if ($event === "Before::Create") {
    // pre-create logic
}

Possible values: "Create", "Before::Create", "Update", "Before::Update", "Delete", "Before::Delete".

$initiator

The user or service account that triggered the model operation. Shorthand for $context.initiator.

Field Type Description
uuid string The user's unique identifier
identificator string The user's login identifier (e.g. email)
name string The user's display name
const userName = $initiator.name;
const userUUID = $initiator.uuid;

$config

$config is not available in workflow expression nodes. Keep configuration in global environment values instead.

$globalEnv

Global environment variables defined at the project level. These are key-value pairs set in the project's global environment configuration.

const stripeKey = $globalEnv.STRIPE_SECRET_KEY;

Expression nodes run as the workflow's run identity. They get every plain value, and an encrypted value only when that identity holds secrets::read global_env, or secrets::read global_env.<group> for a group's values. An encrypted key the identity may not read is left out, so check for it before use. A dry run and an integration node's expression inputs never get encrypted values. See the security model.

An expression node that declares capabilities runs with only those its identity holds, so it gets encrypted values only with a secrets::read global_env[.<group>] capability. An empty list means no permissions. See Capabilities.

Condition and switch nodes get no $globalEnv. They only decide from their input, with no token and no way to reach the platform.

Every group's keys sit at the top level of $globalEnv, with no group name in the path: a key in the payments group is still $globalEnv.STRIPE_SECRET_KEY. To read one group, call $globalEnv.group(name). It returns a plain object with that group's keys, {} for an unknown group, and leaves out encrypted keys the identity may not read. group is a reserved key name and does not show up in Object.keys($globalEnv).

const stripeKey = $globalEnv.group("payments").STRIPE_SECRET_KEY;

$currentEnv

A string indicating the current environment: "dev" or "live".

if ($currentEnv === "live") {
    // live-only logic
}

I/O and Data Flow

io

The I/O object controls data flow between nodes.

Property or method Description
io.input Input payload and upstream node data available to the expression node
io.pipe(data) Passes data to downstream nodes
io.each(array) Passes an array to a connected Each node for iteration
io.getPipedDataOf(nodeName) Retrieves data piped from a specific upstream node
// read data passed when the workflow event was triggered
const orderId = io.input.order_id;
// pipe transformed data to downstream nodes
io.pipe({ orderId: $models[0].uuid, total: 99.99 });
// pass a list of items to an Each node
io.each($models);
// in a downstream node, read piped data from a specific upstream node
const data = io.getPipedDataOf("Transform Order");
const orderId = data?.orderId;

Available Libraries

All libraries are pre-loaded and available as global objects within the expression sandbox.

Core Utilities

Global Description
moment Moment.js — date/time manipulation
_ Lodash — utility library
Handlebars Handlebars — template engine
Buffer Node.js Buffer operations

Security & Encoding

Global Description
bcrypt Password hashing — genSalt, hash, compare
crypto Cryptographic operations — hash, hmac, encrypt, decrypt, sign, verify
jwt JSON Web Tokens — sign, verify, decode

Debugging

Global Description
debug Debug data collector — data added via debug.add(...) appears in workflow execution logs
decorator Modify the record before it is saved with decorator.set({ field: value }) (used in Before:: events)

Identifiers & Encoding

Global Description
uuid UUID generation — uuid.v4(), uuid.v5(name, ns), uuid.validate(str)
nanoid Nano ID generation — nanoid.generate(size), nanoid.custom(alphabet, size)
btoa Base64 encoding — btoa(string)
atob Base64 decoding — atob(base64)

Authentication

Global Description
otp OTP/2FA — otp.generateSecret(), otp.generateToken(secret), otp.verifyToken(token, secret)

SDK

The Protokol SDK is available as $sdk, a factory class that returns a versioned SDK instance:

const { Component } = $sdk.version('0.9')

const products = new Component("products")
const { settings } = await products.settings()

Note

The SDK inside expressions uses the service context of the current project and environment. Authentication is handled automatically.


Error Handling

RetryableError

Throw a RetryableError to signal that the expression failed due to a transient issue and should be retried by the workflow engine.

throw RetryableError("External API temporarily unavailable");

See RetryableError for retry behavior and backoff details.

Regular Errors

Throw a standard Error to fail the node without retry (a message containing a transient marker such as timeout, connection, network or service unavailable is retried):

throw new Error("Validation failed: missing required field");

Execution Environment

Property Value
Memory limit 128 MB (default)
Timeout 60 seconds (default)
Async support Full async/await support

Warning

Expressions run in an isolated sandbox. Node.js system modules (e.g. fs, path) are not available. Use the provided platform services instead.