Event Gateway expressions language

Related Documentation
Incompatible with
on-prem

In Kong Event Gateway, you can use a policy’s condition field to determine whether a certain policy should be executed.

For example, you can create a condition that selects all topics that end with the suffix my_suffix:

{"condition": "context.topic.name.endsWith('my_suffix')"}

Conditions must be between 1 and 1000 characters long.

Supported operators and expressions

For policy conditions and template strings, Kong Event Gateway supports a subset of JavaScript operators and expressions:

Operator or expression type

Operator or function

Logical operators &&, ||, !
Comparison operators ==, !=, <, <=, >, >=
Concatenation operator +
Relational operator in
String functions includes, startsWith, endsWith, substring, match

String functions

Kong Event Gateway supports the following string functions in conditional fields:

Supported fields

Depending on where an expression is authored the fields available in the context vary. In this section we state for each area how the context varies. The source of truth for these is the x-expression field in the API specification.

Variable

Type

Description

Availability

Example

context.auth.type string The type of authentication used Cluster, Produce and Consume policies context.auth.type == 'anonymous'
context.auth.principal.name string The name of the principal for this connection Cluster, Produce and Consume policies context.auth.principal.name == 'user1'
context.topic.name string The name of the topic of the record Produce and Consume policies context.topic.name == 'my-ns.my-topic'
record.headers map<string, string> The headers of the record Produce and Consume policies record.headers['skip-record'] == 'true'
record.value.content map<string, string> The value of the record. Deep fields can be accessed using json object notation Produce and Consume policies used as children of Schema Validation record.value.content['sub.other'] == 3
record.value.validated boolean Whether record validation succeeded or not Produce and Consume policies used as children of Schema Validation record.value.validated == true

Example expressions

Don’t apply a policy if a record has a x-restricted=true header and user is not admin:

context.topic.name == 'filterdemo' && record.headers['x-restricted'] == 'true' && context.auth.principal.name != 'admin'

Apply a policy only for user1 and user2:

context.auth.principal.name == 'user1' || context.auth.principal.name == 'user2'

Apply a policy only for topics that start with my-prefix:

context.topic.name.startsWith('my-prefix')

Apply a policy if a header is present regardless of the value:

'x-optional-header' in record.headers

Apply a policy if the topic is filterdemo and that the record content has a field foo equal to bar and a sub field sub.other equal to 3.

context.topic.name == 'filterdemo' && record.value.content['foo'] == 'bar' || record.value.content['sub.other'] == 3
Something wrong?

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!