How to Get NFTs Minted by an Address
In one request, fetch all ERC-721 and ERC-1155 NFTs minted by a given Ethereum address, user or contract, over any time period.
This tutorial uses the alchemy_getAssetTransfers endpoint.
Some of the reasons why you’d want to get NFTs minted by an address include:
- Displaying all NFTs minted by a user (created and sent to their address)
- Displaying all NFTs minted by a contract (shows you how many have been created)
- Understand which NFTs were “first owned” or originally minted by a given address
Surprisingly, this type of information is not easy to gather with the current Ethereum API. That’s why Alchemy builds a higher level [Enhanced APIs (https://docs.alchemy.com/alchemy/enhanced-apis) to make building in Web3 much easier.
In this tutorial, we’ll be leveraging Alchemy’s Transfers API(alchemy_getAssetTransfers
) to fetch all NFTs minted by a particular account address.
What happens when NFTs are minted?
Under the hood, when an NFT is minted, it is transferred from the null address: 0x0000000000000000000000000000000000000000 to the user account or contract that minted it.
All token creations and destructions typically are from and to the null address respectively, so it’s a good filter when looking for mint or burns events.
This on-chain minting emits a standard transfer event since the asset is being transferred from one account to another.
Since we can filter for transfer events using the [Transfers API]ref:transfers-api), we can easily fetch NFT mints with the right parameter specification!
How to get NFT Mint Events
In order to fetch NFTs minted by a given address we’ll need to specify a few things in our alchemy_getAssetTransfers
request:
fromAddress
: where the transaction originated from, in our case that’s always 0x0000000000000000000000000000000000000000toAddress
: the address we want to see mints from (NFTs go to the address that minted them)fromBlock
: the starting time range we want to fetch NFT mints over (defaults tolatest
)toBlock
: the ending time range we want to fetch NFT mints over (defaults tolatest
)category
: the type of transfer events we care about, in our case we want to see NFTs which are ERC721 and ERC1155 events
Once we’ve specified these inputs we can send the request!
Example: Getting NFT Mint Events
To demonstrate how to get the NFT mint events from an address we’re going to walk through an example using the address 0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1 and get all NFT mint events from block 0 to latest.
For a no-code demonstration of this request, check out Alchemy’s Composer tool!
Follow along with any of the code examples below to make the request.
Alchemy SDK (Recommended)
Alchemy’s SDK allows us to more efficiently interact with Alchemy’s endpoints and make JSON-RPC requests.
NFT Mints GitHub Repo
Ensure you are inside your project folder and type the following command in the terminal:
2. Create a file for the script
In your current directory, create a new file called nft-mints-alchemy-sdk.js
Use your favorite file browser, code editor, or just directly in the terminal using the touch
command like this:
3. Write script!
Copy and paste in the following code snippet into your new file: nft-mints-alchemy-sdk.js
4. Run script!
Now, on your command line, you can run the script by calling:
Node-Fetch
If you’re using node-fetch
a lightweight, common module that brings the Fetch API to Node.js and allows us to make our HTTP requests, below is a code snippet for the request you’d make!
NFT Mints GitHub Repo
1. Create a file.
In your current directory, create a new file called nft-mints-fetch.js
using your favorite file browser, code editor, or just directly in the terminal using the touch
command like this:
2. Write script!
Copy and paste in the following code snippet into your new file: nft-mints-fetch.js
3. Run script!
Now, on your command line, you can execute the script by calling:
Axios
If you’re using Javascript axios
, a promise-based HTTP client for the browser and Node.js which allows us to make a raw request to the Alchemy API, below is a code snippet for the request you’d make!
NFT Mints GitHub Repo
1. Create a file.
In your current directory, create a new file called nft-mints-axios.js
using your favorite file browser, code editor, or just directly in the terminal using the touch
command.
2. Write script!
Copy and paste in the following code snippet into your new file: nft-mints-axios.js
3. Run script!
Now, on your command line, you can execute the script by calling:
How to process the API response
Now that we have made a query and can see the response, let’s learn how to handle the returned data.
Raw API Response:
Without parsing the response, we have a console log that looks like this:
Understanding API Response:
Below are the components of the each transfer in our response.
-
blockNum
: the block number where an NFT mint event occurred, inhex
-
hash
: the transaction hash of NFT minting transaction -
from
: where the transaction originated from, in our case that’s always 0x0000000000000000000000000000000000000000 -
to
: the address we want to see mints from (NFTs go to **** the address that minted them), should be the same address specified in our request -
value
: the amount of ETH transferred, should always benull
in our case since we’re only looking at NFT mints, not sales -
erc721TokenId
: the ERC721 token ID.null
if not an ERC721 token transfer. -
erc1155Metadata
: a list of objects containing the ERC1155tokenId
andvalue
.null
if not an ERC1155 transfer -
tokenId
: the token ID for ERC721 tokens or other NFT token standards -
asset
:ETH
or the token’s symbol.null
if not defined in the contract and not available from other sources. -
rawContract
value
:null
since we’re looking at ERC721 & ERC1155 transferaddress
: NFT contract addressdecimal
:null
Printing out the token type
, tokenId
and contract address
There’s lots of information we can pull from this response. One example you may be interested in displaying are: NFT contract standard (ERC721
or ERC1155
), contractAddress
, and tokenId
With our queried response saved as a JSON object, we can index through the transfers. In particular, we first access the transfers list and then iterate accross a few key parameters: erc1155Metadata
, tokenId
, and rawContract
.
The steps we want to take are:
-
Loop through all transfers in the result
-
Check whether the returned transfer is ERC1155 or not
- If so, loop through tokens within ERC1155
- print
tokenId
andaddress
for each
- print
- If not, assume transfer is ERC721
- print
tokenID
of contract and address
- print
- If so, loop through tokens within ERC1155
If you followed along, your response should look like the following:
And that’s it! You’ve now learned how to fetch NFT mints for an address on Ethereum! If you enjoyed this tutorial for getting NFTs minted by an address, give us a tweet @Alchemy! (Or give the author @crypt0zeke a shoutout!)
Also, join our Discord server to meet other blockchain devs, builders, and entrepreneurs!