Hitting multiple APIs and accessing returned data

I have a set of 3 separate APIs that I'm trying to hit to build a table of data.

  1. Get list of stations
  2. Get tags for those stations
  3. Get names for those tags

I've gotten as far as 1 & 2 using a JS query that goes through every row of a table but am having trouble passing the right data to the 3rd API as it's an array.

First, is it best handling all this in a single JS query? Where I go through a table of #1's data and hit #2 and #3 APIs?
I've tried going down this path but am having trouble getting to the data returned by the APIs. In the following code, I'm hitting #2 and have a single row data stored in 'result' but I can't seem to get into the data to pass to the 3rd API. I'd like to just get to the array of "tags" and use that to pass to the 3rd API.
Also, if I go down this path, how do I combine API results from within the JS?
If there are any other suggestions, would definitely appreciate it!

//const arr = get_audio.data.data //your array of things to iterate over
const query1 = get_tags_by_station // your query to trigger for each

const promises = table1.data.map(row => {
  const result = query1.trigger({
      additionalScope: {
        publisher_id: row.publisher_id, //pass publisher_id to query
        station_id: row.id //pass station id
      }
  })
  return result
});

return Promise.all(promises)

image

Hi @mikec thanks for sharing this here. I'll poke around and see if I can help point you in the right direction. In the meanwhile, have you consulted our docs on APIs (here and here)?

Thanks for responding @alina.retool . I was able to solve this with some help from one of our devs. I got stuck with hitting API limits (too many at a time) and ended up batching with the JS below.

const query1 = get_tags_by_station // your query to trigger for each

const partitionSize = 250; //for reference, API didn't like ~1300 when doing it all at once.
const partitions = [[]];
var currentPartitionIndex = 0;
table1.data.forEach( row => {
   if( partitions[currentPartitionIndex].length >= partitionSize ) {
       partitions.push([]);
	   currentPartitionIndex++;
   }
   partitions[currentPartitionIndex].push(row);
} );

var results = [];

for( let i = 0; i < partitions.length; i++) {
    let partitionPromises = partitions[i].map( row => {
//  console.log('Running query for id', id);  
      return query1.trigger({
        additionalScope: {
          publisher_id: row.publisher_id, //pass publisher_id to query
          station_id: row.id //pass station id. would be available inside {{station_id}} in the target query when run
        }});
    });
	let partitionResults = await Promise.all(partitionPromises);
	results = results.concat(partitionResults);
}

return Promise.resolve(results);
1 Like

Glad to hear you were able to solve it! Please reach out if there are any other issues. :slight_smile: