Skip to main content
ngrok offers a variety of CEL macros that can be used within the Traffic Policy engine to simplify traffic management and dynamic configuration.
These macros help you streamline traffic handling by referencing common values and conditions, making it easier to manage complex logic in your Traffic Policies.
You can use these macros in combination with expressions and actions to create dynamic, condition-based traffic flows.

Base64

base64.decode() -> bytes

Decodes base64-encoded string to bytes. This function will return an error if the string input is not base64-encoded.

Signatures

  • base64.decode(<string>) -> <bytes>

Example

base64.encode() -> string

Encodes bytes to a base64-encoded string.

Signatures

  • base64.encode(<bytes>) -> <string>

Example

Basic Auth

basic_auth.encode(username, password) -> string

Encodes the passed username and password strings into a Base64 string for use in HTTP Basic Authentication.
Appends the prefix Basic to the encoded string.

Example

Example (in expression)

Bytes

bytes.size() -> int

Determines the number of bytes in a sequence.

Signatures

  • bytes.size() -> int
  • size(bytes) -> int

Example

HTTP requests

Available in on_http_request and on_http_response phases. Returns the cookie struct for the specified cookie name, if it exists on the request.
If there are multiple cookies of the same name, the first from the ordering specified in the Cookie header will be returned.

Example

Example (expression)

getReqHeader(string) -> list

Returns a list of header values for the provided key on the request.
Header keys must be written in canonical format.
Defaults to an empty list if the header is not present.

Example

Example (expression)

getQueryParam(string) -> list

Returns a list of the query parameter values from the request URL for the specified key.
Defaults to an empty list if the query param is not present.

Example

Example (expression)

hasQueryParam(string) -> bool

Returns true or false if the specified query parameter key is part of the request URL.

Example

Example (expression)

hasReqCookie(string) -> bool

Returns true or false if a cookie exists on the request with the specified name.

Example

Example (expression)

hasReqHeader(string) -> bool

Returns true or false if the provided header key is present on the request.
Header keys must be written in canonical format.

Example

Example (expression)

HTTP responses

Available in on_http_response phase only. Returns the cookie struct for the specified cookie name, if it exists on the response.
If there are multiple cookies of the same name, the cookie with the longest path will be returned.

Example

Example (expression)

getResHeader(string) -> list

Returns a list of header values for the provided key on the response.
Header keys must be written in canonical format.

Example

Example (expression)

hasResCookie(string) -> bool

Returns true or false if a cookie exists on the response with the specified name.

Example

Example (expression)

hasResHeader(string) -> bool

Returns true or false if the provided header key is present on the response.
Header keys must be written in canonical format.
Defaults to an empty list if the header is not present.

Example

Example (expression)

Hash

hash.crc32(string) -> string

Computes the CRC32 checksum of a string using the IEEE polynomial.
Returns the checksum in hexadecimal format by default.

Signatures

  • hash.crc32(string) -> string (returns hex)
  • hash.crc32(string, format) -> string (format: “hex” or “base64”)

Examples

hash.crc64(string) -> string

Computes the CRC64 checksum of a string using the ISO polynomial.
Returns the checksum in hexadecimal format by default.

Signatures

  • hash.crc64(string) -> string (returns hex)
  • hash.crc64(string, format) -> string (format: “hex” or “base64”)

Examples

hash.md5(string) -> string

MD5 is considered cryptographically broken and should not be used for security purposes. Use SHA-256 or higher for security-sensitive applications.
Computes the MD5 hash of a string.
Returns the hash in hexadecimal format by default.

Signatures

  • hash.md5(string) -> string (returns hex)
  • hash.md5(string, format) -> string (format: “hex” or “base64”)

Examples

hash.sha1(string) -> string

SHA-1 is considered cryptographically weak and should not be used for security purposes. Use SHA-256 or higher for security-sensitive applications.
Computes the SHA-1 hash of a string.
Returns the hash in hexadecimal format by default.

Signatures

  • hash.sha1(string) -> string (returns hex)
  • hash.sha1(string, format) -> string (format: “hex” or “base64”)

Examples

hash.sha256(string) -> string

Computes the SHA-256 hash of a string.
Returns the hash in hexadecimal format by default.

