apitokennftcontractetherscan

Get Current Token Owner for a given NFT Contract


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:

r

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.

https://api-rinkeby.etherscan.io/api?module=account&action=tokennfttx&contractaddress=0x1481e948f2cc7886D454532714D011A7D8e9ec2e&address=0xe93FBC84f5743Ec68a03260f4A9A23c708593d02&page=1&offset=10000&startblock=0&endblock=27025780&sort=asc&apikey=$myapikey


Solution

  • 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!