⚖ī¸NFT Balances

Learn how to fetch ERC721 and ERC1155 NFT balances of user(s) on Ethereum, Base, Zora, and other Airstack-supported chains. their variations.

Airstack provides easy-to-use NFT APIs for enriching Web3 applications with onchain and offchain NFT 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

  • An Airstack account

  • 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 (data) {
    return <p>Data: {JSON.stringify(data)}</p>;
  }

  if (loading) {
    return <p>Loading...</p>;
  }

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

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 NFT Balances of User(s)

You can use Airstack to fetch NFT balance of user(s) across multiple chains, e.g. Ethereum, Base, Degen Chain, and other Airstack-supported chains, by using the TokenBalances API and providing an array of users' 0x address, ENS domain, cb.id, Lens profile, or Farcaster fname/fid to owner input:

Try Demo

Code

query MyQuery {
  Ethereum: TokenBalances(
    input: {
      filter: {
        owner: {
          _in: [
            "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
            "vitalik.eth"
            "lens/@vitalik"
            "fc_fname:vitalik"
          ]
        }
        tokenType: { _in: [ERC1155, ERC721] }
      }
      blockchain: ethereum
      limit: 50
    }
  ) {
    TokenBalance {
      owner {
        identity
      }
      amount
      tokenAddress
      tokenId
      tokenType
      tokenNfts {
        contentValue {
          image {
            extraSmall
            small
            medium
            large
            original
          }
        }
      }
    }
    pageInfo {
      nextCursor
      prevCursor
    }
  }
  Base: TokenBalances(
    input: {
      filter: {
        owner: {
          _in: [
            "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
            "vitalik.eth"
            "lens/@vitalik"
            "fc_fname:vitalik"
          ]
        }
        tokenType: { _in: [ERC1155, ERC721] }
      }
      blockchain: base
      limit: 50
    }
  ) {
    TokenBalance {
      owner {
        identity
      }
      amount
      tokenAddress
      tokenId
      tokenType
      tokenNfts {
        contentValue {
          image {
            extraSmall
            small
            medium
            large
            original
          }
        }
      }
    }
    pageInfo {
      nextCursor
      prevCursor
    }
  }
}

Get NFT Balances of User(s) on Specific Chain

You can use Airstack to fetch NFT balance of user(s) on a specific chain, e.g. Ethereum, by using the TokenBalances API.

For the inputs, specify blockchain to ethereum and provide an array of users' 0x address, ENS domain, cb.id, Lens profile, or Farcaster fname/fid to owner:

Try Demo

Code

query MyQuery {
  TokenBalances(
    input: {
      filter: {
        owner: {
          _in: [
            "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
            "vitalik.eth"
            "lens/@vitalik"
            "fc_fname:vitalik"
          ]
        }
        tokenType: { _in: [ERC1155, ERC721] }
      }
      blockchain: ethereum
      limit: 50
    }
  ) {
    TokenBalance {
      owner {
        identity
      }
      amount
      tokenAddress
      tokenId
      tokenType
      tokenNfts {
        contentValue {
          image {
            extraSmall
            small
            medium
            large
            original
          }
        }
      }
    }
    pageInfo {
      nextCursor
      prevCursor
    }
  }
}

Get Only ERC721/1155 NFT Balances of User(s)

You can use Airstack to fetch only either ERC721 or ERC1155 NFT balance of user(s) by using the TokenBalances API.

For the inputs, specify tokenType to ERC721 and provide an array of users' 0x address, ENS domain, cb.id, Lens profile, or Farcaster fname/fid to owner:

Try Demo

Code

query MyQuery {
  Ethereum: TokenBalances(
    input: {
      filter: {
        owner: {
          _in: [
            "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
            "vitalik.eth"
            "lens/@vitalik"
            "fc_fname:vitalik"
          ]
        }
        tokenType: { _in: [ERC721] }
      }
      blockchain: ethereum
      limit: 50
    }
  ) {
    TokenBalance {
      owner {
        identity
      }
      amount
      tokenAddress
      tokenId
      tokenType
      tokenNfts {
        contentValue {
          image {
            extraSmall
            small
            medium
            large
            original
          }
        }
      }
    }
    pageInfo {
      nextCursor
      prevCursor
    }
  }
  Base: TokenBalances(
    input: {
      filter: {
        owner: {
          _in: [
            "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
            "vitalik.eth"
            "lens/@vitalik"
            "fc_fname:vitalik"
          ]
        }
        tokenType: { _in: [ERC721] }
      }
      blockchain: base
      limit: 50
    }
  ) {
    TokenBalance {
      owner {
        identity
      }
      amount
      tokenAddress
      tokenId
      tokenType
      tokenNfts {
        contentValue {
          image {
            extraSmall
            small
            medium
            large
            original
          }
        }
      }
    }
    pageInfo {
      nextCursor
      prevCursor
    }
  }
}

Get NFT Balances of User(s) on Specific NFT Collection(s)

You can use Airstack to fetch NFT balance of user(s) on a specific NFT collection, e.g. Ethereum Name Service, by using the TokenBalances API.

For the inputs, specify tokenAddress with the NFT collection address and provide an array of users' 0x address, ENS domain, cb.id, Lens profile, or Farcaster fname/fid to owner:

Try Demo

Code

query MyQuery {
  TokenBalances(
    input: {
      filter: {
        tokenAddress: { _eq: "0x57f1887a8BF19b14fC0dF6Fd9B2acc9Af147eA85" }
        owner: {
          _in: [
            "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
            "vitalik.eth"
            "lens/@vitalik"
            "fc_fname:vitalik"
          ]
        }
      }
      blockchain: ethereum
    }
  ) {
    TokenBalance {
      id
      chainId
      blockchain
      tokenAddress
      tokenId
      amount
      formattedAmount
      lastUpdatedBlock
      lastUpdatedTimestamp
      tokenType
    }
  }
}

Developer Support

If you have any questions or need help regarding fetching NFT balances data, please join our Airstack's Telegram group.

More Resources

Last updated

Was this helpful?