I am attempting to cross-reference my local database and confirm ownership of an NFT token. I have the contract address and the users wallet address and Im trying to get a returned array of all current tokens owned by that user for that given contract. If I visit the etherscan contract page I can manually enter the address of the given wallet and get just what I need:
Is there a simple API I can use to get just the current owner of all tokens under a contract? I tried the api from Etherscan below however that doesn't return current ownership, but a list of the transactions.
Try using to OpenSea's Retrieving assets endpoint.
const { data } = await axios.get(
`https://api.opensea.io/api/v1/collections?asset_owner=${userAddress}`,
{
headers: {
Accept: "application/json",
"X-API-KEY": process.env.OPENSEA_API,
},
}
);
This returns an array of a given user's assets.
Another option is to Get a list of 'ERC721 - Token Transfer Events' by Address using Etherscan API
const tokenTransfersByAddress = async (contractaddress, address) => {
try {
const options = {
method: "GET",
url: "https://api.etherscan.io/api",
params: {
module: "account",
action: "tokennfttx",
contractaddress,
address,
page: "1",
offset: "10",
sort: "asc",
apikey: process.env.ETHERSCAN,
},
};
const { data } = await axios.request(options);
return data.result;
} catch (error) {
console.log(error);
return [];
}
};
This returns an array of transactions a given user has made.
Hope this helps!