Signatures

  • hash.sha256(string) -> string (returns hex)
  • hash.sha256(string, format) -> string (format: “hex” or “base64”)

Examples

hash.sha512(string) -> string

Computes the SHA-512 hash of a string.
Returns the hash in hexadecimal format by default.

Signatures

  • hash.sha512(string) -> string (returns hex)
  • hash.sha512(string, format) -> string (format: “hex” or “base64”)

Examples

hash.xxhash64(string) -> string

Computes the XXHash64 hash of a string.
XXHash is a fast, high-quality hash function.
Returns the hash in hexadecimal format by default.

Signatures

  • hash.xxhash64(string) -> string (returns hex)
  • hash.xxhash64(string, format) -> string (format: “hex” or “base64”)

Examples

JSON

json.decode(string) -> list | map

Decodes the passed JSON string into a list or map.

Example

Example (in expression)

json.encode(list | map) -> string

Encodes the passed string into a JSON string.

Example

Example (in expression)

Lists

list.all(x,p) -> bool

Checks if a predicate p holds for all elements of a list, where x is a variable name to be used in p as a reference to the element.

Example

list.encodeJson() -> string

Encodes the list as a JSON string.

Example

list.exists(x,p) -> bool

Checks if a predicate p holds for at least one element of a list, where x is a variable name to be used in p as a reference to the element.

Example

list.exists_one(x,p) -> bool

Checks if a predicate p holds for exactly one element of a list, where x is a variable name to be used in p as a reference to the element.

Example

list.filter(x,p) -> list

Filters a list to include only elements that satisfy a condition, where x is a variable name to be used in p as a reference to the element.

Example

list.join() -> string

Returns a new string with the elements of the list concatenated.
Optionally, a separator can be specified to insert between elements.

Signatures

  • <list<string>>.join(<string?>) -> <string>

Example

list.map(x,t) -> list

Transforms each element in a list by applying the function defined in the expression t,where x is a variable name to be used in t as a reference to the element.

Example

list.map(x,p,t) -> list

Transforms each element in a list by applying the function defined in the expression t to elements that satisfy predicate p, where x is a variable name to be used in p as a reference to the element.

Example

list.size() -> int

Determines the number of elements in a list.

Signatures

  • list.size() -> int
  • size(list) -> int

Example

list.slice() -> list

Returns a new sub-list using the indexes provided.

Signatures

  • <list>.slice(<int>, <int>) -> <list>

Example

list.flatten() -> list

Flattens a list recursively. If an optional depth is provided, the list is flattened to the specified level. A negative depth value will result in an error.

Signatures

  • <list>.flatten() -> <list>
  • <list>.flatten(<int>) -> <list>

Example

list.sort() -> list

Sorts a list with comparable elements. If the element type is not comparable or the element types are not the same, the function will produce an error. Comparable types include: int, uint, double, bool, duration, timestamp, string, bytes.

Signatures

  • <list(T)>.sort() -> <list(T)>

Example

list.sortBy(x, keyExpr) -> list

Sorts a list by a key value. The order is determined by the result of an expression applied to each element of the list. The output of the key expression must be a comparable type, otherwise the function will return an error.

Signatures

  • <list(T)>.sortBy(<bindingName>, <keyExpr>) -> <list(T)>

Example

list.distinct() -> list

Returns the distinct elements of a list.

Signatures

  • <list(T)>.distinct() -> <list(T)>

Example

lists.range() -> list

Returns a list of integers from 0 to n-1.

Signatures

  • lists.range(<int>) -> <list(int)>

Example

Maps

map.all(x,p) -> bool

Checks if a predicate p holds for all elements of a map, where x is a variable name to be used in p as a reference to the key.

Example

map.encodeJson() -> string

Encodes the map as a JSON string.

Example

map.encodeQueryString() -> string

Encodes the map as a URL query string.

Example

map.exists(x,p) -> bool

Checks if a predicate p holds for at least one element of a map, where x is a variable name to be used in p as a reference to the key.

Example

map.exists_one(x,p) -> bool

Checks if a predicate p holds for exactly one element of a map, where x is a variable name to be used in p as a reference to the key.

Example

map.filter(x,p) -> list

