How to Check the Owner of an NFT
Learn how to find the owner of an NFT (ERC-721 or ERC-155) on Ethereum and Polygon in one request using the getOwnersForToken endpoint.
API Endpoint
This tutorial uses the getOwnersForToken endpoint.
If you just need the script for this tutorial refer to the below Recipe or continue reading for more
One of the most fundamental aspects of an NFT is ownership.
Whether you're building an NFT marketplace like OpenSea, a web3 wallet like Rainbow, or an analytics platform like rarity.tools, knowing who owns an NFT is important information.
An NFT can have a single owner (ERC-721) or multiple owners (ERC-1155).
Typically, getting ownership data on an NFT requires you to:
- spin up a node
- call functions on the NFT contract
- parse the output
Now, getting a list of owners for an NFT collection takes one API call using Alchemy's NFT API.
How to Check the Owner of an NFT
In this tutorial, we will write a script in Node.js that tells us who the owner of an NFT is using the NFT API. In this example, we will use an NFT called Safe Haven from the TIMEPieces collection.
Step 0: Configure your developer environment
Before you begin, complete the following steps to set up your web3 developer environment.
1. Install Node.js (> 14) on your local machine
2. Install npm on your local machine
To check your Node version, run the following command in your terminal:
node -v
3. Create a free Alchemy account
Step 1: Create a new Alchemy app
To create an Alchemy app, check out this video or follow the instructions below:
- From Alchemy's dashboard, hover over the Apps drop-down menu and choose Create App.
- Provide a Name and Description for your app.
- For Chain, select Ethereum and for Network select Goerli.
- Click the Create App button.
Once you have created your app, get your API key that we will use later in this tutorial.
- Click on your app's View Key button in the dashboard
- Copy and save the API KEY
Step 2: Create a Node project
Let's create an empty repository and install all node dependencies.
To make requests to the NFT API, we recommend using the Alchemy SDK Quickstart.
However, you can also use the axios
or fetch
libraries. We provide code samples for each
From your terminal, run the following commands:
mkdir nft-owner && cd nft-owner
npm init -y
npm install --save alchemy-sdk
touch main.js
mkdir nft-owner && cd nft-owner
npm init -y
npm install --save axios
touch main.js
mkdir nft-owner && cd nft-owner
npm init -y
touch main.js
This will create a repository named nft-owner
that holds all the files and dependencies we need.
Open this repo in your preferred code editor, where we'll write our code in the main.js
file.
Step 3: Find the owner of an NFT
To get the owner of a single NFT, use the getOwnersForToken function.
This function accepts two required arguments.
contractAddress
: The NFT's smart contract address (ERC-721 or ERC-1155)tokenId
: The ID of the NFT (decimal or hexadecimal format)
We require these two arguments because any NFT on any blockchain can be uniquely identified only by specifying both its contract address and token ID.
For more information, check out the NFT API QuickStart.
Next, add the following code to the main.js
file, using your Alchemy API key:
// Setup: npm install alchemy-sdk
import { Alchemy, Network } from "alchemy-sdk";
const config = {
apiKey: "<-- ALCHEMY APP API KEY -->",
network: Network.ETH_MAINNET,
};
const alchemy = new Alchemy(config);
const main = async () => {
// TIMEPieces contract address
const address = "0xDd69da9a83ceDc730bc4d3C56E96D29Acc05eCDE";
// Safe Haven Token ID
const tokenId = 4254;
// Get owner of NFT
const owner = await alchemy.nft.getOwnersForNft(address, tokenId);
console.log(owner);
};
const runMain = async () => {
try {
await main();
process.exit(0);
} catch (error) {
console.log(error);
process.exit(1);
}
};
runMain();
const axios = require('axios')
// TIMEPieces contract address
const address =
'0xDd69da9a83ceDc730bc4d3C56E96D29Acc05eCDE'
// Safe Haven Token ID
const tokenId = 4254
// Alchemy API key
const apiKey = '<-- ALCHEMY APP API KEY -->';
// Alchemy URL
const baseURL = `https://eth-mainnet.g.alchemy.com/nft/v2/${apiKey}/getOwnersForToken`;
const url = `${baseURL}?contractAddress=${address}&tokenId=${tokenId}`;
const config = {
method: 'get',
url: url,
};
axios(config)
.then(response => console.log(response.data))
.catch(error => console.log(error));
import fetch from 'node-fetch';
// TIMEPieces contract address
const address =
'0xDd69da9a83ceDc730bc4d3C56E96D29Acc05eCDE'
// Safe Haven Token ID
const tokenId = 4254
// Alchemy API key
const apiKey = '<-- ALCHEMY APP API KEY -->';
// Alchemy URL
const baseURL = `https://eth-mainnet.g.alchemy.com/nft/v2/${apiKey}/getOwnersForToken`;
const url = `${baseURL}?contractAddress=${address}&tokenId=${tokenId}`;
var requestOptions = {
method: 'get',
redirect: 'follow'
};
fetch(url, requestOptions)
.then(response => console.log)
.catch(error => console.log('error', error))
Run this script by running the following command in your terminal:
node main.js
If successful, you should see output that looks something like this:
{ owners: [ '0xc9391d1ea5d092f774cf66312a9a347aaedf95a5' ] }
Since the NFT we were interested in was of the ERC-721 standard, we retrieved an array with only a single wallet address as the owner.
If we were retrieving information for an ERC-1155 NFT, which can have multiple owners, the array might contain multiple wallet addresses.
Conclusion
You now know how to use the Alchemy NFT API to check the owner of an NFT.
If you enjoyed this tutorial about how to get all NFTs owned by an address, tweet us at @AlchemyPlatform and give the authors @rounak_banik and @ankg404 a shoutout!
Don't forget to join our Discord server to meet other blockchain devs, builders, and entrepreneurs!
Updated almost 2 years ago