Links
Comment on page
🚪

Token Gating

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

🚪 Token Gating

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

Table Of Contents

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:
npm
yarn
pnpm
pip
React
npm install @airstack/airstack-react
Node
npm install @airstack/node
React
yarn add @airstack/airstack-react
Node
yarn add @airstack/node
React
pnpm install @airstack/airstack-react
Node
pnpm install @airstack/node
pip install airstack
Then, add the following snippets to your code:
React
Node
Python
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>;
}
};
import { init, fetchQuery } from "@airstack/airstack-react";
init("YOUR_AIRSTACK_API_KEY");
const query = "YOUR_QUERY"; // Replace with GraphQL Query
const { data, error } = fetchQuery(query);
console.log("data:", data);
console.log("error:", error);
import asyncio
from airstack.execute_query import AirstackClient
api_client = AirstackClient(api_key="YOUR_AIRSTACK_API_KEY")
query = "YOUR_QUERY" # Replace with GraphQL Query
async def main():
execute_query_client = api_client.create_execute_query_object(
query=query)
query_response = await execute_query_client.execute_query()
print(query_response.data)
asyncio.run(main())
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.
Airstack AI (Demo)

Gating only user(s) that have Lens Profile

You can implement token gating by checking whether users have Lens profile:

Try Demo

https://app.airstack.xyz/query/TpGSzOq9tx
Show the Lens profile of bradorbradley.eth

Code

Query
Response
query GetTokenGatingLens {
Socials(
input: {
filter: {
identity: { _eq: "bradorbradley.eth" }
dappName: { _eq: lens }
}
blockchain: ethereum
}
) {
Social {
profileName
profileTokenId
profileTokenIdHex
userAssociatedAddresses
}
}
}
{
"data": {
"Socials": {
"Social": [
{
"profileName": "lens/@westlakevillage",
"profileTokenId": "99755",
"profileTokenIdHex": "0x0185ab",
"userAssociatedAddresses": [
"0x8ec94086a724cbec4d37097b8792ce99cadcd520"
]
},
{
"profileName": "lens/@brad",
"profileTokenId": "116598",
"profileTokenIdHex": "0x01c776",
"userAssociatedAddresses": [
"0x8ec94086a724cbec4d37097b8792ce99cadcd520"
]
},
{
"profileName": "lens/@bradorbradley",
"profileTokenId": "36",
"profileTokenIdHex": "0x024",
"userAssociatedAddresses": [
"0x8ec94086a724cbec4d37097b8792ce99cadcd520"
]
},
{
"profileName": "lens/@hanimourra",
"profileTokenId": "116239",
"profileTokenIdHex": "0x01c60f",
"userAssociatedAddresses": [
"0x8ec94086a724cbec4d37097b8792ce99cadcd520"
]
}
]
}
}
}
If the length of the data.Socials.Social array is 0, then it implies that the user has no Lens profile.
Otherwise, the user does and can be given access to a the desired feature.

Gating only user(s) that have Lens Profile and NFT

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

Try Demo

https://app.airstack.xyz/query/VERJn9Uhbd
Show the NFT balance of 0x8eC94086A724cbEC4D37097b8792cE99CaDCd520 on specific NFTs

Code

Query
Response
query MyQuery {
TokenBalances(
input: {
filter: {
tokenAddress: {
_in: [
"0x977e43ab3eb8c0aece1230ba187740342865ee78"
"0x9d90669665607f08005cae4a7098143f554c59ef"
]
}
owner: { _eq: "0x8eC94086A724cbEC4D37097b8792cE99CaDCd520" }
}
blockchain: ethereum
limit: 200
}
) {
TokenBalance {
owner {
socials(input: { filter: { dappName: { _eq: lens } } }) {
profileName
profileTokenId
profileTokenIdHex
userAssociatedAddresses
}
}
}
}
}
{
"data": {
"TokenBalances": {
"TokenBalance": [
{
"owner": {
"socials": [
{
"profileName": "lens/@westlakevillage",
"profileTokenId": "99755",
"profileTokenIdHex": "0x0185ab",
"userAssociatedAddresses": [
"0x8ec94086a724cbec4d37097b8792ce99cadcd520"
]
},
{
"profileName": "lens/@brad",
"profileTokenId": "116598",
"profileTokenIdHex": "0x01c776",
"userAssociatedAddresses": [
"0x8ec94086a724cbec4d37097b8792ce99cadcd520"
]
},
{
"profileName": "lens/@bradorbradley",
"profileTokenId": "36",
"profileTokenIdHex": "0x024",
"userAssociatedAddresses": [
"0x8ec94086a724cbec4d37097b8792ce99cadcd520"
]
},
{
"profileName": "lens/@hanimourra",
"profileTokenId": "116239",
"profileTokenIdHex": "0x01c60f",
"userAssociatedAddresses": [
"0x8ec94086a724cbec4d37097b8792ce99cadcd520"
]
}
]
}
}
]
}
}
}
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 Lens profile.
If owner.socials has length 0, then similarly the user has no Lens profile.
Otherwise, the user has Lens profile and can be given access to a the desired feature.

Gating only user(s) that have Lens Profile and POAP

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

Try Demo

https://app.airstack.xyz/query/izTMNkr9m9
Show if 0x4455951fa43b17bd211e0e8ae64d22fb47946ade hold some given specific POAPs and have Lens profile

Code

Query
Response
query MyQuery {
Poaps(
input: {
filter: {
eventId: { _in: ["127462", "141910"] }
owner: { _eq: "0x4455951fa43b17bd211e0e8ae64d22fb47946ade" }
}
blockchain: ALL
}
) {
Poap {
owner {
socials(input: { filter: { dappName: { _eq: lens } } }) {
profileName
profileTokenId
profileTokenIdHex
userAssociatedAddresses
}
}
}
}
}
{
"data": {
"Poaps": {
"Poap": [
{
"owner": {
"socials": [
{
"profileName": "lens/@0x131",
"profileTokenId": "73916",
"profileTokenIdHex": "0x0120bc",
"userAssociatedAddresses": [
"0x4455951fa43b17bd211e0e8ae64d22fb47946ade"
]
}
]
}
}
]
}
}
}
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 Lens profile.
If owner.socials has length 0, then similarly the user has no Lens profile.
Otherwise, the user has that Lens profile 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