đĒ ERC20Learn how to fetch ERC20 token holders of a specific contract address(es) and how to use the same query to check whether a given user holds a specific ERC20 token.
Airstack provides easy-to-use ERC20 token APIs for enriching Web3 applications with onchain and offchain ERC20 token data from Ethereum, Base, Degen Chain, and other Airstack-supported chains .
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 (data) {
return < p >Data: { JSON .stringify (data)}</ p >;
}
if (loading) {
return < p >Loading...</ p >;
}
if (error) {
return < p >Error: { error .message}</ p >;
}
};
Copy import { init , fetchQuery } from "@airstack/node" ;
init ( "YOUR_AIRSTACK_API_KEY" );
const query = `YOUR_QUERY` ; // Replace with GraphQL Query
const { data , error } = await 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.
Get ERC20 Token Holders
You can use Airstack to fetch ERC20 token holders of a given contract address(es) by using the TokenBalances
API and providing the token contract address(es) to tokenAddress
input:
Try Demo
Code
Query Response
Copy query MyQuery {
TokenBalances(
input: {
filter : {
tokenAddress : { _in : [ "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" ] }
}
blockchain : ethereum
limit : 200
}
) {
TokenBalance {
owner {
addresses
domains {
name
isPrimary
}
socials {
dappName
profileName
userAssociatedAddresses
}
xmtp {
isXMTPEnabled
}
}
}
pageInfo {
nextCursor
prevCursor
}
}
}
Copy {
"data" : {
"TokenBalances" : {
"TokenBalance" : [
{
"owner" : {
"addresses" : [
"0xdef1c0ded9bec7f1a1670819833240f027b25eff"
] ,
"domains" : [
{
"name" : "0x.merklejerk.eth" ,
"isPrimary" : false
} ,
{
"name" : "zeroex.eth" ,
"isPrimary" : false
}
] ,
"socials" : null ,
"xmtp" : null
}
} ,
// Other WETH holders
] ,
"pageInfo" : {
"nextCursor": "eyJMYXN0VmFsdWVzTWFwIjp7Il9pZCI6eyJWYWx1ZSI6IjEweGJjNGNhMGVkYTc2NDdhOGFiN2MyMDYxYzJlMTE4YTE4YTkzNmYxM2QweGFhYTJkYTI1NWRmOWVlNzRjNzA3NWJjYjZkODFmOTc5NDA5MDhhNWQxNTYiLCJEYXRhVHlwZSI6InN0cmluZyJ9LCJsYXN0VXBkYXRlZFRpbWVzdGFtcCI6eyJWYWx1ZSI6IjE3MDA0MTA5MTkiLCJEYXRhVHlwZSI6IkRhdGVUaW1lIn19LCJQYWdpbmF0aW9uRGlyZWN0aW9uIjoiTkVYVCJ9",
"prevCursor" : ""
}
}
}
]
Check If User Hold A Specific ERC20 Token
You can use Airstack to check if a user hold a given ERC20 token by using the TokenBalances
API.
For inputs, provide the token contract address(es) to tokenAddress
and user's 0x address, ENS domain, cb.id, Lens profile, or Farcaster fname/fid to owner
as an input:
Try Demo
Code
Query Response
Copy query MyQuery {
TokenBalances(
input: {
filter : {
tokenAddress : { _eq : "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" }
owner : { _eq : "0xdef1c0ded9bec7f1a1670819833240f027b25eff" }
}
blockchain : ethereum
}
) {
TokenBalance {
owner {
addresses
domains {
name
isPrimary
}
socials {
dappName
profileName
userAssociatedAddresses
}
xmtp {
isXMTPEnabled
}
}
}
}
}
Copy {
"data" : {
"TokenBalances" : {
"TokenBalance" : [
{
"owner" : {
"addresses" : [ "0xdef1c0ded9bec7f1a1670819833240f027b25eff" ] ,
"domains" : [
{
"name" : "0x.merklejerk.eth" ,
"isPrimary" : false
} ,
{
"name" : "zeroex.eth" ,
"isPrimary" : false
}
] ,
"socials" : null ,
"xmtp" : null
}
}
] ,
"pageInfo" : {
"nextCursor" : "" ,
"prevCursor" : ""
}
}
}
}
If the given user hold the specified ERC20 token, then TokenBalances
will have non-null value as a response. Otherwise, the API will return null.
Developer Support
If you have any questions or need help regarding fetching ERC20 holders data, please join our Airstack's Telegram group.
More Resources