How to Check the Owner of an NFT
Learn how to find the owner of an NFT on Ethereum and Polygon. The getOwnersForCollection is particularly useful for checking the owner of a specific ERC-721 or ERC-155 token.
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.
A Beeple NFT owned by chronology.eth
Typically, getting ownership data on an NFT requires you to:
  1. 1.
    spin up a node
  2. 2.
    call functions on the NFT contract
  3. 3.
    parse the output
Now, getting a list of owners for an NFT collection takes one API call using Alchemy's NFT API.
How to Create

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:
1
node -v
Copied!

Step 1: Create a new Alchemy app

To create an Alchemy app:
  1. 1.
    Go to your Alchemy dashboard
  2. 2.
    Hover over the Apps drop-down menu
  3. 3.
    Choose Create App
  4. 4.
    Write a Name
  5. 5.
    Add a description
  6. 6.
    Select Ethereum as your Chain
  7. 7.
    Choose Mainnet as your Network
  8. 8.
    Click the Create App button
Creating an app on the Alchemy Dashboard
Once you have created your app, get your API key that we will use later in this tutorial.
  1. 1.
    Click on your app's View Key button in the dashboard
  2. 2.
    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.
However, you can also use the axios or fetch libraries. We provide code samples for each.
From your terminal, run the following commands:
Alchemy Web3 (Recommended)
Axios
Fetch
1
mkdir nft-owner && cd nft-owner
2
npm init -y
3
npm install --save @alch/alchemy-sdk
4
touch main.js
Copied!
1
mkdir nft-owner && cd nft-owner
2
npm init -y
3
npm install --save axios
4
touch main.js
Copied!
1
mkdir nft-owner && cd nft-owner
2
npm init -y
3
touch main.js
Copied!
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.
  1. 1.
    contractAddress: The NFT's smart contract address (ERC-721 or ERC-1155)
  2. 2.
    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 FAQ.
Next, add the following code to the main.js file, using your Alchemy API key:
Alchemy SDK (Recommended)
Axios
Fetch
1
const { initializeAlchemy, getOwnersForNft } = require('@alch/alchemy-sdk');
2
3
// Alchemy app API key
4
const settings = {
5
apiKey: '<-- YOUR ALCHEMY APP API KEY -->',
6
};
7
8
const alchemy = initializeAlchemy(settings);
9
10
const main = async () => {
11
12
// TIMEPieces contract address
13
const address =
14
'0xDd69da9a83ceDc730bc4d3C56E96D29Acc05eCDE'
15
16
// Safe Haven Token ID
17
const tokenId = 4254
18
19
// Get owner of NFT
20
const owner = await getOwnersForNft(alchemy, address, tokenId)
21
console.log(owner)
22
}
23
24
const runMain = async () => {
25
try {
26
await main();
27
process.exit(0);
28
}
29
catch (error) {
30
console.log(error);
31
process.exit(1);
32
}
33
};
34
35
runMain();
Copied!
1
const axios = require('axios')
2
3
// TIMEPieces contract address
4
const address =
5
'0xDd69da9a83ceDc730bc4d3C56E96D29Acc05eCDE'
6
7
// Safe Haven Token ID
8
const tokenId = 4254
9
10
// Alchemy API key
11
const apiKey = '<-- ALCHEMY APP API KEY -->';
12
13
// Alchemy URL
14
const baseURL = `https://eth-mainnet.alchemyapi.io/nft/v2/${apiKey}/getOwnersForToken`;
15
const url = `${baseURL}?contractAddress=${address}&tokenId=${tokenId}`;
16
17
const config = {
18
method: 'get',
19
url: url,
20
};
21
22
axios(config)
23
.then(response => console.log(response.data))
24
.catch(error => console.log(error));
Copied!
1
import fetch from 'node-fetch';
2
3
// TIMEPieces contract address
4
const address =
5
'0xDd69da9a83ceDc730bc4d3C56E96D29Acc05eCDE'
6
7
// Safe Haven Token ID
8
const tokenId = 4254
9
10
// Alchemy API key
11
const apiKey = '<-- ALCHEMY APP API KEY -->';
12
13
// Alchemy URL
14
const baseURL = `https://eth-mainnet.alchemyapi.io/nft/v2/${apiKey}/getOwnersForToken`;
15
const url = `${baseURL}?contractAddress=${address}&tokenId=${tokenId}`;
16
17
var requestOptions = {
18
method: 'get',
19
redirect: 'follow'
20
};
21
22
fetch(url, requestOptions)
23
.then(response => console.log)
24
.catch(error => console.log('error', error))
Copied!
Run this script by running the following command in your terminal:
1
node main.js
Copied!
If successful, you should see output that looks something like this:
1
{ owners: [ '0xc9391d1ea5d092f774cf66312a9a347aaedf95a5' ] }
Copied!
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

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!