December 13, 2022

Tutorial for Blockchain Developers on How to Get All Transfers of an NFT

Table of Contents

With the Web3 market’s leading NFT API and EVM API from Moralis, you can get all transfers of an NFT seamlessly. All you need to do is install the Moralis SDK and call the ”getNFTTransfers” endpoint! Basically, by adding a token address, the token ID, and the chain to the code snippet below, you cover the central code to get all transfers of an NFT:

const response = await Moralis.EvmApi.nft.getNFTTransfers({
    address,
    tokenId,
    chain,
  });

Calling the ”getNFTTransfers” endpoint returns all transfers associated with the token address and stores them in the ”response” object. From there, it is up to you to use this information creatively in your Web3 development endeavors. That said, for more detailed information on calling this endpoint, check out the documentation page on how to get all transfers of a non-fungible token. Otherwise, join us in this tutorial as we show you this works in practice! 

How to Get All Transfers of an NFT – Overview 

If you are looking to build an NFT marketplace, price estimator, analytics platform, or any other NFT-related platform, you will soon realize that you require on-chain data. A great example is the transfer records of NFTs, which you, among other things, might want to display to improve the user experience. However, querying this information has been quite cumbersome from a conventional perspective. Fortunately for you, this is no longer the case, thanks to Moralis’ NFT API. If you want to learn more about how this works, stay stuck in as we show you how to get all transfers of an NFT with Moralis!

First, let’s go back to basics and explore exactly what an NFT is. From there, we’ll explain why it is important to get the transfer history of an NFT. However, if you are already familiar with this, you can skip straight into the ”Tutorial: How to Get All Transfers of an NFT in 3 Steps” section. In that part, we illustrate how to get all transfers of an NFT in the following three straightforward steps: 

  1. Prerequisites
  2. Call Moralis’ ”getNFTTransfers” Endpoint
  3. Run the Program

What’s more, the tool you will familiarize yourself with during this tutorial is only one of Moralis’ Web3 APIs. If you are serious about getting into blockchain development, you should consider checking out other tools from Moralis. A great example is the Streams API, allowing you to stream on-chain data into your projects’ backends via Moralis webhooks!

Now, before you move further in this article, sign up with Moralis immediately, as you must have an account to follow along in the upcoming tutorial! 

What is an NFT?

Before showing you how to get all transfers of an NFT, this initial section explores the intricacies of these tokens. By doing so, you receive an overview of what they are, which helps in understanding why it is essential for developers to access information associated with NFTs. So, without further ado, let’s answer the question, ”what is an NFT?”.

nft coin coming out of computer monitor

NFTs is an abbreviation for ”non-fungible tokens”, and to understand what they are, an excellent place to start is to analyze the terminology. The term ”non-fungible” originates from traditional economics and describes goods, items, and assets with unique properties. For instance, a painting is non-fungible, as art pieces are generally one-of-a-kind. Based on this definition, NFTs are unique, one-of-a-kind cryptocurrency tokens. 

To make the description above even more understandable, let’s explore the other side of the spectrum: fungible assets. Since non-fungible assets are unique, it means that fungible assets are interchangeable. 

In the blockchain world, a clear example of a fungible asset is Bitcoin. All Bitcoin tokens are identical, suggesting that one can be swapped for another without monetary compensation as they have the same value. The same cannot be said for NFTs. Why? Because these tokens have unique properties, one might not share the same value as another. 

Since NFTs are non-fungible tokens, their uniqueness makes them ideal for representing virtually any other non-fungible assets. Consequently, this is why many people associate NFTs with digital art. However, note that NFTs generally do not ”contain” an asset like a digital art piece but rather point to a non-fungible asset in their metadata, providing proof of ownership. 

Nevertheless, with a brief introduction to NFTs, let us now explore why it is important to get the transfer histories of these tokens! 

Why is it Important to Get the Transfer History of an NFT? 

Anyone can query a blockchain network for information, such as the transaction history of an NFT. But why is it important for developers to get all transfers of an NFT? 

digital art image of a computer grid showing components such as nft blocks, smart contracts, user profiles, etc.

