Show/hide layer depending on response from api interface asset

Hi all,

is there a best practice or nifty way to show or hide a layer, alternatively navigate to a new scene, depending on the response from a call to an api call (interface asset).

In my case I would want to show a layer if there is content (text) in the response from the call to the external api. If there is no content ("") I would like to skip-ahead to another layer in my experience by hiding the current layer.

/Fredrik

Hi @it15,

I recommend you to check this sample that may fit perfectly for what you’re trying to achieve: Giphy API

In the Experience, we make a request on Giphy API based on a Text Input value.

When the API returns an error or no results, we hide some layers and show others.
Feel free to download, test and dig in!

When requests failed (check the conditions) :

When no results (check the conditions) :

Regards,

Alex.

1 Like

In action it will behave like this:

Sweet, this is exactly my current approach so I will continue on this path.

Thanks!

1 Like

I have an issue with this approach that might be related to how api:s handle stuff differently.

The endpoint for my api request delivers a list, an array, containing a few objects. But sometimes this array is empty, like [], but still identifies as a completed request (http status 200 ok).

So my question is now: How can I create a condition in where I check if the array itself is empty rather than if an object in the array is empty?

In my first attempt I made a condition like this: "text of showcase_texts Equals but as text itself does not exist when the array is empty the condition does not seem to work.

Is it wrong or unorthodox for an api to return an empty array? Should it rather return a status 404?

Either way it would be interesting to know if it is possible to actually make conditions that check wether the array is empty or not. Perhaps some kind of count?

/F

That would be the best indeed if you’re the master of your API.
A count value would help you to set up 2 conditions, one with count==0 and one with count>0.

We provide this property on our Excel Interface Asset but for an API you have to implement it yourself.

Regards,

Alex.

I have tweaked the API so that the response is 404 when array is empty, this works.

1 Like