๐ช Token GatingLearn 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
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
Copy npm install @airstack/airstack-react
Node
Copy npm install @airstack/node
React
Copy yarn add @airstack/airstack-react
Node
Copy yarn add @airstack/node
React
Copy pnpm install @airstack/airstack-react
Node
Copy pnpm install @airstack/node
Then, add the following snippets to your code:
React Node Python
Copy 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 >;
}
};
Copy 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);
Copy 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.
Gating only user(s) that have Lens Profile
You can implement token gating by checking whether users have Lens profile:
Try Demo
Code
Query Response
Copy query GetTokenGatingLens {
Socials(
input: {
filter : {
identity : { _eq : "bradorbradley.eth" }
dappName : { _eq : lens }
}
blockchain : ethereum
}
) {
Social {
profileName
profileTokenId
profileTokenIdHex
userAssociatedAddresses
}
}
}
Copy {
"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
Code
Query Response
Copy 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
}
}
}
}
}
Copy {
"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
Code
Query Response
Copy 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
}
}
}
}
}
Copy {
"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