đŸšĒToken Gating

Learn how to enable users to access certain features only if they have a Farcaster account or a combination of Farcaster + other criteria such as a specific POAP or NFT.

Airstack provides easy-to-use APIs for enriching Farcaster applications and for integrating onchain and offchain data with Farcaster.

In this tutorial, you will learn how to build token gating systems in your dapp that grant feature access to Farcaster user(s) based on specific onchain criteria.

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

Pre-requisites

  • An Airstack account (free)

  • Basic knowledge of GraphQL

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 (loading) {
    return <p>Loading...</p>;
  }

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

  // Render your component using the data returned by the query
};

Other Programming Languages

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

🤖 AI Natural Language​

Airstack provides an AI solution for you to build GraphQL queries to fulfill your use case easily. You can find the AI prompt of each query in the demo's caption or title for yourself to try.

Gating only user(s) that have Farcaster

You can implement token gating by checking whether users have Farcaster:

Try Demo

Show Farcaster name and ID of dwr.eth, 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045, and jayden.lens

Code

query GetTokenGatingFarcasters {
  Socials(
    input: {filter: {identity: {_in: ["dwr.eth", "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045", "jayden.lens"]}, dappName: {_eq: farcaster}}, blockchain: ethereum}
  ) {
    Social {
      profileName
      userId
      userAssociatedAddresses
    }
  }
}

If the length of the data.Socials.Social array is 0, then it implies that the user has no Farcaster.

Otherwise, the user does and can be given access to a the desired feature.

Gating only user(s) that have Farcaster and NFT

You can implement token gating by checking whether users have both Farcaster and the given NFT:

Try Demo

Show NFT balance of 0xfaba1e9ed7f667e8c7a851c9ed15aed99aa80289 on specific NFTs

Code

query MyQuery {
  TokenBalances(
    input: {filter: {tokenAddress: {_in: ["0xBC4CA0EdA7647A8aB7C2061c2E118A18a936f13D", "0x23581767a106ae21c074b2276D25e5C3e136a68b"]}}, blockchain: ethereum, limit: 200}
  ) {
    TokenBalance {
      owner {
        socials(input: {filter: {dappName: {_eq: farcaster}}}) {
          userId
          profileName
        }
      }
    }
  }
}

If the length of the data.TokenBalances.TokenBalance array is 0, then it implies that the user has no given NFT held.

Otherwise, the user have at least one of the given NFT and then can have the owner.socials to be checked further to confirm if the user has any Farcaster.

If owner.socials has length 0, then similarly the user has no Farcaster.

Otherwise, the user has Farcaster and can be given access to a the desired feature.

Gating only user(s) that have Farcaster and POAP

You can implement token gating by checking whether users have both Farcaster and the given POAP:

Try Demo

Show if 0x4455951fa43b17bd211e0e8ae64d22fb47946ade hold some given specific POAPs

Code

query MyQuery {
  Poaps(
    input: {filter: {eventId: {_in: ["127462", "141910"]}, owner: {_eq: "0x4455951fa43b17bd211e0e8ae64d22fb47946ade"}}, blockchain: ALL}
  ) {
    Poap {
      owner {
        socials(input: {filter: {dappName: {_eq: farcaster}}}) {
          profileName
          userId
          userAssociatedAddresses
        }
      }
    }
  }
}

If the length of the data.Poaps.Poap array is 0, then it implies that the user has no given POAP held.

Otherwise, the user have at least one of the given POAP and then can have the owner.socials to be checked further to confirm if the user has any Farcaster.

If owner.socials has length 0, then similarly the user has no Farcaster.

Otherwise, the user has Farcaster and can be given access to a the desired feature.

Developer Support

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

More Resources

Last updated

#300: add-user-details

Change request updated