400 Bad Request Moralis SDK Web3API.token.getTokenIdMetadata

Hello, Iā€™ve been getting this 400 Bad Request error response when I try to run:

const options = { address, token_id,  chain: 'eth'};

const nft = await Moralis.Web3API.token.getTokenIdMetadata(options);

The weirdest thing is that I also get a lot of 200 success responses where I get the data I expect, even though the only difference between requests that give me 200 vs 400 is the token_id.

The actual error response is:

{"code":141,"error":"[object Object]"}

Iā€™ve tried moralis version 0.0.45 and 0.0.69 with no improvement and this was working no problem a few weeks ago. It was only brought to my attention recently and when I look to see whatā€™s going on, this happens across like 50-100 requests where half are 200 responses with the metadata that I need and the other are the error above.

Any idea what might be causing this? Is it due to rate limiting or my Moralis server needing to be upgraded?

Hey @snstarosciak

Please always provide your server subdomain so we will be able to check your logs on our end :raised_hands:

that seems like a old version of Moralis

you may also have an old version of Moralis Server because you donā€™t have a specific error message.

Thanks for the response. What version of Moralis should I be using then?

In terms of Moralis server, is that something I can update through the Moralis admin dashboard?

@Yomoo are you referring to https://t4uvhn89qbpj.usemoralis.com?

you can update the server from admin dashboard, it looks like your server is version 0.0.281 and latest version now is 0.0.307

you should use latest Moralis SDK version
usually you can get latest version with <script src="https://npmcdn.com/moralis@latest/dist/moralis.js" ></script>

Ah okay thankyou for this, after upgrading I am now seeing that the 141 error is saying that the Rate limit is exceeded. So my theory was correct haha is this related to the server needing to be upgraded? Or do I need to artificially slow down the requests?

what is the exact error that you get?

depending on the message it can be two different cases: https://docs.moralis.io/misc/rate-limit#why-am-i-rate-limited

Ah this makes sense. The exact error Iā€™m getting is this:

{"code":141,"error":"Rate limit exceeded."}

I saw you can setup rate limiting in the cloud code, but Iā€™m not exactly sure where to do that. I saw this snippet:

Moralis.settings.setAPIRateLimit({
  anonymous:10, authenticated:20, windowMs:60000
})

Is there an equivalent for the Front-end SDK?

that error look like the second case of error, how many requests did you make?

you have to put a modified version of that snippet in your cloud code, it should be enough to put it there, but that 10 means that are allowed only 10 requests in a minute for a user that is not authenticated.

Hmm yeah I mean Iā€™m on the paid plan and Iā€™m still getting this error, even after upgrading the server. I believe itā€™s roughly 139 requests where Iā€™m hitting rate limits. Even when I update the cloud code, itā€™s still executing all those calls and Iā€™m hitting the same rate limit issue hmmm. Just out of curiosity, what would be a safe API Rate limit for a free account? Maybe I can start there just to make sure thatā€™s working at the very least.

those 139 requests, in what interval of time are you making them? in a second, in a minute?

and to what API endpoint are those requests?

The Request URL is https://t4uvhn89qbpj.usemoralis.com:2053/server/functions/getTokenIdMetadata

And actually I may have not updated the correct place with the rate limiting code. Is there some documentation to explains where exactly to place ā€œCloud codeā€? I assume itā€™s not inside the ā€œCloud Functionsā€ code but elsewhere, because it doesnā€™t seem to be rate limiting my calls at all. Itā€™s 139 calls in less than a minute, for sure

I see this in logs:

Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com web3 api fetch error: Rate limit exceeded. HTTPResponse { 
Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com   status: 429, 
Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com   headers: { 
Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com    date: 'Fri, 17 Dec 2021 14:06:56 GMT', 
Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com     'content-type': 'application/json; charset=utf-8', 
Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com     'content-length': '34', 
Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com     connection: 'close', 
Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com     'x-powered-by': 'Express', 
Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com     'access-control-allow-origin': '*', 
Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com     'x-rate-limit-remaining-ttl': '51', 
Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com     'x-rate-limit-used': '283', 
Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com     'x-rate-limit-limit': '5000', 
Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com     'x-rate-limit-throttle-remaining-ttl': '5', 
Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com     'x-rate-limit-throttle-used': '192', 
Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com     'x-rate-limit-throttle-limit': '100', 
Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com     'x-rate-limit-remaining': '51', 
Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com     'x-rate-limit-throttle-remaining': '5', 

in particular:

Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com     'x-rate-limit-throttle-used': '192', 
Dec 17 06:06:56 t4uvhn89qbpj.usemoralis.com     'x-rate-limit-throttle-limit': '100', 

Hmmm yeah Iā€™m not sure what to do here. I keep getting rate limited even though Iā€™m on the paid plan and I normally shouldnā€™t be even getting close to the limit. Is there some kind of rate block thatā€™s happening that is impossible to get around?

so 100 is the max there? I guess thereā€™s no way around that is there

I think that you are making too many requests in a second, and it is a limit of requests per second

Well Iā€™ve added a rate limiting function on the front-end website that only tries calling the API a max of 5 times per second and now Iā€™m getting this error:

{"code":141,"error":"Too many requests, please try again later."}

What could I be doing wrong now? haha

now you are in the case of rate limit that you can resolve with that code added in cloud functions code
but add there bigger numbers and not only 10 and 20

I tried that and it didnā€™t seem to do anything. Itā€™s really weird that Iā€™m only doing 5 requests to that API endpoint per second and after like 50 calls, Iā€™m getting that same error now. Am I being limited per minute now if itā€™s more than 100 or so?