Array of parameters for an API

So i’m using an API connector to fetch data from the USDA API to fetch a list of nutrients for a given food item. Now i need a list of 4 nutrients, 208-Energy, 269-Sugars, 204-Total Lipids and 205-Carbohydrates. I’ve passed the nutrients parameter as a shared parameter, and 4 such parameters with the same name with different values.

And the API is only fetching one nutrient value.

My guess is that i need to pass these values as an array. Can anybody help me in specifying how i can do that?

1 Like

Well you’d mock it up (initialize the API call) by sending something like:

Key: nutrients Value: 205, 204, 208, 269

This will usually work if nutrients is expecting an array. Try that first.

If the API is poorly written, it might be expecting a different syntax for an array versus a single value. In that case, it could be expecting (case 2):

[ 205, 204, 208, 269 ]

or even worse (case 3):

[ "205", "204", "208", "269" ]

At any rate, there are ways to handle turning a Bubble list into what the API is expecting when it comes time to pass data via a “Get data from an external API…” workflow step.

In the case of case 1 you’re golden. In cases 2 and 3 I bet you’re going to be back with another question (“how do I make my list look like my api needs?”). Let’s burn that bridge when we come to it. :wink:

Hey Keith, i’ve tried all the methods you’ve mentioned here. This is the error i get from the API.

I’m not sure what else i can try.

From the API documentation:

CURL: curl -H "Content-Type:application/json" -d '{"nutrients":["204","205","208","269"],"max":25,"offset":0}' DEMO_KEY@api.nal.usda.gov/ndb/nutrients

I don’t have time to try it now and but the above may help Keith or others to assist you.

Well, there you have it. OP should initialize their call like that. Bracket delimited array of strings.

{“nutrients”: [“204”,“205”,“208”,“269”],“max”:25,“offset”:0}

^^^ ok, so there’s your body payload for your call, right? So in the body section of your API setup, you transform this to:

{"nutrients": ["<nutrients>"],"max":<max>, "offset":<offset>}

And then the key/value pairs will magically show up. And you fill them in like:

nutrients 204”,”205”,”208”,”269
max 25
offset 0

(I have no idea if max and offset are required or optional — read your API docs, right? Could that have been your problem, ? You never showed us a screenshot of your API Call setup…

Now, I know the formatting on nutrients looks weird but this is the correct way to do it. Once your call is initialized, and you need to pass a list of nutrient values to your api call, you’ll pass it like:

My-nutrient-list:join with “”,””

And that will give you the correct final result. I told you this was the funkiest option.

But you’ll note that configured this way your calls will work correctly even if the nutrient list is one item and you don’t have to type those stupid quotes yourself. :wink:

Also, @prajnyabaliga3186, is this a GET (I’m guessing so) or a POST? Sorry, I’m bad at reading curl options and I’m in my phone.

Anyway, I think you get the idea now.

This site takes GET or POST calls.

If using a GET, the parameters should all be query parameters, each item in the list being another parameter of the same name.

If using a POST, you can still use query parameters, or you can use a body text to send JSON instead, instead of using parameters. This only works if you add the header Contnet-Type: application/json.

Here’s an example, from their v2 interface …

document at … https://ndb.nal.usda.gov/ndb/doc/apilist/API-FOOD-REPORTV2.md

API connector screenshots

1 Like

This is a GET one. So this same API call works fine on the Postman app, some issue with bubble while passing multiple nutrient values

Yes this a GET call so, the nutrients i’ve sent as multiple parameters of the same name. Works fine on postman, some issue with bubble