DocsData & access · FRL
On this page

Calculate values in FRL

Use expressions, scratch data and helper functions.

Before you begin: Use request and event context.

Expressions

FRL expressions support paths, strings, numbers, arrays, objects, function calls, comparisons, logical operators, math operators, unary operators, and template strings.

frl

Rule excerpt: add this to a complete FRL rule before running it. Try a complete example →

Use semicolons after assignments, returns, and function calls inside hooks, endpoint bodies, and function bodies. A newline alone does not end these statements.

Build nested scratch data as you go

You can assign to a deep $context path without creating each parent object first. For example, $context.order.data.items = []; creates order and data if they are missing. This makes it easy to assemble a response or collect values across a hook or endpoint:

backend/index.frl
Rule input (JSON)

Use a complete version 2 rule file. Select a post with “postType” and “action”, or an “endpoint” by name. Saved data, jobs, payments and email need the local server. Dedupe replay protection also needs a site.

Check complete rules or run schemas, permissions, hooks and calculations against the input above. Add excerpts to a complete rule before running.

On the first assignment, FRL creates the missing preview and recipient objects. The same behavior works with $local inside a helper function:

backend/index.frl
Rule input (JSON)

Use a complete version 2 rule file. Select a post with “postType” and “action”, or an “endpoint” by name. Saved data, jobs, payments and email need the local server. Dedupe replay protection also needs a site.

Check complete rules or run schemas, permissions, hooks and calculations against the input above. Add excerpts to a complete rule before running.

$context is scratch data for the current rule run. $local belongs to one helper-function call; jobs also have $local scratch data. Neither is saved automatically. To change a saved post, use a post write such as post.edit(...) or write to $post.pending.data in a before hook. A deep assignment creates parents only when they are missing or null: if an existing parent is a string, number, or other non-object, the assignment fails. Helper functions can read $context, but they write their own $local values and return results for the caller to assign.

Helper Functions

Built-in helpers include:

FunctionExamplePurpose
str.lower(value)str.lower($post.pending.data.email)Lowercase a string.
str.upper(value)str.upper($post.pending.data.code)Uppercase a string.
str.trim(value)str.trim($post.pending.data.name)Trim whitespace.
str.length(value)str.length($request.body.attemptId)Count string length; empty or missing values return 0.
str.includes(value, textOrArray)str.includes($post.pending.data.title, "pro")Check string contents.
str.slice(value, start, end?)str.slice("Frontbacked", -6)Return part of a string; negative positions count backward from the end.
str.slug(...values)str.slug($post.pending.data.title)Build a URL-friendly slug with a six-character random suffix to reduce collisions.
random.id(length)random.id(18)Generate a secure alphanumeric id.
random.string(lengthOrOptions?, options?)random.string(8, { alphabet: "numbers" })Generate invite codes, PINs, and custom tokens.
arr.size(array)arr.size($post.pending.data.tags)Count array items.
math.min(...)math.min($post.pending.data.price, 100)Return the minimum value.
math.max(...)math.max($post.pending.data.price, 0)Return the maximum value.
crypto.verifyHmac(options)crypto.verifyHmac({ secret, payload, signature })Verify signed webhook requests.

Theme rules can also declare local functions with fn. Call them through $fn everywhere, including schema expressions, permissions, hooks, endpoints, jobs, and other helpers. For example, use $fn.canEdit() for your helper and $admin.canEdit("shipments") for an admin permission helper. Bare calls are reserved for native language helpers such as must(); canEdit() does not refer to your declared helper.

backend/index.frl
Rule input (JSON)

Use a complete version 2 rule file. Select a post with “postType” and “action”, or an “endpoint” by name. Saved data, jobs, payments and email need the local server. Dedupe replay protection also needs a site.

Check complete rules or run schemas, permissions, hooks and calculations against the input above. Add excerpts to a complete rule before running.

Helper functions can be used inside allow rules, hooks, endpoint bodies, and other helpers. They can read the current $context, $actor, $admin, and resource helpers such as posts.count(...). Helpers called from endpoint blocks can also read $request. Use $local for temporary values inside the helper, then return the result.

Secure Defaults

If a concrete post action has no matching allow rule, it is denied. This is intentional. A theme should explicitly say who can create, get, edit, and delete each protected post type.

For public form submissions, use a condition based on the submitted data instead of leaving the rule open by accident.

frl

Rule excerpt: add this to a complete FRL rule before running it. Try a complete example →

For site-owner actions, prefer $admin permission helpers scoped to the content being managed.

frl

Rule excerpt: add this to a complete FRL rule before running it. Try a complete example →

Named declarations follow the FRL naming rules. The system, system_…, and system.… names are reserved for Frontbacked, case-insensitively. Ordinary data fields are unaffected.