How to get the owner of an NFT
Step 1: Setup Moralis​
Read the article Setting Up Moralis: Getting Started and make sure to finish all the steps. Only after that you can go ahead to complete this guide.
Step 2: Get All NFTs Owned By An Address​
In order to get all the owners of an NFT, Moralis provides you with a getNFTTokenIdOwners endpoint to do so.
Here you'll need three parameters: address
, token_id
and chain
.
Once you've obtained the required parameters, 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 address = "0xa186d739ca2b3022b966194004c6b01855d59571";
const chain = EvmChain.ETHEREUM;
const tokenId = "1";
const response = await Moralis.EvmApi.nft.getNFTTokenIdOwners({
address,
chain,
tokenId,
});
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 address = "0xa186d739ca2b3022b966194004c6b01855d59571";
const chain = EvmChain.ETHEREUM;
const tokenId = "1";
const response = await Moralis.EvmApi.nft.getNFTTokenIdOwners({
address,
chain,
tokenId,
});
console.log(response.toJSON());
};
runApp();
from moralis import evm_api
api_key = "YOUR_API_KEY"
params = {
"address": "0xa186d739ca2b3022b966194004c6b01855d59571",
"token_id": "1",
"chain": "eth",
"format": "decimal",
"limit": 100,
"cursor": "",
"normalizeMetadata": True,
}
result = evm_api.nft.get_nft_token_id_owners(
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": 1,
"page": 1,
"page_size": 100,
"cursor": null,
"result": [
{
"token_address": "0xa186d739ca2b3022b966194004c6b01855d59571",
"token_id": "1",
"owner_of": "0xc81082690edc8cde6d83a7549aa6a74534305372",
"block_number": "15821471",
"block_number_minted": "15821471",
"token_hash": "c4940b74fa9c9e1605316dc98f4a3eb3",
"amount": "1",
"contract_type": "ERC721",
"name": "NFTPass",
"symbol": "ATTPASS",
"token_uri": "https://metadata.atticc.xyz/1",
"metadata": "{\"attributes\":[{\"trait_type\":\"Name\",\"value\":\"Atticc Early Adopter Pass\"},{\"trait_type\":\"Category\",\"value\":\"Early Adopter Pass\"},{\"trait_type\":\"Number of Seats\",\"value\":\"5555\",\"display_type\":\"number\"},{\"trait_type\":\"Royalty\",\"value\":\"0\",\"display_type\":\"number\"}],\"image\":\"https://media.atticc.xyz/Pass1.webp\"}",
"last_token_uri_sync": "2022-10-25T00:21:22.266Z",
"last_metadata_sync": "2022-10-25T00:21:26.139Z",
"minter_address": "0xc81082690edc8cde6d83a7549aa6a74534305372"
}
]
}
Congratulations 🥳 you just got the owners of an NFT 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.