Functions (2)

Last updated Oct 14, 2026 The Cloudflare Rules language provides functions for manipulating and validating values in an expression:

Transformation functions

The Rules language supports several functions that transform values extracted from HTTP requests. A common use case for transformation functions is the conversion of a string of characters to uppercase or lowercase, since by default, string evaluation is case-sensitive.

For example, the lower() function converts all uppercase characters in a string to lowercase.

In the expression below, the lower() function transforms http.host values to lowercase so that they match the target value "www.cloudflare.com":

lower(http.host) == "www.cloudflare.com"

Transformation functions that do not take arrays as an argument type require the [*] index notation. Refer to Arrays for more information.

The Rules language supports these transformation functions:

any

any(Array<Boolean>): Boolean

Returns true when the comparison operator in the argument returns true for any of the values in the argument array. Returns false otherwise.

Example:

any(url_decode(http.request.body.form.values[*])[*] contains "an xss attack")

all

all(Array<Boolean>): Boolean

Returns true when the comparison operator in the argument returns true for all values in the argument array. Returns false otherwise.

Example:

all(http.request.headers["content-type"][*] == "application/json")

encode_base64

encode_base64(input String | Bytes [, flags String]): String

Encodes an input string or byte array to Base64 format.

The flags parameter is optional. You can provide one or more flags as a single string. The available flags are the following:

  • u: Uses URL-safe Base64 encoding (uses - and _ instead of + and /).
  • p: Adds padding (appends = characters to make the output length a multiple of 4, as required by some systems).

By default, the output uses standard Base64 encoding without padding.

Examples:

encode_base64("hello world")          will return "aGVsbG8gd29ybGQ"
encode_base64("hello world", "p")     will return "aGVsbG8gd29ybGQ="
encode_base64("hello world", "u")     will return "aGVsbG8gd29ybGQ"
encode_base64("hello world", "up")    will return "aGVsbG8gd29ybGQ="

You can combine encode_base64() with other functions to create signed request headers:

encode_base64(sha256(concat(to_string(ip.src), http.host, "my-secret")))

Note

You can only use the encode_base64() function in request/response header transform rules.

cidr

cidr(address IP address, ipv4_network_bits Integer, ipv6_network_bits Integer): IP address

Returns the network address corresponding to an IP address (IPv4 or IPv6), given the provided IPv4 and IPv6 network bits (which determine the corresponding netmasks).

The address parameter must be a field, that is, it cannot be a literal String.

The ipv4_network_bits value must be between 1 and 32, and the ipv6_network_bits value must be between 1 and 128.

Examples:

  • If ip.src is 113.10.0.2, cidr(ip.src, 24, 24) will return 113.10.0.0.
  • If ip.src is 2001:0000:130F:0000:0000:09C0:876A:130B, cidr(ip.src, 24, 24) will return 2001:0000:0000:0000:0000:0000:0000:0000.

Note

You can only use the cidr() function in custom rules and rate limiting rules.

cidr6

cidr6(address IP address, ipv6_network_bits Integer): IP address

Returns the IPv6 network address corresponding to an IPv6 address, given the provided network bits (which determine the netmask). If you provide an IPv4 address in the first parameter, it will be returned unchanged.

The address parameter must be a field, that is, it cannot be a literal String.

The ipv6_network_bits value must be between 1 and 128.

This function is equivalent to: cidr(<address>, 32, <ipv6_network_bits>).

Examples:

  • If ip.src is 2001:0000:130F:0000:0000:09C0:876A:130B, cidr6(ip.src, 24) will return 2001:0000:0000:0000:0000:0000:0000:0000.
  • If ip.src is 113.10.0.2, cidr6(ip.src, 24) will return 113.10.0.2 (unchanged).

Note

You can only use the cidr6() function in custom rules and rate limiting rules.

coalesce

coalesce(value1 T, value2 T [, ...]): T

Returns the first argument that is not nil. It checks arguments from left to right. If all arguments are nil, the function returns nil. Provide at least two arguments of the same data type.

For comparison behavior involving nil, refer to Missing values.

The following expression compares the request path with a query parameter. If expected_path is missing, coalesce() uses / instead:

http.request.uri.path eq coalesce(http.request.uri.args["expected_path"][0], "/")

concat

concat(String | Bytes | Array): String | Array

Takes a comma-separated list of values. Concatenates the argument values into a single String or array.

The return type depends on the type of input arguments. For example, if you concatenate arrays, the function will return an array.

For example, concat("String1", " ", "String", "2") will return "String1 String2".

decode_base64

decode_base64(source String): String

Decodes a Base64-encoded String specified in source.

source must be a field, that is, it cannot be a literal String.

For example, with the following HTTP request header: client_id: MTIzYWJj, (any(decode_base64(http.request.headers["client_id"][*])[*] eq "123abc")) would return true.

Note

You can only use the decode_base64() function in Transform Rules, custom rules, and rate limiting rules.

ends_with

ends_with(source String, substring String): Boolean

Returns true when the source ends with a given substring. Returns false otherwise. The source cannot be a literal value (like "foo").

For example, if http.request.uri.path is "/welcome.html", then ends_with(http.request.uri.path, ".html") will return true.

hash_in_range

