Keyri Low-Code Rule Builder

The Keyri Low-Code Rule Builder enables you to quickly create custom rules of any complexity to suit your application by harnessing most of the data that Keyri collects and calculates. Rulesets are written as JSON objects using a syntax similar to MongoDB queries. Please reference this page for a full list of available conditions that you can evaluate in your rules and our examples for examples of useful custom rules. Please also see this page for a real-life example of the JSON object that the Keyri rules engine evaluates: Keyri Rules Engine JSON.

Logical Operators

$and

Explicitly define a set of conditions that must all be true. Use an array to list these conditions.

  • Example: Block users who are both on a new device and VPN:
{
  "rule": "New Device and VPN - deny",
  "conditions": {
    "$and": [
      {
        "instanceDeltas.deviceUsers.changed": {
          "$eq": "true"
        }
      },
      {
        "threat.is_vpn": {
          "$eq": "true"
        }
      }
    ]
  },
  "outcome": "deny",
  "signal": "new_device_and_vpn",
  "enabled": true,
  "strength": 2
}

$or

Define a set of conditions where at least one must be true. Like $and, it uses an array.

  • Example: Warn if users are either on a new device or VPN:
{
  "rule": "New Device or VPN - warn",
  "conditions": {
    "$or": [
      {
        "instanceDeltas.deviceUsers.changed": {
          "$eq": "true"
        }
      },
      {
        "threat.is_vpn": {
          "$eq": "true"
        }
      }
    ]
  },
  "outcome": "warn",
  "signal": "new_device_and_vpn",
  "enabled": true,
  "strength": 2
}

Comparison Operators

$gte (Greater Than or Equal)

Checks if the value is greater than or equal to the specified number.

  • Example: {"age": {"$gte": 18}} - true if age is 18 or more.

$gt (Greater Than)

Checks if the value is strictly greater than the specified number.

  • Example: {"age": {"$gt": 18}} - true if age is more than 18.

$lte (Less Than or Equal)

Checks if the value is less than or equal to the specified number.

  • Example: {"age": {"$lte": 18}} - true if age is 18 or less.

$lt (Less Than)

Checks if the value is strictly less than the specified number.

  • Example: {"age": {"$lt": 18}} - true if age is less than 18.

$eq (Equal)

Checks if the value is equal to the specified value.

  • Example: {"name": {"$eq": "John"}} - true if name is "John".
  • Example: {"active": {"$eq": true}} - true if active is true.

$ne (Not Equal)

Checks if the value is not equal to the specified value.

  • Example: {"name": {"$ne": "John"}} - true if name is not "John".

$in (In Array)

Checks if the value is in the specified array.

  • Example: {"color": {"$in": ["red", "blue"]}} - true if color is either "red" or "blue".

$nin (Not In Array)

Checks if the value is not in the specified array.

  • Example: {"color": {"$nin": ["red", "blue"]}} - true if color is neither "red" nor "blue".

$not

Applies a logical NOT to the specified condition.

  • Example: {"status": {"$not": "inactive"}} - true if status is not "inactive".

Dynamic Key Evaluation

This feature allows using the values of the data object as keys in the logic object.

Say you want the user's current country to have been used at least 25% of the time by the user. Since you can't know the user's country of origin at the first session, you can craft a rule like this:

  • Example:
    {
      "userModel.country_code.${ipGeoData.country_code}.percent": {
        "$gte": 0.25
      }
    }
    Here, because ipGeoData.country_code is say "US", the data-object you're looking up becomes userModel.country_code.US.percent, which is what you want.

Mathematical Operations

$sum

Calculates the sum of specified values in an array.

  • Example: Block if the sum of money spent in the last 24 hours is greater than $2,500:
{
  "rule": "24-hour spend threshold - $2500",
  "conditions": {
    "$and": [
      {
        "2500": {
          "$lte": {
            "$sum": [
              "userModel.metadata.amount._sum_24_hours",
              "eventMetadata.amount"
            ]
          }
        }
      },
      {
        "eventMetadata.amount": {
          "$gt": 0
        }
      }
    ]
  },
  "outcome": "deny",
  "signal": "24-hour_spend_threshold_exceeded",
  "enabled": true,
  "strength": 2
}

Here, we're saying if the sum of money spent in the last 24 hours and the current transaction amount is Greater Than or Equal to $2,500, evaluate to true

Other Arithmetic Operations

  • $add: Adds two values.
  • $subtract: Subtracts the second value from the first.
  • $multiply: Multiplies two values.
  • $divide: Divides the first value by the second.