βš™οΈAdvanced Filter Patterns

Learn various filter combinations to build both simple and complex filter for only listening to certain Farcaster events.

Subscription filtering is used to decide what events an endpoint will receive based on the webhook event payload. The subscription filter is an enriched JSON syntax for both simple and complex filters (such as special logical and arithmetic operators $or, $gte, $eq).

​Simple filters

Simple filters directly match keys to values, and they can be nested. They can also match items in an array.

​Direct object match

This filter is used to validate simple JSON webhook payloads.

{
  "userId": "602"
}

​Nested object match

This filter is used to validate nested webhook payloads.

Airstack webhooks filter only accept nested filters up to 3 levels. More than 3-level nesting will result in error.

{
  "socialCapital": {
    "socialCapitalRank": 1
  }
}

​Array contains match

This filter is used to validate if there is any match in an array field in the webhook payloads.

{
  "userAssociatedAddresses": "0xa3a736379bfb4a9748c3e4daf5f9af0edfcc6c1a"
}

​Complex filters

Complex filters contain more logic such as logical operators and special operators. Complex filters are employed to filter events using one or more conditions, e.g., $or logical operator filter.

​$neq

This filter matches event which directly does not match the event type in the webhook payload.

{
  "userId": {
    "$neq": "1"
  }
}

This filter is used to match multiple conditions evaluated using OR logic as defined in the schema.

{
  "$or": [
    {
      "userId": "602"
    },
    {
      "userAddress": "0xa3a736379bfb4a9748c3e4daf5f9af0edfcc6c1a"
    }
  ]
}

$and

This filter is used to match multiple conditions evaluated using AND logic as defined in the schema. This can also be combined with other operators such as $or.

{
  "$and": [
    {
      "$.socialCapital.$.socialCapitalRank": {
        "$lte": 100
      }
    },
    {
      "$or": [
        {
          "userId": "602"
        },
        {
          "userAddress": "0xa3a736379bfb4a9748c3e4daf5f9af0edfcc6c1a"
        }
      ]
    }
  ]
}

​Array Contains filters

This filter is used to match keys where the value can be a range of values. It can be used in place of $or if you are comparing on the same field. The opposite of this operator $nin can be used to match keys where the value is not in a range of values.

{
  "userId": {
    "$in": ["602", "2602"]
  }
}

​Arithmetic filters

These filters match events based on arithmetic operators. For example, the filter below will match all events where the age is greater than 1. The operators supported are $gt, $gte, $lt, $lte

{
  "socialCapital": {
    "socialCapitalRank": {
      "$lte": 100
    }
  }
}

​Array positional filters

These filters match events with payloads that are array either in the root or nested.

{
  "$.socialCapital.$.socialCapitalRank": {
    "$lte": 100
  }
}

​Regex filters

These filters match events with payloads that match the supplied regex.

{
  "profileName": {
    "$regex": "^betashop"
  }
}

Developer Support

If you have any questions or need help regarding defining custom filters for your use cases, please join our Airstack's Telegram group.

More Resources

Last updated