How to chain API calls in Zapier using data from the first request

I’m new to Zapier and I’m having trouble figuring out how to make two API calls where the second one uses data from the first. I’ve tried using promises but I can’t get async/await to work properly. Here’s what I’ve got so far:

function fetchFirstData(z, bundle) {
  const requestConfig = {
    url: `https://api.example.com/${bundle.authData.org}/catalogs/${bundle.inputData.catalogId}/items`,
    method: 'GET',
    headers: {
      'Accept': 'application/json',
      'Authorization': `Bearer ${bundle.authData.accessToken}`
    },
    params: { limit: 1 }
  };

  return z.request(requestConfig).then(response => response.json);
}

function fetchSecondData(z, bundle, firstData) {
  const requestConfig = {
    url: `https://api.example.com/${bundle.authData.org}/catalogs/${bundle.inputData.catalogId}/items/${firstData[0].id}`,
    method: 'GET',
    headers: {
      'Accept': 'application/json',
      'Authorization': `Bearer ${bundle.authData.accessToken}`
    }
  };

  return z.request(requestConfig).then(response => response.json);
}

// How do I combine these two calls and process the results?

Can someone help me understand how to chain these requests and process the data correctly in Zapier? Thanks!

As someone who’s worked extensively with Zapier, I can share a technique that’s been reliable for me when chaining API calls. Instead of using promises or async/await directly, I’ve found success with Zapier’s built-in z.request() method combined with a series of .then() calls. Here’s an approach that might work for you:

return z.request(firstRequestConfig)
.then(response => {
const firstData = response.json;
return z.request({
url: https://api.example.com/${bundle.authData.org}/catalogs/${bundle.inputData.catalogId}/items/${firstData[0].id},
method: ‘GET’,
headers: {
‘Accept’: ‘application/json’,
‘Authorization’: Bearer ${bundle.authData.accessToken}
}
});
})
.then(response => {
const secondData = response.json;
// Process both sets of data here
return { firstData, secondData };
});

This method has consistently worked for me in Zapier’s environment. It keeps the code clean and avoids some of the pitfalls with async/await in Zapier’s execution context. Give it a try and let me know if you need any further clarification!

I’ve encountered similar challenges with API chaining in Zapier. One effective approach is to utilize the .perform() method within your custom code step. Here’s a refined version of your code that should work:

const fetchFirstData = (z, bundle) => {
  // Your existing fetchFirstData code here
};

const fetchSecondData = (z, bundle, firstData) => {
  // Your existing fetchSecondData code here
};

const perform = async (z, bundle) => {
  const firstData = await fetchFirstData(z, bundle);
  const secondData = await fetchSecondData(z, bundle, firstData);
  
  // Process and return the combined data
  return {
    firstDataResult: firstData,
    secondDataResult: secondData
  };
};

module.exports = {
  perform
};

This structure allows Zapier to handle the asynchronous nature of the API calls more efficiently. Remember to adjust the return object based on your specific data processing needs.

hey there! i’ve dealt with this before. try using a promise chain instead of async/await. something like:

return fetchFirstData(z, bundle)
.then(firstData => fetchSecondData(z, bundle, firstData))
.then(secondData => {
// process both sets of data here
});

this should work in zapier. hope it helps!