How to Get NFT Owners at a Specific Block Height
Learn how to get snapshot of all the owners of an NFT collection at a particular point in time or block height
This tutorial uses the getOwnersForContract endpoint.
Ownership data for an NFT collection is quite possibly the most important piece of information stored on-chain for that particular collection. Needless to say, it is data that is heavily harvested by NFT and non-NFT projects alike.
This information can be critical if you own the project and you want to reward your owners with extra utility (for example, an airdrop). It is also extremely useful to other projects that are trying to gain traction from owners of well-established collections.
The Bored Ape Yacht Club NFT collection
It is common for projects to use ownership data from a particular height instead of the latest block. This is done to ensure that the market prices of the NFTs remain relatively stable, and long-term holders are rewarded for their loyalty and contributions.
Typically, in order to get ownership data for an NFT collection at a particular block, you would have to do the following:
- Spin up a node
- Extract historical data for the block height you’re interested in
- Call functions on the NFT contract
- Parse the output
Following these steps usually takes a significant amount of resources and time. For instance, to get the ownership data of a 10,000 PFP collection, you’d have to call the ownerOf
function 10,000 times.
Fortunately, all of this data is actually available in one API call: getOwnersForCollection.
About this Tutorial
We will write a simple script in Node to gets us all the owners of the Bored Ape Yacht Club collection at block number 15753215 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 an Alchemy app
In case you haven’t already, sign up for a free Alchemy account.
Alchemy’s account dashboard where developers can create a new app on the Ethereum blockchain.
Next, navigate to the Alchemy Dashboard and create a new app.
Make sure you set the chain to Ethereum and the network to Mainnet.
Once the app is created, click on your app’s View Key button on the dashboard.
Take note of the HTTP URL.
The URL will be in this form: https://eth-mainnet.g.alchemy.com/v2/xxxxxxxxx
You will need this later.
Step 3: Create a node project
Let’s now create an empty repository and install all node dependencies.
To make requests to the NFT API, use the Alchemy SDK.
You can also use axios
or fetch
alternatively.
This will create a repository named nft-ownership
that holds all your files and dependencies.
Next, open this repo in your favorite code editor.
We will be writing all our code in the main.js
file.
Step 4: Get owners of the NFT collection
To get all owners of a particular NFT collection, we will use the getOwnersForCollection method.
This method takes in two arguments:
contractAddress
: The address of the NFT collection (an ERC-721 or an ERC-1155 contract)block
: The point in time or block number (in hex or decimal) to fetch collection ownership information for.
Note that the block
parameter is optional. If you do not set it, the API will fetch ownership information from the latest available block.
Add the following code to the main.js
file.
Run the script using the following command:
If all goes well, you should see output that looks something like this:
Conclusion
Congratulations! You now know how to use the Alchemy NFT API to get a list of owners of an NFT collection at a particular block height.
If you enjoyed this tutorial on how to get all owners of an NFT collection, give us a tweet @Alchemy.
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 share your project with us!