Expressions
Expressions are inline JavaScript expressions executed in an embedded JS runtime.
Expressions within R-Templates can themselves be
templated to access provider and var data. Expressions within Queries
are not templated, though standard JS string interpolation is possible; provider values are read
simply by the provider name, and vars are accessed through the _v object.
Expressions are most commonly used to access data from a provider (via templates) or to transform data from an HTTP response to be sent into a provider. In addition to standard JS operations, some helper functions are provided.
Only simple expressions can be executed, meaning you cannot have multiple lines.
Helper functions
"Dummy" parameters
Any ${x:_} interpolation within an R-Template that does not rely
on any providers will be statically evaluated once to reduce redundant JS execution. While this
does not cause issues in most cases, there are some functions (such as random() and epoch())
that can return different values for the same input.
When ${p:null} is Required
Starting in version 0.6.1, random() and epoch() are automatically evaluated per-request in
most contexts (URLs, headers, body, logger queries). However, ${p:null} is still required in
declare blocks.
In declare blocks (requires ${p:null}):
declare:
# ✅ Correct - generates a new random number each time
randomValue: !x '${x:random(0, 100, ${p:null})}'
timestamp: !x '${x:epoch("ms", ${p:null})}'
# ❌ Wrong - evaluates once and reuses the same value
randomValue: !x '${x:random(0, 100)}'
timestamp: !x '${x:epoch("ms")}'
In URLs, headers, body, and loggers (automatic, no ${p:null} needed):
endpoints:
- method: POST
# ✅ All of these automatically generate new values per-request
url: 'http://localhost:8080/?ts=${x:epoch("ms")}'
headers:
X-Request-Time: '${x:epoch("ms")}'
body: !str '{
"timestamp": ${x:epoch("ms")},
"randomId": ${x:random(1000, 9999)}
}'
How the Dummy Parameter Works
In declare blocks, expressions that don't reference providers are evaluated during configuration
loading. By adding ${p:null}, you create a provider reference, forcing the expression to be
evaluated during request execution instead:
declare:
foo: !c
collects:
- take: 5
from: ${x:random(0, 100, ${p:null})}
as: _nums
then: ${x:entries(${p:_nums})}
Since this declare relies on a provider value (${p:null}), random() will be called each time.
The dummy value is not used internally.
This limitation/workaround only applies to ${x:_} segments in declare blocks. Query
expressions and request-time templates (URL, headers, body, loggers) are evaluated normally.
Function list
| Function | Description |
|---|---|
encode(value, encoding)
|
Encode a string with the given encoding. value - any expression. The result of the expression will be coerced to a string if needed and
then encoded with the specified encoding.
Example: with the value |
end_pad(value, min_length, pad_string)
|
Pads a string or number to be minimum length. Any added padding will be added to the end of the string. value - An expression whose value will be coerced to a string if needed. Example: with the value |
entries(value)
|
Returns the "entries" which make up value. For an object this will yield the object's key/value pairs. For an array it yields the array's indices and elements. For a string it yields the indices and the characters. For boolean and null types it yields back those same values. Examples With the value
would return
would return
would return
would return |
epoch(unit)
or
|
Returns time since the unix epoch. unit - A string literal of Example:
|
|
or
|
Turns an array of values into a string or turns an object into a string. value - any expression. When the expression resolves to an array, the elements of the array are
coerced to a string if needed and are then joined together to a single string using the specified
separator. When the value resolves to an object and the three argument variant is used then the
object will be turned into a string with the specified separators. In any other case value is
coerced to a string and returned. Examples With the value With the value
or for an alternative, json-ified view: |
json_path(object, query)
|
Provides the ability to execute a json path query against an object and returns an array of values. The query must be a string literal. Example: |
match(string, regex)
|
Allows matching a string against a regex. Returns an object with the matches from the regex. Named matches are supported though any unnamed matches will be a number based on their position. Match If the first parameter is not a string it will be coerced into a string. Regex look arounds are not supported. Example: If a response body were the following:
Then the following expression:
Would return:
|
parseInt(value)
|
Converts a string or other value into an integer ( value - any expression. The result of the expression will be coerced to a string if needed and then converted. |
parseFloat(value)
|
Converts a string or other value into an floating point number ( value - any expression. The result of the expression will be coerced to a string if needed and then converted. |
|
or
|
Generates a random number between start (inclusive) and end (exclusive). Both start and end must be number literals. If both numbers are integers only integers will be generated within the specified range. If either number is a floating point number then a floating point number will be generated within the specified range. dummy - Optional dummy parameter. Use Example:
|
|
|
Creates an array of numeric values in the specified range. start - any expression resolving to a whole number. Represents the starting number for the range (inclusive). end - any expression resolving to a whole number. Represents the end number for the range (exclusive). Examples:
|
|
or
|
Creates an array of Example: |
replace(needle, haystack, replacer)
|
Replaces any instance of a string (needle) within a JSON value (haystack) with another string (replacer). This function will recursively check the JSON for any string value of needle and replace it with replacer. This includes checking within a nested object's key and value pairs, within arrays and within strings. needle - an expression whose value will be coerced to a string if needed. Example: with the value |
start_pad(value, min_length, pad_string)
|
Pads a string or number to be minimum length. Any added padding will be added to the start of the string. value - an expression whose value will be coerced to a string if needed. Example: with the value |
stwrap(string)
|
String Wrap. "Wraps" a String by adding a |
val_eq(value1, value2)
|
Performs by-value equality comparison of the two values. In Javascript, Array and Object types are compared by reference, so |
Custom Javascript
Custom Javascript can be loaded to add additional helper functions to expressions. The js source to be used is optionally provided in the lib_src top-level key.
Custom js is only loaded from that single location, so external modules or Node packages cannot be used. Functions should be simple transformations that could not be easily expressed with the built-in offerings. 1
All helper functions, predefined or custom, are added into the same namespace, so custom js can call the predefined functions naturally.
Javascript code is executed by boa engine.
Example
Assuming that lib_src refers to the following Javascript code:
function plus_two (x) {
return x + 2;
}
then the R-Template ${x:plus_two(${p:foo})} can be executed.
1 This also means don't make the functions async.