Let’s say you want to build a marketplace such as OpenSea, an NFT analytics platform, or an NFT price estimator. In these instances, you can highly benefit from the ability to get all transfers of an NFT. With this information, you can create a more compelling user experience (UX) by, among other things, displaying an NFT’s transaction history directly in your app’s user interface (UI). As a result, your users can avoid querying the blockchain themselves and make informed decisions based on this data. 

You might also want to use this data to calculate average prices, price developments, and much more. Accordingly, this data can be highly useful in your Web3 development endeavors. That said, it is equally important that developers have easy access to this on-chain data, which is where Moralis enters the picture! 

You can query a blockchain for on-chain NFT data in multiple ways, but it generally requires a massive amount of time and engineering resources. However, with Moralis’ NFT API and the ”getNFTTransfers” endpoint, you can get all transfers of an NFT with only a few lines of code. 

If you want to learn more about this, join us in the following section, where we provide a comprehensive tutorial on how to get all transfers of an NFT in three steps! 

Tutorial: How to Get All Transfers of an NFT in 3 Steps 

Now that you have familiarized yourself with NFTs and have a more profound understanding of why it is essential to get all transfers of an NFT, this section will show you how to acquire this data. To make this process more seamless, we use Moralis’ NFT API to make a single call to the ”getNFTTransfers” endpoint. 

powerful cross-chain nft api from moralis

All you need to do is supply a contract address, token ID, and the chain you are interested in. In turn, you receive a response if the form of an array containing all transfers associated with the specified address. You’ll also receive additional data regarding each transaction, such as the block number, transaction hash, contract type, and much more!

What’s more, through this Moralis tool, you can get all transfers of an NFT in only three steps: 

  1. Prerequisites
  2. Call Moralis’ ”getNFTTransfers” Endpoint
  3. Run the Program

By following the steps above, you can get all transfers of an NFT in no time. If this sounds exciting, join us as we kickstart this tutorial by covering the required prerequisites! 

Step 1: Prerequisites 

Before we dive into the script we use to get all transfers of an NFT, you need to deal with a few prerequisites. First, you must ensure that you have both NodeJS and npm ready. So, if you do not have them already, visit the following webpage and download the latest version of NodeJS: “https://nodejs.dev/en/download/“. 

From there, the next prerequisite is to sign up with Moralis. Setting up your account is free and only takes a few seconds! So, create your account immediately. 

However, you might wonder, ”why do I need a Moralis account?”. In order to make API calls to any of Moralis’ endpoints, including ”getNFTTransfers”, you require an API key, which is why you need to be a member of Moralis. Once you have joined, you can find your key by logging in and navigating to the ”Web3 APIs” page: 

web3 api page showing the api key

You can go ahead and copy the key immediately by clicking on the “copy” button: 

arrow pointing at the copy button for the api key

Save the key for now, as you will need it in the second step.

Nevertheless, that covers the prerequisites! Let’s jump straight into the second step, where we show you how to add the code for calling the ”getNFTTransfers” endpoint! 

Step 2: Call Moralis’ ”getNFTTransfers” Endpoint 

In this second step, we will show you the code we use to get all transfers of an NFT by calling the ”getNFTTransfers” endpoint. To begin with, open your integrated development environment (IDE) and set up a new JavaScript project. From there, create a new ”index.js” file and add the following contents:

const Moralis = require('moralis').default;
const { EvmChain } = require('@moralisweb3/common-evm-utils');

const runApp = async () => {
  await Moralis.start({
    apiKey: "YOUR_API_KEY",
    // ...and any other configuration
  });
  
  const address = '0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d';
  
  const tokenId = 1;

  const chain = EvmChain.ETHEREUM;

  const response = await Moralis.EvmApi.nft.getNFTTransfers({
    address,
    tokenId,
    chain,
  });
  
  console.log(response.toJSON());
}

runApp();

Above, you can find the complete code to get all transfers from an NFT. However, you need to make a few configurations before running this program. First, you must add your API key to the code by replacing ”YOUR_API_KEY”. Next, you need to consider the ”address”, ”tokenId”, and ”chain” variables. 

