Skip to main content
Best answer

Multiple requests on the same trigger

  • August 25, 2020
  • 6 replies
  • 2124 views

anfuca

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

Best answer by Mercology

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 = [];
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...');
 })

 

 

View original
Did this topic help you find an answer to your question?
This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

6 replies

Mercology
Forum|alt.badge.img+5
  • Zapier Expert
  • 65 replies
  • Answer
  • August 25, 2020

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 = [];
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...');
 })

 

 


anfuca
  • Author
  • Beginner
  • 5 replies
  • August 25, 2020

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":[{"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


Mercology
Forum|alt.badge.img+5
  • Zapier Expert
  • 65 replies
  • August 25, 2020

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


anfuca
  • Author
  • Beginner
  • 5 replies
  • August 26, 2020

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

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


anfuca
  • Author
  • Beginner
  • 5 replies
  • August 31, 2020

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!


Danvers
Forum|alt.badge.img+12
  • Zapier Staff
  • 3731 replies
  • September 15, 2020

Thanks for sharing the answer with us, @anfuca!