How to retrieve the owner's address of a Polygon NFT without using OpenSea API?

I’m trying to get the owner’s address of an NFT on the Polygon Network. The OpenSea API doesn’t support Polygon, which is causing issues. I need this address for a MetaMask auth process.

I’ve considered web scraping, but I’m looking for a more reliable method. Any suggestions on how to fetch the owner’s address from Polygon directly?

Here’s some example code I’ve been working with:

const Web3 = require('web3');
const web3 = new Web3('https://polygon-rpc.com');

const contractAddress = '0x1234567890123456789012345678901234567890';
const tokenId = '123456789';

async function getNftOwner() {
  const contract = new web3.eth.Contract(ABI, contractAddress);
  try {
    const owner = await contract.methods.ownerOf(tokenId).call();
    console.log('NFT owner:', owner);
  } catch (error) {
    console.error('Error:', error);
  }
}

getNftOwner();

Any help or tips would be greatly appreciated!

Your approach using Web3.js is sound. I’ve implemented similar functionality recently. One crucial aspect to consider is the ERC721 standard interface. Most NFTs on Polygon adhere to this. Ensure your ABI includes the ‘ownerOf’ function. If you’re unsure about the ABI, you can often find verified contract ABIs on Polygonscan.

A reliable alternative is to use the Polygon RPC directly. You can make HTTP requests to the RPC endpoint with the appropriate JSON-RPC call. This method bypasses the need for Web3.js entirely, which can be beneficial in certain scenarios.

Remember to handle rate limiting and potential network issues in your production code. Polygon’s network can occasionally experience congestion, so implementing retry logic is advisable.

hey mate, i’ve dealt with this before. you’re on the right track with web3.js! just make sure ur ABI is correct for the nft contract. also, double-check the contract address and tokenId. if ur still stuck, try using etherscan’s polygon explorer to verify the contract details. good luck!

hey there! i’ve been working with polygon nfts recently too, and i think i might have a solution for you. have you considered using the alchemy api? it’s pretty awesome for this kind of stuff and supports polygon.

here’s a quick example of how you could use it:

const { Network, Alchemy } = require("alchemy-sdk");

const config = {
  apiKey: "YOUR_API_KEY",
  network: Network.MATIC_MAINNET,
};
const alchemy = new Alchemy(config);

async function getOwner(contractAddress, tokenId) {
  const owner = await alchemy.nft.getOwnersForNft(contractAddress, tokenId);
  console.log("Owner:", owner);
}

getOwner("0x1234...", "123");

it’s pretty straightforward and reliable. plus, you don’t have to worry about dealing with abis or anything like that. what do you think? have you tried any other apis besides opensea?