Extracting token metadata from token_uri

Web3Api.account.getNFTsForContract has been returning null metadata for the past 4/5 days. In the meantime, I’m trying to extract the metadata json object from the IPFS token_uri.
Chain: 0x4, Server subdomain: z62iwqedukzn
This is the cloud function I’m using:

Moralis.Cloud.define("get_token_uri", async (url) => {
        return await Moralis.Cloud.httpRequest({
            url: url,
            headers: {
                method: "GET",
                accept: "application/json",
            },
        })
            .then((httpResponse) => httpResponse.data)
            .catch((error) => logger.info(error));
    });
let tokenList;
    function getData() {
        tokenList = data.result.map(async (token) => {
            let metadata = await Moralis.Cloud.run(
                "get_token_uri",
                token.token_uri
            );
            return { id: token.token_id, metadata };
        });
    }

But here, metadata is returning undefined. How can I actually get the metadata from the token_uri?
ssf

You could add some logging to see what is happening, like logger.info(url) in your cloud function. And also some logging in case of error.
You can also try this example: NFTOwners beforesave function failing after update

1 Like
Moralis.Cloud.define("get_token_uri",(url) => {
  return Moralis.Cloud.httpRequest({
    "url": url,
    "headers": {
        'method': 'GET',
        'accept': 'application/json'
       }
   }).then(function(httpResponse){
    return httpResponse.data;
  },function(httpResponse){
    logger.info("error");
    logger.info(httpResponse);
  });
 });

I tried that example, it’s still returning undefined and this is what I’m getting in the logs:

  • 2021-11-20T14:39:56.304Z - Ran cloud function get_token_uri for user sgRjBIlPwHlNIloeP4T2CGaG with: Input: {} Result: undefined
  • 2021-11-20T14:39:56.303Z -
  • 2021-11-20T14:39:56.302Z - error

I think that you are not reading or/and sending the url parameter the right way

Yeah the value doesn’t seem to be getting passed on properly but I checked the uri parameter and
token.token_uri
does contain the actual uri. So I don’t get why it isn’t getting passed on to the cloud function.

Also is there any ETA for when the metadata property will start generating normally again? It was working perfectly fine until the last 4/5 days.

The syntax may be different to pass a parameter to a cloud function, and also to read it in a cloud function.

I don’t expect to get it fixed this weekend.

Yeah I needed to send the uri in an object. It’s working now.

1 Like

Could you write what that looked like? I’m just starting today with the NFT programming tutorial and there is no metadata, so having this to look at when I get to the same point might be really helpful, thank you!

Instead of passing the token_uri directly to the cloud function, I made the uri a property of a param object and then passed the param object to the cloud function. As shown here: https://docs.moralis.io/moralis-server/cloud-code/cloud-functions#calling-cloud-functions

@kbx hey, I have the same issue as you did in this post. I see that you solved it by providing the uri as a property of a param object and then passed it to cloud function. I am still struggling with getting my code work. Would you mind sending a code snip of your solution when you get a chance? Thank you!

Hello, you just have to follow the documentation where you pass in a params object.

Moralis.Cloud.define("get_token_uri", async (request) => {
  return await Moralis.Cloud.httpRequest({
      url: request.params.url,
      headers: {
          method: "GET",
          accept: "application/json",
      },
  })
      .then((httpResponse) => httpResponse.data)
      .catch((error) => logger.info(error));
});

...

let metadata = await Moralis.Cloud.run('get_token_uri', {
  url: 'https://ipfs.moralis.io:2053/ipfs/QmUdMnJXxjNeo5WMguGJdptKXRTouwuQEddnc2bpseYYYu',
});
console.log('metadata', metadata);
1 Like

@alex Okay thank you! I got it working now.!