How to Get all NFT Transactions by an Address
Learn how to fetch all ERC-721 and ERC-1155 NFTs transferred by a given Ethereum address over any time period.
This tutorial uses the alchemy_getAssetTransfers endpoint.
A few quick reasons why you’d want to get NFT transfer history by an address:
- Building an NFT activity page for all transfers/sales.
- Calculating the volume of NFTs traded by an address.
While this type of data cannot be easily queried via the Ethereum API Quickstart um API], Alchemy builds higher-level Enhanced APIs Overview to make Web3 interactions much easier.
In this tutorial, we’ll be leveraging Alchemy’s Transfers API(alchemy_getAssetTransfers
) to query all NFT transfers by an address.
What happens when NFTs are transacted?
Behind the scenes, whenever an NFT undergoes an on-chain sale or swap, any associated smart contract calls will emit a standard transfer event since the asset is ultimately being transferred from one account to another.
Since we can specifically filter for transfer events using the Transfers API, we can easily fetch NFT transactions with the right combination of filter parameters!
How to get NFT Transaction History
In order to fetch NFT transaction history by a given address, we’ll need to specify a few things in our alchemy_getAssetTransfers
request:
fromAddress
: where the NFT transaction originated from when fetching NFT transaction history originatingfrom
an address we use thistoAddress
: the NFT recipient’s address when fetching NFT transaction history by recipient address we use thisfromBlock
: the starting time range we want to fetch NFT transactions over (defaults tolatest
)toBlock
: the ending time range we want to fetch NFT transactions 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: How to get NFT Transaction History Originating From
An Address
To demonstrate how to get the NFT transaction history from an address we’re going to walk through an example using the address 0x5c43B1eD97e52d009611D89b74fA829FE4ac56b1 and getting all NFT transaction events from it spanning block 0 to the 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.
Ensure you are inside your project folder and type the following command in the terminal:
1. Create a file.
In your current directory, create a new file called nft-tx-history-from-alchemy-sdk.js
Use your favorite file browser, code editor, or just directly in the terminal using the touch
command like this:
2. Write script!
Copy and paste the following code snippet into your new file: nft-tx-history-from-alchemy-sdk.js
3. Run script!
Now, on your command line, you can execute 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, here’s a code snippet for the request you’d make!
1. Create a file.
In your current directory, create a new file called nft-tx-history-from-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 the following code snippet into your new file: nft-tx-history-from-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, here’s a code snipper for the request you’d make!
1. Create a file.
In your current directory, create a new file called nft-tx-history-from-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 the following code snippet into your new file: nft-tx-history-from-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 command-line print-out that looks like this:
Understanding API Response:
Below are each of the components in our response.
-
blockNum
: the block number where an NFT transaction occurred, inhex
-
hash
: the transaction hash of NFT transaction -
from
: where the transaction originated from -
to
: where the NFT was received -
value
: the amount of ETH transferred, should always benull
in our case since we’re only looking at NFT transfer events which typically only transfer the NFT and not ETH -
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 across 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 thus far, your response should look like the following:
And that’s it! You’ve now learned how to fetch NFT transaction history given an address on Ethereum! If you enjoyed this tutorial, give us a tweet @Alchemy! (Or give the author @crypt0zeke some love!)
Also, join our Discord server to meet other blockchain devs, builders, and entrepreneurs!