hash_in_range(min Integer, max Integer, seed Integer | Bytes | IP address [, ...]): Integer

Hashes seed fields into an integer from min to max. The range includes min but excludes max. Both bounds must be integer literals, with max greater than min. Seeds must be fields, not literal values.

The function ignores missing (nil) seed values. If all seed values are missing, it returns nil. If every seed might be missing, use coalesce() for a fallback.

All HTTP products support this function on all plans.

The following expression selects approximately 10% of requests at random:

hash_in_range(0, 100, cf.random_seed) < 10

The cf.random_seed field provides random bytes for every request. Because the field is always present, this example needs no fallback.

With Cloudflare for SaaS, use custom metadata to control rollout progression. Define rollout_pct as a custom key for each hostname. Set its value to an integer from 0 to 100. The expression selects approximately that percentage of requests:

hash_in_range(0, 100, cf.random_seed) < coalesce(lookup_json_integer(cf.hostname.metadata, "rollout_pct"), 0)

If rollout_pct is missing, coalesce() supplies 0. The rule then matches no requests.

Hash algorithm

The hash algorithm may change at any time without prior notice. Do not rely on a specific result for a fixed seed. Use cf.random_seed to get fresh random bytes for each request.

join

join(items Array<String>, separator String): String

Returns a string which is the concatenation of the strings in items with the separator between each item.

If any of the arguments is nil, the returned value will be nil.
If the items array is empty, the returned value will be an empty string.
If the items array contains a single item, then no concatenation occurs and the (single) item will be returned as is.

This function does the opposite of the split() function.

Example:

# Joins all HTTP request header names into a single string, with names separated by commas
join(http.request.headers.names, ",")

Note

The join() function is only available in Transform Rules, custom rules, and Custom Error Rules.

has_key

has_key(map: Map<T>, key: String): Boolean

Returns true if the key specified in the second argument, which can be a literal or a dynamic string, is an existing key in the map provided as first argument; returns false otherwise.

The data type of the values in map (indicated by T) can be any type.

If any of the arguments is nil, the returned value will be nil.

Examples:

# Check if an HTTP request header exists:
has_key(http.request.headers, "x-my-header")

# Check if a request header exists based on the name of the first query argument:
has_key(http.request.headers, lower(http.request.uri.args.names[0]))

has_value

has_value(collection: Map<T> | Array<T>, value: T): Boolean

Returns true if the value specified in the second argument, which can be a literal or a dynamic value, is found in the collection provided as first argument; returns false otherwise.

The data type of the values in the collection (indicated by T) must match the data type of the provided value. Additionally, T must be a primitive data type, that is, it must be one of Boolean, Integer, String, Bytes, or IP address.

If any of the arguments is nil, the returned value will be nil.

Examples:

# Check if there is an HTTP request header with the exact name 'X-My-Header'
has_value(http.request.headers.names, "X-My-Header")

# Check if there is a request header with the exact name provided as the first query argument:
has_value(http.request.headers.names, http.request.uri.args.names[0])

is_jwt_present

is_jwt_present(token_configuration_id: String): Boolean

Returns true if the request has a token as configured in the token configuration with the ID token_configuration_id.

token_configuration_id must be the ID of an existing token configuration.

Example:

is_jwt_present("51231d16-01f1-48e3-93f8-91c99e81288e")

Note

This function is only available in API JWT validation rules.

is_jwt_valid

is_jwt_valid(token_configuration_id: String): Boolean

Returns true if the request has a valid token according to the token configuration with the ID token_configuration_id.

token_configuration_id must be the ID of an existing token configuration. The function returns false if the token is missing from the request.

is_jwt_valid("51231d16-01f1-48e3-93f8-91c99e81288e")

Note

This function is only available in API JWT validation rules.

len

len(String | Bytes | Array): Integer

Returns the byte length of a String or Bytes value, or the number of elements in an array.

For example, if the value of http.host is "example.com", then len(http.host) will return 11.

lookup_json_integer

lookup_json_integer(field String, key String | Integer, key String | Integer optional, ...): Integer

Returns the integer value associated with the supplied key in field.

The field must be a string representation of a valid JSON document.

The key can be an attribute name, a zero-based position number in a JSON array, or a combination of these two options (as extra function parameters), while following the hierarchy of the JSON document to obtain a specific integer value.

Note: This function only works for plain integers. For example, it will not work for floating numbers with a zero decimal part such as 42.0.

Examples:

  • Given the following JSON object contained in the http.request.body.raw field:
    { "record_id": "aed53a", "version": 2 }
    Then lookup_json_integer(http.request.body.raw, "version") will return 2.
  • Given the following nested object:
    { "product": { "id": 356 } }
    Then lookup_json_integer(http.request.body.raw, "product", "id") will return 356.
  • Given the following JSON array at the root level:
    ["first_item", -234]
    Then lookup_json_integer(http.request.body.raw, 1) will return -234.
  • Given the following array in a JSON object attribute:
    { "network_ids": [123, 456] }
    Then lookup_json_integer(http.request.body.raw, "network_ids", 0) will return 123.
  • Given the following root-level array of JSON objects:
    [{ "product_id": 123 }, { "product_id": 456 }]
    Then lookup_json_integer(http.request.body.raw, 1, "product_id") will return 456.