How to call external API in Moralis cloud function and job

Hi,
I am using Moralis cloud function and job to call the external api but looks like it is not working.

 Moralis.Cloud.define("getWalletTokens", async (request) => {
  logger.info('pk request', request);	
  const address = request.param.address;
  const tokenResponse = await getAllTokens(address);
  logger.info(tokenResponse);	
  return tokenResponse;
});

function getAllTokens(address) {
    logger.info('inside request');
	return Moralis.Cloud.httpRequest({
          url: `https://deep-index.moralis.io/api/v2/${address}/erc20?chain=bsc`
        }).then(function(httpResponse) {
        // success
    		logger.info("PK HTTP==>",httpResponse);	
              return httpResponse.text;
        },function(httpResponse) {
          // error
          console.error('Request failed with response code ' + httpResponse.status);
        });
}


const params =  { address: "0xce8b75302ab72fc366b62557cfcbb43f3d438328cf" };
const tokens = await Moralis.Cloud.run("getWalletTokens", params);

I am calling above cloud function from web app. also when I am logging the request parameter I am not seeing the parameter which I am passing from my code. I am getting empty value for request.param.address

this doesn’t work as you would expect, try to use logger.info(JSON.stringify(request))

logger.info doesn’t support 2 parameters

Is there way to save multiple object to Moralis DB?
as currently based on document it is allowing to save single object. like below example.

const Monster = Moralis.Object.extend("Monster");
const monster = new Monster();

monster.save()

There are some functions to do bulk operations, you can find them in documentation.

Hi,
I have written a cloud job in cloud function code as below. but I am not seeing the cloud job in my jobs list only default job i.e. syncBscAddress there.

function updateWalletBalances() {
  const User = Moralis.Object.extend("User");
  const query = new Moralis.Query(User);
  const users = await query.find();
  logger.info('PDXusers===>'+JSON.stringify(users));
}

Moralis.Cloud.job("updateWalletBalance", (request) =>  {
  // params: passed in the job call
  // headers: from the request that triggered the job
  // log: the Moralis Server logger passed in the request
  // message: a function to update the status message of the job object
  const { params, headers, log, message } = request;
  message("I just started");
  return updateWalletBalances();
});

maybe it was not set to run, sometimes I set for a job to run after 12 and half a day doesn’t run

I am making a call to speedy notes inside the cloud function and I am getting below error.

{
    "data": {
        "message": "Rate limit exceeded."
    },
    "headers": {
        "access-control-allow-origin": "*",
        "cf-cache-status": "DYNAMIC",
        "cf-ray": "6ce7ad5548b52b95-FRA",
        "connection": "close",
        "content-length": "34",
        "content-type": "application/json; charset=utf-8",
        "date": "Sun, 16 Jan 2022 13:26:42 GMT",
        "etag": "W/\"22-A17CvmppE17nAhS6Eu3AUBz7zmU\"",
        "expect-ct": "max-age=604800, report-uri=\"https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct\"",
        "server": "cloudflare",
        "vary": "Accept-Encoding",
        "x-powered-by": "Express",
        "x-rate-limit-ip-used": "200",
        "x-rate-limit-limit": "1500",
        "x-rate-limit-remaining": "55",
        "x-rate-limit-remaining-ip-ttl": "55",
        "x-rate-limit-remaining-ttl": "55",
        "x-rate-limit-throttle-ip-used": "144",
        "x-rate-limit-throttle-limit": "60",
        "x-rate-limit-throttle-remaining": "5",
        "x-rate-limit-throttle-remaining-ip-ttl": "5",
        "x-rate-limit-throttle-remaining-ttl": "5",
        "x-rate-limit-throttle-used": "144",
        "x-rate-limit-used": "208"
    },
    "status": 429,
    "text": "{\"message\":\"Rate limit exceeded.\"}"
}

It looks like a request to web3api and that rate limit seems to be because of too many requests made in a second.

So, what is solution to the same?

You might wanna make a gap of a few seconds between making your request to avoid getting rate limited :raised_hands:

I have put the delay between the API call. but still I am getting same issue.

you could try with a bigger delay, at some point it should work

you are not using javascript? asking because you may be able to do that call directly from the SDK

1 Like

Can we use javascript fetch method instead Moralis.Cloud.httpRequest inside the cloud function?

no, only Moralis.Cloud.httpReques, but it should work fine

Although we have specified a bigger delay but we are getting same issue. Same web3 api we are using in web application where we are making similar amount of requests. it is working fine.
Any idea beside above issue?

it looks like there are too many requests made in a short period of time

I have put 500ms delay between each requests, though I am getting same error.

it may depend on what request are made, you can try with 2000 ms first to see how it works.