Returning only a few fields of a thing via an API call

Hi everyone,
Am struggling with what I expected would be something fairly simple to do. In my database I have two things named Collection and Section. Collection has 8 fields- relevant ones for this being name, a cover image and a list of sections. Sections also have multiple fields, but relevant ones are name and the collection they belong to.

I wish to use the API to do the following things:

  1. Return the names, ids and cover images of all the collections of a user. I wish to exclude the remaining fields because those are not needed. These will be shown in a dropdown at the place the API is called from (a separate app I am building using code)
  2. For each collection, return the ids and names of all the sections that belong to it. These too will be shown in a dropdown.

If I use the Bubble data API to get the collections or sections, then I also end up getting fields that I do not need and that seems to be a waste of network bandwidth for data which is useless and potentially large (I have a list of items in each section which can run into 100s). Is it possible to configure the API in a manner which returns only the required fields?

I have scoured the forum for answers on this, but can’t seem to find anything. Any help would be much appreciated. Tagging @keith @vini_brito @Jici @shpak.serhiy @kevin4 as I have seen you folks the most active on all the API threads I have read. Look forward to hearing from you guys and the rest of the community.

Akhil

If the API you are calling allow you to format the request to exclude field, you can do it. You need to read API doc for that. In Bubble, when you initialize call, you can choose to ignore field too. It’s not totally the same because the call will fetch them anyway, but Bubble will not pass them in frontend.

Dear @Jici,
Thanks for the super fast response. I think I may have not explained the situation completely. The API is called from a separate app I am building using code. I want Bubble to send only the required fields in response to this API call. I am not sure the solution you are suggesting applies?

Akhil

Sorry. I didn’t read correctly.
I think of two way to “manipulate” data output. First one is to use an API Workflow with return data from API instead of using an API Data call.

Maybe you can also use privacy settings to display only specific fields data by unchecking view all fields and select what you want to display

Dear @Jici,
Appreciate you taking the time on this. I don’t think changing privacy settings works as a solution because that will affect the rest of my app too where I do want the other fields to be visible. I thought of using the workflow API too, but my concern with that was that I will have to return data in separate arrays instead of a single json object and this might lead to some data mismatch. An example should make it clearer. Let’s say I have two collections named C1 and C2, with unique IDs ID1 and ID2. Ideally, what I want to return is of the form:
{
{Name: C1,
ID: ID1},
{Name:C2,
ID:ID2}
}
However, using the workflow method I will be forced to send something like
{
Names: [C1,C2],
IDs: [ID1,ID2]
}
This would be after configuring return data in a manner such as “Do a search for Current User’s Collections’ name” and “Do a search for Current User’s Collections’ Unique IDs”. Which in itself will not be a problem if the search results always follow a consistent order i.e. data of collection 1 always comes before data of collection 2. My concern is that results might be ordered in a manner such as [C1,C2] and [ID2, ID1]- so that C1 gets the ID of C2 and vice versa. Does that make sense? Am sorry if it is convoluted. I tried running a test on a small sample of 4 collections and the results seem to be in a consistent order so am going to try to implement this solution and hope it continues to be consistent.

However, I have now run into another unrelated problem. I just upgraded my app plan to personal and switched to a custom domain. The domain is set up properly and all calls get redirected to this new URL. However, the API calls which were working fine at myapp.bubbleapp.io are now failing with a 401 authentication error on myapp.io domain. I am using the session cookie to authenticate. Any idea why this might be the case? Thanks again for taking the time out to help.

Cheers
Akhil

Nevermind the authentication issue. It was a problem with permissions at the place I am making the API call from. Appreciate all your help :slight_smile:

Hey @akhilpuri2003, did you find a satisfactory solution to output only a subset of all fields in a list of Things?

Hey @sudsy,
I used the workflow API. Please refer to the screenshot below. The data returned is always well ordered and that will help you process it wherever you are calling the API from. For example in the screenshot below, let’s say I have 3 sections in my database- this API will return an object containing 3 arrays { [ID1,ID2,ID3], [Name1, Name2, Name3], [Collection1, Collection2, Collection3]}. Since the 1st item in array 1 maps to the first items in arrays 2 and 3 and so on, I can construct an array of objects with just the required data at the place I am calling the API from.

Hope this helps.

Cheers
Akhil

Thanks for the reply, @akhilpuri2003. See my comments below…

That’s one of the things I already tried. Unfortunately, it breaks when a field has no value, because it omits the field entirely instead of outputting an “empty” value for the field.

In the example below, for instance, no image has yet been uploaded for Product FOUR and Product FIVE.

{
    "status": "success",
    "response": {
        "title": [
            "Product FIVE",
            "Product FOUR",
            "Zen Garden"
        ],
        "description": [
            "This is the description of product 5.",
            "This is the description of product 4.",
            "This is the description of the Zen Garden."
        ],
        "image": [
            "//s3.amazonaws.com/appforest_uf/f1564373124819x142177157546424800/zen-garden.jpg"
        ]
    }
}

As a result, that solution will not work for me. I then thought that this cool new Bubble feature (which, as of this writing, has not yet been formally announced but is available in the editor) would make this super simple. And it does…

[{
   "title" : "Product FIVE",
   "description" : "This is the description of product 5.",
   "image" : ""
},
{
   "title" : "Product FOUR",
   "description" : "This is the description of product 4.",
   "image" : ""
},
{
   "title" : "Zen Garden",
   "description" : "This is the description of the Zen Garden.",
   "image" : "//s3.amazonaws.com/appforest_uf/f1564373124819x142177157546424800/zen-garden.jpg"
}]

The problem with this approach is that the Return as plain text option must be enabled, which means the endpoint does not return JSON. :frowning_face:

Depending on where the content’s being consumed, though, it might be as simple as applying JSON.parse()to the result.

Anyway, still no ideal solution, but this approach is closer to what I need. If you come up with a better way, please post back to this thread.

-Steve

It returns text/plain, but it’s still a valid JSON.


2 Likes

Thanks, @lottemint.md. Yeah, I know it’s valid JSON, but the response won’t have the proper Content-Type header. It’s good to know that Bubble will still recognize it as JSON, but I’m not sure all clients would recognize it as such. :confused:

When you make a request in node, you still need to use JSON.parse() method even if the Content-Type is application/json. So, I’m not sure if that’s an issue here. :slight_smile:

One issue that I notice that we cannot JSON stringify values here. :frowning:

Sure, but I suspect automation services like Zappier, Integromat, and the like will honor the Content-Type and thus not properly parse the response. I guess I’d have to do some testing to be sure.

Did you ever find a solution for this @sudsy?

I’m having the same issue with fetching plain-text data from Bubble using third party apps?

Hi @messly,

You might have to elaborate on your specific use case. If the third-party service allows you to control the data schema, Bubble’s data API might be an option. The best approach really kind of depends on the options available in the service that’s consuming your Bubble endpoint.

Hi, where does the last screenshot come from??

1 Like

Hi!

That’s from the API connector (when you initialize a call).

@lottemint.md in the last screenshot if ‘Return a plain text’ is checked on the workflow API, how did it come through in with the nice JSON format? if I choose JSON format in the API connector I get the error below, and if I choose text the response comes through in a big messy text (not parsed). Thanks in advance for the help if you see this!

1 Like

please ensure if the JSON is valid

Is there a way to return a nested array of within the array? Like in this json the categories:
Screenshot 2023-04-12 at 2.18.46 PM