How to get all NFT transfers in a block
Prerequisitesβ
Before getting started, make sure you have the following ready:
- Node v.14+ or Python
- NPM/Yarn or Pip
Step 1: Setup Moralisβ
First register your Moralis account and get your Moralis API Key.
Once you have your Moralis API Key, install the Moralis SDK in your project.
- npm
- yarn
- pnpm
- pip
npm install moralis @moralisweb3/common-evm-utils
yarn add moralis @moralisweb3/common-evm-utils
pnpm add moralis @moralisweb3/common-evm-utils
pip install moralis
Step 2: Get All NFTs Transfers in a Blockβ
In order to get all the NFT transfers in a block, Moralis provides you with a getNFTTransfersByBlock
endpoint to do so.
Here you'll need two parameters: blockNumberOrHash
and chain
.
Once you've obtained both the blockNumberOrHash
and chain
, you can copy the following code:
- index.js (JavaScript)
- index.ts (TypeScript)
- index.py (Python)
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 blockNumberOrHash = "15846571";
const chain = EvmChain.ETHEREUM;
const response = await Moralis.EvmApi.nft.getNFTTransfersByBlock({
blockNumberOrHash,
chain,
});
console.log(response.toJSON());
};
runApp();
import Moralis from "moralis";
import { EvmChain } from "@moralisweb3/common-evm-utils";
const runApp = async () => {
await Moralis.start({
apiKey: "YOUR_API_KEY",
// ...and any other configuration
});
const blockNumberOrHash = "15846571";
const chain = EvmChain.ETHEREUM;
const response = await Moralis.EvmApi.nft.getNFTTransfersByBlock({
blockNumberOrHash,
chain,
});
console.log(response.toJSON());
};
runApp();
from moralis import evm_api
api_key = "YOUR_API_KEY"
params = {
"block_number_or_hash": "15846571",
"chain": "eth",
}
result = evm_api.nft.get_nft_transfers_by_block(
api_key=api_key,
params=params,
)
print(result)
Step 3: Run the scriptβ
To run the script, enter the following command:
- Shell (JavaScript)
- Shell (TypeScript)
- Shell (Python)
node index.js
ts-node index.ts
python index.py
In your terminal, you should see the following JSON response:
{
"total": 111,
"page": 0,
"page_size": 100,
"cursor": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJvcmRlciI6IkRFU0MiLCJvZmZzZXQiOjAsImxpbWl0IjoxMDAsImJsb2NrX251bWJlciI6IjE1ODQ2NTcxIiwid2hlcmUiOnt9LCJwYWdlIjoxLCJrZXkiOiIxNTg0NjU3MS4xMDguMTM2LjAiLCJ0b3RhbCI6MTExLCJpYXQiOjE2NjY5Njc1NDN9.N9rkTDnDuHCgEZk2x2QhycI_CwgyKqSiCG0KuacEoAc",
"result": [
{
"block_number": "15846571",
"block_timestamp": "2022-10-28T12:30:47.000Z",
"block_hash": "0x5bdb81ef57386daa6ad3b9893216db9b15bb5d88f9c5524fabb106eace4c69c6",
"transaction_hash": "0x1cf0a4282ca892210a42ee14633d9dffcb644d198a5a7751af1046568c59beb9",
"transaction_index": 156,
"log_index": 307,
"value": "0",
"contract_type": "ERC1155",
"transaction_type": "Single",
"token_address": "0x03a980c83bb58838298ea7689d9d4d22aad51599",
"token_id": "0",
"from_address": "0x47da053384abd2ee2bf4883927e7e1dcc3f5f95b",
"to_address": "0x981ff49a2007a095eca50458159525e65e7b1d0c",
"amount": "1",
"verified": 1,
"operator": "0x47da053384abd2ee2bf4883927e7e1dcc3f5f95b"
},
{
"block_number": "15846571",
"block_timestamp": "2022-10-28T12:30:47.000Z",
"block_hash": "0x5bdb81ef57386daa6ad3b9893216db9b15bb5d88f9c5524fabb106eace4c69c6",
"transaction_hash": "0x1cf0a4282ca892210a42ee14633d9dffcb644d198a5a7751af1046568c59beb9",
"transaction_index": 156,
"log_index": 306,
"value": "0",
"contract_type": "ERC1155",
"transaction_type": "Single",
"token_address": "0x5728ff21bdadb0d3505f4e307f79282d53d92fde",
"token_id": "0",
"from_address": "0x47da053384abd2ee2bf4883927e7e1dcc3f5f95b",
"to_address": "0x981ff49a2007a095eca50458159525e65e7b1d0c",
"amount": "1",
"verified": 1,
"operator": "0x47da053384abd2ee2bf4883927e7e1dcc3f5f95b"
},
{
"block_number": "15846571",
"block_timestamp": "2022-10-28T12:30:47.000Z",
"block_hash": "0x5bdb81ef57386daa6ad3b9893216db9b15bb5d88f9c5524fabb106eace4c69c6",
"transaction_hash": "0x1cf0a4282ca892210a42ee14633d9dffcb644d198a5a7751af1046568c59beb9",
"transaction_index": 156,
"log_index": 305,
"value": "0",
"contract_type": "ERC1155",
"transaction_type": "Single",
"token_address": "0x03a980c83bb58838298ea7689d9d4d22aad51599",
"token_id": "0",
"from_address": "0x47da053384abd2ee2bf4883927e7e1dcc3f5f95b",
"to_address": "0x981ff49a2007a095eca50458159525e65e7b1d0c",
"amount": "1",
"verified": 1,
"operator": "0x47da053384abd2ee2bf4883927e7e1dcc3f5f95b"
}
]
}
Congratulations π₯³ you just got all the NFT transfers in a block with just a few lines of code using the Moralis NFT API!
API Referenceβ
If you want to know more details on the endpoint and optional parameters, check out:
Supportβ
If you face any trouble following the tutorial, feel free to reach out to our community engineers in our Discord or Forum to get 24/7 developer support.