Filters a map to include only keys that satisfy a condition, where x is a variable name to be used in p as a reference to the key.

Example

map.map(x,t) -> list

Transforms each key in a map by applying the function defined in the expression t, where x is a variable name to be used in t as a reference to the key.

Example

map.map(x,p,t) -> list

Transforms each key in a map by applying the function defined in the expression t to keys that satisfy predicate p, where x is a variable name to be used in p as a reference to the key.

Example

map.size() -> int

Determines the number of entries in a map.

Signatures

  • map.size() -> int
  • size(map) -> int

Example

Query string

queryString.decode(string) -> map

Decodes the supplied query string into a map.

Example

Example (in expression)

queryString.encode(map) -> string

Encodes the passed map into a query string.

Example

Example (in expression)

Random

rand.double() -> double

Returns a random double between 0 and 1.

Example

Example (in expression)

rand.int(min,max) -> int

Returns a random int between the provided min and max values.
Only supports positive integers and min must be larger than the provided max.
By default, min is 0 and max is 1.

Example

Examples (in expression)

The following is an example of using rand.int with the default values:
The following is an example of using rand.int with custom values:

Secrets

secrets.get(string, string) -> string

Takes the vault name as the first argument and the secret name as the second argument.
Returns the secret value.
Security Macros allow you to access sensitive information directly in your Traffic Policies.
Your ngrok account has a Vault that can store Secrets.
Any secrets that you add to your vault will be available across your account on all Traffic Policies.
Updates to these secrets will be reflected across all Traffic Policies automatically.

How secrets are secured

  • Secrets are protected at rest using industry standard AES-256 encryption
  • ngrok’s REST API does not return secrets as part of any of its response payloads
  • REST API traffic is encrypted in-transit using HTTP/S and TLS 1.2+
For more information on secrets, see Traffic Policy Secrets.

Example

String

string.matches() -> bool

Tests whether a string matches a given RE2 regular expression.
This function provides a simple way to validate patterns in strings.

Signatures

  • matches(string, regex) -> bool
  • string.matches(regex) -> bool

Example

string.startsWith() -> bool

Tests whether a string starts with the specified prefix.

Signatures

  • string.startsWith(prefix) -> bool

Example

string.endsWith() -> bool

Tests whether a string ends with the specified suffix.

Signatures

  • string.endsWith(suffix) -> bool

Example

string.contains() -> bool

Tests whether a string contains the specified substring.

Signatures

  • string.contains(substring) -> bool

Example

string.charAt() -> string

Returns the character at the given position. If the position is negative, or greater than the length of the string, the function will produce an error.

Signatures

  • <string>.charAt(<int>) -> <string>

Example

string.size() -> int

Determines the length of a string in terms of the number of Unicode codepoints.

Signatures

  • string.size() -> int
  • size(string) -> int

Example

string.indexOf() -> int

Returns the index of the first occurrence of a substring within the string.
The function also accepts an optional position argument to start the search.

Signatures

  • <string>.indexOf(<string>, <int?>) -> <int>

Example

string.format() -> string

Returns a new string with substitutions being performed, printf-style. The valid formatting clauses are %s (string), %d (integer), %f (double with fixed-point precision), %e (double in scientific notation), %b (binary integer or boolean), %x (lowercase hex), %X (uppercase hex), and %o (octal).

Signatures

  • <string>.format(<list>) -> <string>

Example

string.lastIndexOf() -> int

Returns the integer index at the start of the last occurrence of the search string. If the search string is not found the function returns -1. The function also accepts an optional position which represents the last index to be considered as the beginning of the substring match. If the substring is the empty string, the index where the search starts is returned (string length or custom).

Signatures

  • <string>.lastIndexOf(<string>) -> <int>
  • <string>.lastIndexOf(<string>, <int>) -> <int>

Example

string.split() -> list

Splits a string into a list of substrings using a specified separator.
Optionally, a maximum number of splits can be defined.

Signatures

  • <string>.split(<string?>, <int?>) -> list<string>

Example

string.replace() -> string

Replaces occurrences of a substring with another string.
Optionally, limits the number of replacements.

Signatures

  • <string>.replace(<string>, <string>, <int?>) -> <string>

Example

string.decodeBase64() -> string

