NFT API - Full Guide & Walkthrough
What is an NFT API?β
An NFT API is a collection of APIs that can be used to index NFT data, e.g. NFT balance, NFT metadata, from multiple EVM chains that we support.
The NFT API is designed to provide high-quality, structured NFT data to developers to build application that interact with various kinds of NFTs (ERC721, ERC115, others).
Therefore, the ideal use cases for the NFT API are listed as below, but not limited to:
- Portfolio dashboard
- Wallets
- NFT-gating applications (NFT-based tickets or memberships)
- NFT marketplaces
- etc.
The NFT API is divided into 6 categories based on their use cases as follows:
Get NFTs APIβ
The Get NFTs API is a collection of NFT API that provides developer with all NFT stats (name, address, metadata, etc.) that fulfills certain search requirements, e.g. NFTs owned by a wallet, NFTs in a collection, NFTs with specific metadata, etc.
This API category comprised of 4 APIs:
API Name | Path | Use Cases |
---|---|---|
getMultipleNFTs | /nft/getMultipleNFTs | Fetch a complete stats of multiple NFTs (max 25 per call) in a given tokens array. |
getWalletNFTs | /{address}/nft | Fetch all NFTs in a given wallet address . This is ideal for displaying NFT balance for a portfolio dashboard. |
getContractNFTs | /nft/{address} | Fetch all tokens in a given NFT collection address . |
searchNFTs | /nft/search | Fetch all NFT that have metadata that contains a given q string. This is ideal for building an NFT search functionality. |
Get NFT Transfers APIβ
The Get NFT Transfers API is a collection of NFT API that provides developer with various historical NFT transfers data.
This API category comprised of 5 APIs:
API Name | Path | Use Cases |
---|---|---|
getWalletNFTTransfers | /{address}/nft/transfers | Fetch all transfers of NFT that interacted with a given address . This does not include any indirect NFT transfers, e.g. transferring NFT with OpenSea contract. |
getNFTContractTransfers | /nft/{address}/transfers | Fetch all transfers of a specific NFT collection with a given contract address . |
getNFTTransfersFromToBlock | /nft/transfers | Fetch all transfers of NFTs within the given from_block and to_block range. This is ideal for fetching NFT transfers from a specific range of time or block numbers. |
getNFTTransfersByBlock | /block/{block_number_or_hash}/nft/transfers | Fetch all transfers of NFTs in a given block_number_or_hash |
getNFTTransfers | /nft/{address}/{token_id}/transfers | Fetch all transfers of a specific NFT with given address and token_id . |
Get NFT Collections APIβ
The Get NFT Collections API is a collection of NFT API that provides developer with NFT collections-related data.
If an NFT collection has not been synced by the Moralis NFT API, it is also possible to manually index it by calling the syncNFTContract API.
This API category comprised of 3 APIs:
API Name | Path | Use Cases |
---|---|---|
getWalletNFTCollections | /{address}/nft/collections | Fetch all NFT collection (not specific token) that is owned by a given address . |
getNFTContractMetadata | /nft/{address}/metadata | Fetch the NFT collection metadata (name, symbol, and contract type ERC721, ERC115, others) of a given address . |
syncNFTContract | /nft/{address}/sync | Manually sync to index an unindexed NFT collection with a given address . |
Get NFT Owners APIβ
The Get NFT Owners API is a collection of NFT API that provides developer with fetching and verifying ownership of certain NFT or NFT collections.
This API category comprised of 2 APIs:
API Name | Path | Use Cases |
---|---|---|
getNFTOwners | /nft/{address}/owners | Fetch all the token holders/owners of a given NFT collection with contract address . This can be used to verify NFT ownership for NFT-gating application. |
getNFTTokenIdOwners | /nft/{address}/{token_id}/owners | Fetch all the token holders/owners of a given specific NFT token with contract address and token_id . This can be used to verify NFT ownership for NFT-gating application. |
Get NFT Market Data APIβ
The Get NFT Market Data API is a collection of NFT API that provides developer with all NFT market related-data, such as NFT historical trades and price.
This API category comprised of 2 APIs:
API Name | Path | Use Cases |
---|---|---|
getNFTTrades | /nft/{address}/trades | Fetch the list of trades of an NFT with a given contract address on OpenSea NFT Marketplace. |
getNFTLowestPrice | /nft/{address}/lowestprice | Fetch the lowest price of an NFT with a given contract address on OpenSea NFT Marketplace. |
Get NFT Metadata APIβ
The Get NFT Metadata API is a collection of NFT API that provides developer with metadata of NFTs that would like to be indexed. This works out of the box with various types of NFTs (ERC721, ERC1155, others) and either on-chain or off-chain metadata.
This API comprised of 2 APIs:
API Name | Path | Use Cases |
---|---|---|
getNFTMetadata | /nft/{address}/{token_id} | Fetch the metadata (either off-chain or on-chain) a specific NFT with a given address and token_id . |
reSyncMetadata | nft/{address}/{token_id}/metadata/resync | Resync/Update NFT metadata to its latest, if it dynamically changes. |
How to get started?β
To get started with Moralis NFT API, there are two methods that can be used, depending on the programming language that you are using:
Programming Languages | Method |
---|---|
JavaScript/TypeScript, Python | Moralis SDKs |
Others (e.g. Java, C/C++, Ruby, etc.) | REST API |
For this guide, we'll particularly use the Moralis SDK for examples.
If you would like to use other languages calling the Moralis NFT API using regular REST API call, then make sure to check the NFT API reference pages to get all the parameters and responses type.
Step 1: Install the Moralis SDKβ
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
yarn add moralis
pnpm add moralis
pip install moralis
Step 2: Add to Your Codeβ
To use the NFT API, it is very simple. All the NFT API can be called by using Moralis.EvmApi.nft.{apiName}
where apiName
will be replaced by the NFT API used.
In this guide, suppose you are building a simple NFT portfolio dashboard.
Let's start to use the NFT API to build these three initial features:
Feature #1: Fetch NFT balance of the user's wallet addressβ
In order to fetch the NFT balance of the user's wallet address, Moralis provides you with a getWalletNFTs endpoint to do so.
Here you'll need two parameters: address
and chain
.
Once you've obtained both the address
and chain
, you can copy the following code and add it to your existing codebase:
- JavaScript
- TypeScript
- 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 address = "USER_WALLET_ADDRESS";
const chain = "CHAIN" // e.g EvmChain.ETHEREUM, EvmChain.POLYGON
const response = await Moralis.EvmApi.nft.getWalletNFTs({
address,
chain,
});
console.log(response.raw);
};
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 address = "USER_WALLET_ADDRESS";
const chain = "CHAIN" // e.g EvmChain.ETHEREUM, EvmChain.POLYGON
const response = await Moralis.EvmApi.nft.getWalletNFTs({
address,
chain,
});
console.log(response.raw);
};
runApp();
from moralis import evm_api
import json
api_key = "YOUR_API_KEY"
params = {
"address": "USER_WALLET_ADDRESS",
"chain": "CHAIN", # e.g. "eth", "polygon", etc.
}
result = evm_api.nft.get_wallet_nfts(
api_key=api_key,
params=params,
)
# converting it to json because of unicode characters
print(json.dumps(result, indent=4))
Once the code is added, you will be able to obtain all the NFT that the user own in its wallet address using just a few lines of code with Moralis NFT API.
Feature #2: Fetch Historical NFT transfers that interacted with user's wallet addressβ
In order to fetch the historical NFT transfers that interacted the user's wallet address, Moralis provides you with a getWalletNFTTransfers endpoint to do so.
Here you'll need two parameters: address
and chain
.
Once you've obtained both the address
and chain
, you can copy the following code and add it to your existing codebase:
- JavaScript
- TypeScript
- 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 address = "USER_WALLET_ADDRESS";
const chain = "CHAIN" // e.g EvmChain.ETHEREUM, EvmChain.POLYGON
const response = await Moralis.EvmApi.nft.getWalletNFTTransfers({
address,
chain,
});
console.log(response.raw);
};
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 address = "USER_WALLET_ADDRESS";
const chain = "CHAIN" // e.g EvmChain.ETHEREUM, EvmChain.POLYGON
const response = await Moralis.EvmApi.nft.getWalletNFTTransfers({
address,
chain,
});
console.log(response.raw);
};
runApp();
from moralis import evm_api
api_key = "YOUR_API_KEY"
params = {
"address": "USER_WALLET_ADDRESS",
"chain": "CHAIN", # e.g. "eth", "polygon", etc.
}
result = evm_api.nft.get_wallet_nft_transfers(
api_key=api_key,
params=params,
)
print(result)
Once the code is added, you will be able to obtain the historical NFT transfers that interacted the user's wallet address using just a few lines of code with Moralis NFT API.
Feature #3: Fetch NFT lowest price from the NFT balanceβ
In order to fetch the NFT price from the NFT balance, Moralis provides you with a getNFTLowestPrice endpoint to do so.
Here you'll need two parameters: address
and chain
.
Once you've obtained both the address
and chain
, you can copy the following code and add it to your existing codebase:
- JavaScript
- TypeScript
- 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 address = "NFT_CONTRACT_ADDRESS"; // provided from `getWalletNFTs`
const chain = "CHAIN" // e.g EvmChain.ETHEREUM, EvmChain.POLYGON
const response = await Moralis.EvmApi.nft.getNFTLowestPrice({
address,
chain,
});
console.log(response.raw);
};
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 address = "NFT_CONTRACT_ADDRESS"; // provided from `getWalletNFTs`
const chain = "CHAIN" // e.g EvmChain.ETHEREUM, EvmChain.POLYGON
const response = await Moralis.EvmApi.nft.getNFTLowestPrice({
address,
chain,
});
console.log(response.raw);
};
runApp();
from moralis import evm_api
api_key = "YOUR_API_KEY"
params = {
"address": "NFT_CONTRACT_ADDRESS", # provided from `getWalletNFTs`
"chain": "CHAIN", # e.g. "eth", "polygon", etc.
}
result = evm_api.nft.get_nft_lowest_price(
api_key=api_key,
params=params,
)
print(result)
Once the code is added, you will be able to obtain the NFTs lowest price from the NFT balance using just a few lines of code with Moralis NFT API.
Step 3: Going Live!β
Once you have a few lines of new code, you have successfully integrated the Moralis NFT API to your simple NFT portfolio dashboard app.
Now, it's time to push your code to production.
Before doing so, make sure that your API key is stored in a secure place. The best practice for this will be:
- Storing your API key in an environment variable (secrets)
process.env
- Have your API called on the backend. While it is possible to call the NFT API on the fronted, it is highly discouraged as it can easily reveal your API key on the browser.
Once everything checks out, your app is good to go live with Moralis NFT API! π