In the code snippet above, we added default values to these three variables. As such, you need to input the token address of the NFT you want to query. Moreover, you also need to change the token ID and chain to fit the token accordingly. 

The code then uses these three variables as parameters when calling the ”getNFTTransfers” endpoint. The results returned are then stored in the ”response” object, which is finally console-logged. Once you are satisfied with your configurations, all that remains is running the code. We will show you how to do so in the final step!  

Step 3: Run the Program 

In this last step of the ”How to Get All Transfers of an NFT” tutorial, we will show you how to run the code and check out what the responses look like. However, before doing so, you need to open a new terminal and install the Moralis SDK in your project using the npm command below: 

npm install moralis @moralisweb3/common-evm-utils

From there, all that remains to execute the program is running the following terminal input: 

node index.js

Once you run the command above, the program will log the results in the console. Accordingly, you should receive a response similar to the one shown below: 

{
  "total": 2,
  "page": 0,
  "page_size": 100,
  "cursor": null,
  "result": [
    {
      "block_number": "12344148",
      "block_timestamp": "2021-04-30T21:11:46.000Z",
      "block_hash": "0xa367b094366bc68de295ae6167797afc55eeb8383869363a6d7eb143c31d8274",
      "transaction_hash": "0xe93e858f9330afa4581e260198195623aa7f5cd2809012440ea291d317be9f2f",
      "transaction_index": 154,
      "log_index": 328,
      "value": "0",
      "contract_type": "ERC721",
      "transaction_type": "Single",
      "token_address": "0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d",
      "token_id": "1",
      "from_address": "0xaba7161a7fb69c88e16ed9f455ce62b791ee4d03",
      "to_address": "0x46efbaedc92067e6d60e84ed6395099723252496",
      "amount": "1",
      "verified": 1,
      "operator": null
    },

In the code snippet, we only included information regarding one of the transfers. However, as you can see from the ”total” parameter at the very top, there are two transfers in total. Moreover, every transaction is returned to you in the ”results” array, and you will receive the same information regarding all NFT transfers associated with the token address provided. 

As such, you get parsed data such as the block number, block hash, ”to” and ”from” addresses, and much more. From here, you can use this information directly in your Web3 projects to, among other things, improve the user experience! 

That covers the entirety of this tutorial. If you want more information about this process, check out the documentation page on how to get all transfers of a non-fungible token

Summary – Get All Transfers of an NFT

If you want to create NFT-related projects, you will quickly come to realize that you can highly benefit from accessing real-time on-chain data. For instance, if you want to create an NFT marketplace, you can improve the UX by displaying the transaction history of NFTs directly in your app’s UI. However, getting this information has been quite difficult from a traditional perspective. Fortunately, this is no longer the case, thanks to Web3 infrastructure providers such as Moralis!

moralis in grey letters

This tutorial taught you how to get all transfers of an NFT using Moralis. Thanks to Moralis’ NFT API and the ”getNFTTransfers” endpoint, you were able to do so in only three quick steps: 

  1. Prerequisites
  2. Call Moralis’ ”getNFTTransfers” Endpoint
  3. Run the Program

If you found this tutorial helpful, you might want to learn more about Moralis’ NFT API. If this is the case, check out our articles on how to get NFT collection data or get all NFTs from a contract! In addition, if you’re interested in storage solutions, explore metadata for NFT storage in Moralis’ NFT metadata article!

Nevertheless, no matter what on-chain NFT data you want to query, sign up with Moralis to access a more seamless developer experience. With an account, you can fully leverage the power of blockchain technology in all future development endeavors!

NFT API
Unlock the full potential of your NFT projects with this industry-leading NFT API! Fast, easy, and free.
NFT API
Related Articles
February 9, 2024

How to Query Blockchain Data for Transactions, Balances, and More 

February 6, 2023

Blockchain App Development – How to Develop Blockchain Applications

November 17, 2022

Exploring the Optimism Network – A Next-Gen L2 Blockchain for Ethereum

October 4, 2023

Polygon Node Guide: How to Run a Polygon RPC Node

August 28, 2022

How to Set Up a Self-Hosted Parse Server

November 20, 2023

What’s the Difference Between Polygon PoS vs Polygon zkEVM?