Use the Web3API in cloud functions

I’m trying to fetch NFT metadata in a cloud function: const tokenIdMetadata = await Moralis.Web3API.token.getTokenIdMetadata(options);

Trying to run the function from the JS console gives me the following error:
Cannot read property 'token' of undefined"

Is it possible to use this API in cloud functions somehow?

Could you show us the options object as well? Which chain are you trying to query ?

Also, let us know what version of the SDK you’re using here ?

These are other posts that discuss some of the implementations –
For BSC testnet -

For using with React-moralis -

I’m trying to use this from within a cloud function:

Moralis.Cloud.define('getNFTMetadata', async (req) => {
  const chain = req.params.chain;
  const address = req.params.address;
  const token_id = req.params.tokenId;
  const options = { address, token_id, chain };
  const tokenIdMetadata = await Moralis.Web3API.token.getTokenIdMetadata(options);
  return tokenIdMetadata;
});

I saw on another post that I’d most likely would need to use the REST API, so now I’m calling it like this:

Moralis.Cloud.define('getNFTMetadata', async (req) => {
  const chain = req.params.chain;
  const address = req.params.address;
  const token_id = req.params.tokenId;
  const options = { address, token_id, chain };
  const tokenIdMetadata = await Moralis.Cloud.httpRequest({
    url: "https://deep-index.moralis.io/api/v2/nft/" + address + "/" + token_id + "?chain=" + chain,
    headers: {
    	"x-api-key": "xxxx"
    }
  });
  return tokenIdMetadata;
});

Yes, please include the API key and after that could you first check if ETH main net querying works?

An example -

Also a youtube tutorial on this -

Yes I included the API key and now it works. At least partially. I’m currently testing on Rinkeby.

When fetching the metadata for 0x8acb7ca932892eb83e4411b59309d44dddbc4cdf with token id 0 I get the following output as desired:

{
  "token_address": "0x8acb7ca932892eb83e4411b59309d44dddbc4cdf"
  "token_id": "0"
  "contract_type": "ERC721"
  "token_uri": "https://ipfs.io/ipfs/Qmd9MCGtdVz2miNumBHDbvj8bigSgTwnr4SbyH6DNnpWdt?filename=1-PUG.json"
  "metadata": NULL
  "synced_at": NULL
  "amount": "1"
  "name": "Dogie"
  "symbol": "DOG"
}

When trying an Opensea NFT contract (0x88b48f654c30e99bc2e4a1559b4dcf1ad93fa656) with token id 60806687971596192406245363901054716930940494021460825456651576206974743740417, I get a 404 error. Is this due to ERC1155 not being supported or is this an issue with OpenSea? I know that Opensea has an error with their metadata uri.
Their uri is something like uri.com/0x{id}.json but their metadata is actually reachable at uri.com/{id}.json (you need to remove the 0x).

EDIT: Also why is the metadata NULL in the first request, shouldn’t it contain the metadata of the token?

Also tested on a mainnet opensea contract (address: 0x495f947276749ce646f68ac8c248420045cb7b5e, tokenId: 91321981255833606897973224722031697500117164304167139448040192051706717011969)

same issue (404)

Hi,
You can fetch the token-url in order to get the metadata, you will get:

{
    "name": "PUG",
    "description": "An adorable PUG pup!",
    "image": "https://ipfs.io/ipfs/QmSsYRx3LpDAb1GZQm7zZ1AuHZjfbPkD6J7s9r41xu1mf8?filename=pug.png",
    "attributes": [
        {
            "trait_type": "cuteness",
            "value": 100
        }
    ]
}

For the OpenSea token part: (https://opensea.io/assets/0x495f947276749ce646f68ac8c248420045cb7b5e/91321981255833606897973224722031697500117164304167139448040192051706717011969) this looks like a lazy minted token (meaning that no gas fees were paid on minting and it is only present in OpenSea internal database until it will be sold and someone will pay the gas fees for minting on blockchain).

Also on details on OpenSea you have:

Contract Address. 0x495f...7b5e
Token ID    9132198125583360...
Blockchain  Ethereum
Metadata    Editable

Having editable metadata suggest that it is lazy minted too.

1 Like