đŸšĒToken Gating

Learn how to enable users to access certain features only if they follow a certain user or have common followers with a given user.

Airstack provides easy-to-use APIs for enriching Web3 social applications with and integrating on-chain and off-chain data with Farcaster.

Table Of Contents

In this guide you will learn how to use Airstack to:

Pre-requisites

Get Started

JavaScript/TypeScript/Python

If you are using JavaScript/TypeScript or Python, Install the Airstack SDK:

React

npm install @airstack/airstack-react

Node

npm install @airstack/node

Then, add the following snippets to your code:

import { init, useQuery } from "@airstack/airstack-react";

init("YOUR_AIRSTACK_API_KEY");

const query = `YOUR_QUERY`; // Replace with GraphQL Query

const Component = () => {
  const { data, loading, error } = useQuery(query);

  if (data) {
    return <p>Data: {JSON.stringify(data)}</p>;
  }

  if (loading) {
    return <p>Loading...</p>;
  }

  if (error) {
    return <p>Error: {error.message}</p>;
  }
};

Other Programming Languages

To access the Airstack APIs in other languages, you can use https://api.airstack.xyz/gql as your GraphQL endpoint.

Gating Only User(s) That Follows A Given User

You can build a token-gating system that gates only users that follow a given user by checking if user A is following a given user B.

This can be done by providing the user B's identity either a 0x address, ENS, cb.id, or Farcaster on the Wallet top-level query's identity input and the user A's identities in the socialFollowing.

For example, check if betashop.eth (user A) is following ipeciura.eth (user B):

Try Demo

Show me if betashop.eth is following ipeciura.eth

Code

If you need to check multiple users A simultaneously, then simply provide more identities into the identity input in the socialFollowings nested API.

query isFollowing { # Top-level is User B's Identity (ipeciura.eth)
  Wallet(input: {identity: "ipeciura.eth", blockchain: ethereum}) {
    socialFollowings( # Here is User A's Identity (betashop.eth)
      input: {filter: {identity: {_in: ["betashop.eth"]}}}
    ) {
      Following {
        dappName
        dappSlug
        followingProfileId
        followerProfileId
        followerAddress {
          addresses
          socials {
            dappName
            profileName
          }
          domains {
            name
          }
        }
      }
    }
  }
}

If betashop.eth is following ipeciura.eth on Farcaster, then it will appear as a response in the Following array as shown in the sample response and thus should be given feature access.

Otherwise, betashop.eth will be considered an not be given any feature access.

Gating Only User(s) That Have Common Followers

You can build a token-gating system that gates only users that have common followers with a given user.

This can be done by fetching the intersection of following between two or more users providing either 0x addresses, ENS names, or Farcaster profiles.

For example, check if betashop.eth (user A) has any common following ipeciura.eth (user B):

Try Demo

Show me common following of both betashop.eth and ipeciura.eth

Code

query MyQuery {
  SocialFollowings( # user A: betashop.eth
    input: {filter: {identity: {_eq: "betashop.eth"}}, blockchain: ALL, limit: 200}
  ) {
    Following {
      followingAddress {
        socialFollowings( # user B: ipeciura.eth
          input: {filter: {identity: {_eq: "ipeciura.eth"}}, limit: 200}
        ) {
          Following {
            followingAddress {
              socials {
                fnames
                profileName
                profileTokenId
                profileTokenIdHex
                userId
                userAssociatedAddresses
              }
            }
          }
        }
      }
    }
  }
}

If betashop.eth has common following ipeciura.eth on Farcaster, then it will appear as a response in the innermost Following array as shown in the sample response and thus should be given feature access.

Otherwise, betashop.eth will be considered an not be given any feature access.

Developer Support

If you have any questions or need help creating social following token gating, please join our Airstack's Telegram group.

More Resources

Last updated

Was this helpful?