Skip to main content

Hi, 
I want to update an existing trigger for my app. 
I want to include a new value on the response. But, to include that value, I need to make a 2nd API call using a value from the first response.

Can I make multiple requests on a same trigger? Or at least, a second one? I’m trying to make a second z.request into the response of the first request with no lucky.


Thanks in advance

I have done this a few times.  The key is to have the first request use a synchronous call so it executes to completion and then the use the data from that call to make the second request.

Part 1:  setup the first API call.  I had a need to call an API several times to get the data I needed for the second call.  This also works for a single call.

//
//Function to call each url in an array of urls
//
const requestAsync = function(url) {
    return z.request(url).then((response) => response.json)
}
//
//Create the array of urls to call synchronously
//
var urlArr = A];
const urls = {  url: 'https://your.api.com',   method: 'GET',  

  headers: {  'Accept': 'application/json'  },
  params: { “name”: “myparameter”  }
};
urlArr.push(urls);

 

PART 2: Call the first API synchronously
//
//Call the function for each item in the urlArr
//
return Promise.all(urlArr.map(requestAsync))
 .then(function(values){
   //Process the results of the first API Call

   }

Part 3: Build the API options

    const options = {  url: 'https://your.api.com',
      method: 'PUT',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
      },
      params: { “name”: “myparameter”  },
      body: {“name”: “MyBody”   }
    };

Part 4: Call the second API

        return z.request(options)
          .then((response) => {
            response.throwForStatus();
            const results = response.json;
            if (response.status === 200) {
                return {"response status": response.status};
            } else {
                return results;
            }
         }); //End of PUT
   }) //End of Promise
 .catch(function(err) {
    z.console.log(err);
    //throw new Error('SOMETHING WENT WRONG...');
 })

 

 


Hi @Mercology thanks, for your answer.

I’m working on it. 

Now I have a problem trying to process the first API call response.

The “values” that is returned is something like this:
{"total":8,"elements":l{"id":"1234","status":"RESPONSED","userCode":":censored:6:e19648f405:","groupCode":":censored:6:e19648f405:","creationDate":1597271297982}]

I’m trying to access to elements. But if I do values.elements, an error appears:
Results must be an array, got: undefined, (undefined)

In my initial trigger, with just one API call, it works perfect. I am not very familiar with the use of Promise.all so maybe I’m missing something.

Thanks again


What does values.elements[0].id produce?  Also if you could share your code, I might be able to assist more.  Feel free to direct message me.

David


values.elements[0].id returns ‘undefined’.

If I made a simple API call without using Promise.All, it works… :expressionless:


Ok, so finally what I needed to put is values[0].elements[0].id
Remember to always use z.console.log 

Thanks @Mercology for the help!


Thanks for sharing the answer with us, @anfuca!