Get Data from DB or call API if entry is empty


I’m trying to show the distance a building has to certain locations (that I have saved on DB), for this I’m using the Google Map’s API distance and duration.

The problem is that every time someone enters this page an API request is made to get the distance and duration of each interest point, which consume almost all my API request credits. To solve this I was trying to save this information in my DB because it is static and shouldn’t change over time.

So I was wondering if there is a way to get the data from my DB and show it, if this data doesn’t exist call Google Map’s API distance and duration, show this data and also save it to DB so in the future I can call it from there.

The flow is something like this:

if do search for is not empty:
text = do search for …
text = Google Map’s API distance and duration …
create new thing

Hope someone can help :slight_smile:

This is a possible solution, but I don’t know if this will work for your case.
Create a DB and store the API response in one field with the type of the API (when you create an API Data request, Bubble will also create a “type” (or more than 1 when there’s list) of the API (named the same way that you name the API endpoint)
The data source will be the same type using DB field or API Connector. So it should work to change the data source according to filter

It could work but I’m not sure how can I save it based on the condition. Like what workflow do I use because I need to do it for each element in a recurring group, I can make the API call but I can’t find a way to save the info to DB. A hack could be when I click the element create a new thing based on the element’s info but this is less than ideal since I would need to go property by property clicking all interest points, so I need to do it on load or something like that :confused:

I solved it, for anyone wondering I did it by creating a backend workflow which calls the API directly with API connector, unfortunately the plugin I was using didn’t have this action so I had to do it manually.

The flow was the following, on page load → send list of interest point to backend + the specific property → if interest point + property wasn’t on DB → Call API → with the API’s results create entries on DB.

Then on the page I did a condition to show the entries on DB if they existed and if they don’t exist to call the API and display the data. So first time I load the page all entries are created on DB and shown (by doing an API request), next time the entries are shown doing a DB request instead of an API request which limits the amount of requests done.

1 Like