How to Get All NFTs Owned by an Address
Learn how to get all NFTs (and their metadata) owned by an address using the Alchemy NFT API.
This tutorial uses the getNFTs endpoint.
If you’re building a wallet, an NFT marketplace like OpenSea, or a Building a dApp with Real-Time Transaction Notifications, chances are that you will need to list all NFTs owned by a user as well as information about the NFTs themselves (such as name, description, traits, etc.)
NFT gallery showing multiple NFTs including Degen Apes, Bored Ape Yacht Club (BAYC), Mekaverse, and Degen Trash Pandas.
To get all NFTs owned by a user on a blockchain like Ethereum or Polygon, you would have to parse the entire blockchain since genesis, track all ERC-721
and ERC-1155
contracts, and track the latest ownership status of every NFT.
This requires a massive amount of engineering resources and time.
However, it is possible to bypass this effort by using Alchemy’s NFT API.
In this tutorial, you will use the Alchemy NFT API to get all NFTs owned by a wallet address.
If you are new, read the NFT API Quickstart Guide to learn how the NFT API works.
About This Tutorial
You will write a simple script in Node.js
to get the top 100 NFTs owned by a wallet on Ethereum.
This will be achieved using a free Alchemy developer account and the NFT API.
Creating the NFT Ownership Script
Step 1: Install Node and npm
In case you haven’t already, install node and npm on your local machine.
Make sure that node is at least v14 or higher by typing the following in your terminal:
Step 2: Create a new Alchemy app
In case you haven’t already, sign up for a free Alchemy account.
Alchemy dashboard where NFT developers can easily create a new app on a specific blockchain and network.
Next, navigate to the Alchemy Dashboard and create a new app.
Make sure you set the chain to Ethereum and network to Mainnet.
Once the app is created:
- click on your app’s View Key button on the dashboard
- take a note of the API Key (e.g.
54gYyVG14f........................
)
You will need this API Key later.
Step 3: Create a Node project
Next, create an empty repository and install all node dependencies.
To make requests to the NFT API, we recommend using the Alchemy API Overview
Alternatively, you can use axios
or fetch
.
This will create a repository named nft-ownership
that holds all the files and dependencies.
Open this repo in your favorite code editor.
You will be writing all your code in the main.js
file.
Step 4: Get all NFTs owned by an address
To get all NFTs owned by an address, use the getNFTsForOwner
method.
The getNFTsForOwner method takes in one required argument:
owner
: The wallet address of which we want to get NFT ownership data.
By default, the getNFTsForOwner
method returns metadata of the NFTs.
We are going to stick with this default behavior.
You can also restrict the NFT collections by passing in an array of contractAddresses[]
and filtering out NFTs that have been classified as spam using the filter
argument.
For more information on this, check out the NFT API FAQ. Add the following code to the main.js
file:
Run this script by typing:
You should then see an output that looks something like this:
You get all the metadata about the NFTs in the same request.
This data can be parsed and displayed on your web3 frontend with ease.
If the address owns more than 100 NFTs, the response will contain a pageKey
key-value pair.
Use this key to issue another request to getNFTs
with pageKey
as another argument to owner
.
This will return the next 100 NFTs owned by the user.
You can repeat this process until you reach the end of the wallet’s collection.
For more information, check out the NFT API docs.
Step 5: Parse the NFT API output
The response you received in the previous step is not human-readable. Let’s parse the API output to show only the titles of the NFTs owned by a wallet.
Replace the contents of main.js
with the following:
Run the script using:
You should obtain an output that looks like this:
Conclusion
Congratulations! You now know how to use the Alchemy NFT API to get all NFTs owned by a user on a blockchain (whether that be Ethereum or Polygon).
If you enjoyed this tutorial on how to get all NFTs owned by an address, tweet us at @AlchemyPlatform and give the authors @rounak_banik and @ankg404 a shoutout!
Don’t forget to join our Discord server to meet other blockchain devs, builders, and entrepreneurs!
Ready to start using the Alchemy NFT API?
Create a free Alchemy account and do share your project with us!