âī¸ ERC20 BalancesLearn how to get ERC20 token balances of user(s) on Ethereum, Base, Zora, and other Airstack-supported chains.
Airstack provides easy-to-use APIs for enriching Web3 applications and integrating ERC20 token balance 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 get ERC20 tokens owned by user(s) .
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.
đ¤ 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.
Get ERC20 Tokens Owned By User(s)
You can fetch all ERC20 tokens owned by any user(s) across multiple chains, e.g. Ethereum, Base, Degen Chain, and other Airstack-supported chains :
Try Demo
Code
Query Response
Copy query MyQuery {
TokenBalances(
input: {
filter : {
owner : {
_in : [
"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
"vitalik.eth"
"lens/@vitalik"
"fc_fname:vitalik"
]
}
tokenType : { _eq : ERC20 }
}
blockchain : ethereum
limit : 50
}
) {
TokenBalance {
owner {
addresses
domains {
name
isPrimary
}
socials {
profileName
profileTokenId
profileTokenIdHex
userAssociatedAddresses
}
xmtp {
isXMTPEnabled
}
}
amount
tokenAddress
token {
name
symbol
}
}
pageInfo {
nextCursor
prevCursor
}
}
}
Copy {
"data" : {
"TokenBalances" : {
"TokenBalance" : [
{
"owner" : {
"addresses" : [
"0xd8da6bf26964af9d7eed9e03e53415d37aa96045"
] ,
"domains" : [
{
"name" : "quantumexchange.eth" ,
"isPrimary" : false
} ,
// Other ENS domains
]
"socials" : [
{
"profileName" : "vitalik.eth" ,
"profileTokenId" : "5650" ,
"profileTokenIdHex" : "0x1612" ,
"userAssociatedAddresses" : [
"0xadd746be46ff36f10c81d6e3ba282537f4c68077" ,
"0xd8da6bf26964af9d7eed9e03e53415d37aa96045"
]
} ,
{
"profileName" : "lens/@vitalik" ,
"profileTokenId" : "100275" ,
"profileTokenIdHex" : "0x0187b3" ,
"userAssociatedAddresses" : [
"0xd8da6bf26964af9d7eed9e03e53415d37aa96045"
]
}
] ,
"xmtp" : [
{
"isXMTPEnabled" : true
}
]
} ,
"amount" : "45934484403886362668" ,
"tokenAddress" : "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2" ,
"token" : {
"name" : "Wrapped Ether" ,
"symbol" : "WETH"
}
}
// Other Ethereum ERC20s
] ,
"pageInfo" : {
"nextCursor": "eyJMYXN0VmFsdWVzTWFwIjp7Il9pZCI6eyJWYWx1ZSI6ImVmYmMyM2UwZGZkYmFiY2Y0MjFjNzRmNmE5ODlkMWNhMjdhMTJlYjRjZWUyNmM5NmViNzZhMzZhMTk3MzA0ZjUiLCJEYXRhVHlwZSI6InN0cmluZyJ9LCJsYXN0VXBkYXRlZFRpbWVzdGFtcCI6eyJWYWx1ZSI6IjE2OTE0Mjk2NjIiLCJEYXRhVHlwZSI6IkRhdGVUaW1lIn19LCJQYWdpbmF0aW9uRGlyZWN0aW9uIjoiTkVYVCJ9",
"prevCursor" : ""
}
}
}
}
Developer Support
If you have any questions or need help regarding fetching token balances of user(s), please join our Airstack's Telegram group.
More Resources