Use API Rest Json Data from one query in another API Rest query

Hello

Im discovering Retool (this is awesome), Im trying to use API Rest Json Data from one query in another API Rest query variable and I got a null rather than having the data value.

Query 1

getMyData will feed getMyData.data.my_ids

Query 2

getDetailsFromIds need a variable input called myidsvar and need to be feed by {{getMyData.data.my_ids[i]}}

getDetailsFromIds will then query API Rest with https://mywebsite/api/v1/blabla?myidsvar=my_ids

but here my_ids is null

Is it possible to do this?

Hi @SoIntuitive, welcome to the community :waffle:

You might want to check your left panel and view how your getMyData.data is structured. Will this process be dynamic and would not require user input or you will run getDetailsFromIds if the prior query runs successfully?

For the latter, which I assume what you want, you have to be specific on how you call that ID from getMyData. If my_ids will return an array and only contains a single value, just use straight up on your second API query: https://mywebsite/api/v1/blabla?myidsvar={{getMyData.data.my_ids[0]}}

If this however contains multiple ids, then you have to use js to iterate over the values. Not sure what value type is accepted on your myidsvar parameter.

actually you are right I use https://mywebsite/api/v1/blabla?myidsvar={{getMyData.data.my_ids[i]}} in my second query.

Let me give more details.

From a search field, getMyData run each time the search field is updated (so each time you type or remove one letter), then getMyData query return data (data is updated each time getMyData has been run).

Then I select the data on the second query with {{getMyData.data.my_ids[i]}}. I have an array with multiple values inside. Let's say [1,2,3,4,5,6,7,8]. I can use these values in my list view and each block contain the good values, I use "i" there..

As I understand I can not use "i" in the query parameter value so I will need to use javascript and at which level I should use this and in which components or transformer exactly?

Do you have an idea about what js function I can use at the place I have to use js?

@SoIntuitive

Hey there, welcome to Retool :wave:

I want to make sure I am understanding your question about the parameter value. What are you looking to put here?

1 Like

Thanks for the welcome guys.

This is what I use there:
{{getHorsesDetails.data.horse_id[i]}}

When I use a fixed number eg: {{getHorsesDetails.data.horse_id[0]}} the value is passed through the query and it works but I need to get the value for all values in the array not only the first value.

But what Im trying to do is wrong cause all data will be based on 1 ID and what i would like is to get 1 query for each ID given to myidsvar but Im not sure if we can trigger the queries from the text field to display the final information.

Later the query will have other parameters
https://mywebsite/api/v1/blabla?myidsvar={{getMyData.data.my_ids[i]}}&anotherparam={{getMyData.data.name[i]}}&etc....

Is it possible to use an array containing multiple values as a query parameter value?

Depends on your API endpoint if it accepts an array and how it should be presented (got a quick read on this first part on how array is passed in query parameter). If you know how this is being handled, it might be easy to just string_agg your ids and pass on as a parameter or a bit cumbersome like iterate over your list of ids to run the API endpoint.

Hi Jocen, following your first answer, I have found something interesting in the doc.

I have finally created a new js query that just string ids and pass it as a parameter value.

2 things left now:

  • I need to run this automatically once is run another existing main/root query that run at page load(not yet searched how to do)

  • As I run 1 query per id, my textfield (used to finally show data) show the last query data. I can only use the last dataset fetched, I have no idea how to store them to my textfield or even to set them as value for each textfield of my listview before the next query is run. I'm currently trying to set the text field to display fetched values with {{getMyData.data.names[i]}} from the dataset getMyData.data but it just shows the last data from dataset of the last query.

The question is how to store multiple dataset resulting of multiples queries?

Appreciate your help guys. Thanks.

I got it working by addding a temporary variable then give it the i value of my js script. Once done i used this var in the event handler of my Rest query, it took me less time to research so I can say the learn curve is potentially fast... lets continue.

Thank you for your help both :pray: Can't progress without your help and hints .