Decodes the Base64 string and returns it as a string.

Example

string.decodeJson() -> map | list

Decodes the JSON string and returns it as a map or list.

Example

string.decodeQueryString() -> map

Decodes the string as a URL query and returns a map with the query parameters.

Example

string.encodeBase64() -> string

Encodes the string and returns it as a base64 encoded string.

Example

string.escapeUrl() -> string

Returns the string with percent encoding applied.

Example

string.isJson() -> bool

Checks if the string is valid JSON and returns true if so, otherwise false.

Example

string.isPrivateIp() -> bool

Checks if the string is a valid private IP address falling in the range of
as per RFC 1918.
It returns true if so, otherwise false.

Example

string.isQueryString() -> bool

Checks if the string is valid Query String and returns true if so, otherwise false.

Example

string.isURL() -> bool

Checks if the string is a valid URL and returns true if so, otherwise false.

Example

string.lower() -> string

Lowercases a UTF-8 string.

Example

string.upper() -> string

Uppercases a UTF-8 string.

Example

string.parseUrl() -> URL

Returns the provided string as a net URL map structure.

Example

string.unescapeUrl() -> string

Decodes a percent-encoded string back to its original form.

Example

string.md5() -> string

MD5 is considered cryptographically broken and should not be used for security purposes. Use SHA-256 or higher for security-sensitive applications.
Computes the MD5 hash of the string.
Returns the hash in hexadecimal format by default.

Signatures

  • string.md5() -> string (returns hex)
  • string.md5(format) -> string (format: “hex” or “base64”)

Examples

string.sha1() -> string

SHA-1 is considered cryptographically weak and should not be used for security purposes. Use SHA-256 or higher for security-sensitive applications.
Computes the SHA-1 hash of the string.
Returns the hash in hexadecimal format by default.

Signatures

  • string.sha1() -> string (returns hex)
  • string.sha1(format) -> string (format: “hex” or “base64”)

Examples

string.sha256() -> string

Computes the SHA-256 hash of the string.
Returns the hash in hexadecimal format by default.

Signatures

  • string.sha256() -> string (returns hex)
  • string.sha256(format) -> string (format: “hex” or “base64”)

Examples

string.sha512() -> string

Computes the SHA-512 hash of the string.
Returns the hash in hexadecimal format by default.

Signatures

  • string.sha512() -> string (returns hex)
  • string.sha512(format) -> string (format: “hex” or “base64”)

Examples

string.crc32() -> string

Computes the CRC32 checksum of the string using the IEEE polynomial.
Returns the checksum in hexadecimal format by default.

Signatures

  • string.crc32() -> string (returns hex)
  • string.crc32(format) -> string (format: “hex” or “base64”)

Examples

string.crc64() -> string

Computes the CRC64 checksum of the string using the ISO polynomial.
Returns the checksum in hexadecimal format by default.

Signatures

  • string.crc64() -> string (returns hex)
  • string.crc64(format) -> string (format: “hex” or “base64”)

Examples

string.xxhash64() -> string

Computes the XXHash64 hash of the string.
XXHash is a fast, high-quality hash function.
Returns the hash in hexadecimal format by default.

Signatures

  • string.xxhash64() -> string (returns hex)
  • string.xxhash64(format) -> string (format: “hex” or “base64”)

Examples

string.lowerAscii() -> string

Returns a new string where all ASCII characters are lower-cased. This function does not perform Unicode case-mapping for characters outside the ASCII range.

Signatures

  • <string>.lowerAscii() -> <string>

Example

string.upperAscii() -> string

Returns a new string where all ASCII characters are upper-cased. This function does not perform Unicode case-mapping for characters outside the ASCII range.

Signatures

  • <string>.upperAscii() -> <string>

Example

string.substring() -> string

Returns the substring given a numeric range corresponding to character positions. Optionally may omit the trailing range for a substring from a given character position until the end of a string. Character offsets are 0-based with an inclusive start range and exclusive end range. It is an error to specify an end range that is lower than the start range, or for either the start or end index to be negative or exceed the string length.

Signatures

  • <string>.substring(<int>) -> <string>
  • <string>.substring(<int>, <int>) -> <string>

Example

string.trim() -> string

Returns a new string which removes the leading and trailing whitespace in the target string. The trim function uses the Unicode definition of whitespace which does not include the zero-width spaces.

Signatures

  • <string>.trim() -> <string>

Example

string.reverse() -> string

Returns a new string whose characters are the same as the target string, only formatted in reverse order.

Signatures

  • <string>.reverse() -> <string>

Example

strings.quote() -> string

Takes the given string and makes it safe to print (without any formatting due to escape sequences). If any invalid UTF-8 characters are encountered, they are replaced with \uFFFD.

Signatures

  • strings.quote(<string>) -> <string>

Example

Timestamps

ts.getDate() -> int

Extracts the day of the month (1-based indexing) from a timestamp.

Signatures

  • Timestamp.getDate() -> int (in UTC)
  • Timestamp.getDate(string) -> int (with timezone)

Example

ts.getDayOfMonth() -> int

Returns the day of the month from a timestamp, using zero-based indexing.

Signatures

  • Timestamp.getDayOfMonth() -> int (in UTC)
  • Timestamp.getDayOfMonth(string) -> int (with timezone)

Example

ts.getDayOfWeek() -> int

Returns the day of the week from a timestamp, using zero-based indexing (0 for Sunday).

Signatures

  • Timestamp.getDayOfWeek() -> int (in UTC)
  • Timestamp.getDayOfWeek(string) -> int (with timezone)

Example

ts.getDayOfYear() -> int

Returns the day of the year from a timestamp, using zero-based indexing.

Signatures

  • Timestamp.getDayOfYear() -> int (in UTC)
  • Timestamp.getDayOfYear(string) -> int (with timezone)

Example

ts.getFullYear() -> int

Returns the year from a timestamp.

Signatures

  • Timestamp.getFullYear() -> int (in UTC)
  • Timestamp.getFullYear(string) -> int (with timezone)

Example

ts.getHours() -> int

Returns the hour from a timestamp or converts a duration to hours.

Signatures

  • Timestamp.getHours() -> int (in UTC)
  • Timestamp.getHours(string) -> int (with timezone)
  • Duration.getHours() -> int (convert the duration to hours)

Example

ts.getMilliseconds() -> int

Returns the milliseconds from a timestamp or the milliseconds portion of a duration.

Signatures

  • Timestamp.getMilliseconds() -> int (in UTC)
  • Timestamp.getMilliseconds(string) -> int (with timezone)
  • Duration.getMilliseconds() -> int (extracts the milliseconds portion)

Example

ts.getMinutes() -> int

Returns the minutes from a timestamp or converts a duration to minutes.

Signatures

  • Timestamp.getMinutes() -> int (in UTC)
  • Timestamp.getMinutes(string) -> int (with timezone)
  • Duration.getMinutes() -> int (convert the duration to minutes)

Example

ts.getMonth() -> int

Returns the month from a timestamp, using zero-based indexing (0 for January).

Signatures

  • Timestamp.getMonth() -> int (in UTC)
  • Timestamp.getMonth(string) -> int (with timezone)

Example

ts.getSeconds() -> int

Returns the seconds from a timestamp or converts a duration to seconds.

Signatures

  • Timestamp.getSeconds() -> int (in UTC)
  • Timestamp.getSeconds(string) -> int (with timezone)
  • Duration.getSeconds() -> int (convert the duration to seconds)

Example

URL

url.escape(string) -> string

Returns the string with percent encoding applied.

Example

Example (in expression)

url.parse(string) -> URL

Returns the provided URL string as a net URL map structure.

Example

Example (in expression)

url.unescape(string) -> string

Decodes a percent-encoded string back to its original form.

Example

Example (in expression)

Utility

has(field) -> bool

Checks whether a field or property exists in a list or map.

Example

inCidrRange(ip,cidr) -> bool

Evaluates whether the given IP address falls within the specified CIDR range. Returns true if the IP is within the range, and false if it is outside the range or if the provided CIDR is invalid.

Example

Example (in expression)

inCidrRanges(ip,cidrs) -> bool

Checks if the given IP address falls within any of the specified CIDR ranges. Returns true if the IP is within at least one valid CIDR range, and false if it is not within any valid range.
Invalid CIDR ranges are ignored.

Example

Example (in expression)