šŸ˜ƒ Dynamic data - including lists - in body of API post requests - POSSIBLE!

Perhaps this is common knowledge and Iā€™m just a little slow on the uptake, but I recently learned something that completely changed my understanding of the API Connector (and API tab of the plugin editor).

Just above the Body field, it readsā€¦

  • Body (JSON object, use <> for dynamic values)

I had always thought it meant that angle brackets could be used only within the context of individual property values. However, itā€™s actually much more flexible than that!

What it really means is that angle brackets can be used anywhere within the body of the request and that any such bracket-enclosed text will appear as a field in the property editor so that its value can be specified using the dynamic expression builder.

So basically, Bubble simply runs pattern recognition and substitution on the entire body text field and converts all bracket-enclosed entities into dynamic expression fields in the editor!

What this means isā€¦

The following API configā€¦


 
 

ā€¦produces the following editor experienceā€¦


 
 

ā€¦which results in the following request bodyā€¦

endpoint
 
 

But it also means the following is perfectly validā€¦


 
 
Thatā€™s right. The entire body of the request can be constructed using a single dynamic expression! (ā€¦although the need for just a single field would probably be infrequent)

Anyway, coupled with the recent :formatted as text operator for lists, this means that literally anything - including dynamic lists and objects of arbitrary complexity - can be supplied as part of an API request body.

Perhaps this is old news to some, but it was an eye-opener for me.

-Steve

23 Likes

1 Like

Hey @sudsy ! This was exactly the info I needed, thanks!

I have one question though, what did you put in the Value box of The List

in order to be able to initialize the API call in Bubble?

In my case, my list (lista_de_items) is a list of things. What can I put in the Value box in order to initialize my API call?

It just needs to be an array in valid JSON format. Just one item should suffice, but it has to be an array. In my case, it was an array of strings (Bubble Text types)ā€¦

["my string"]

In the real world, you might want to run each string in your JSON through the :format as JSON-safe operator.

-Steve

1 Like

Thanks for your reply!

Any idea of what should I type to initialize the call if it is an array of things (A Bubble datatype thing)?

Solved. Typing a unique id did the trick. Thanks!

1 Like

Amazing post - thanks

1 Like

Hi there!

Iā€™m having a tough time figuring this out as well and am hoping you know this/its a simple mistake.

If I add this API call, it works fine and I can access the entire body in a workflow:

Having the entire JSON body in a workflow window seems a little messy and sometimes the ā€œTemplateModelā€ changes (e.g., ā€œproduct_urlā€ isnā€™t used in some cases). So I was hoping to get TemplateModel into a Bubble API ā€œParameterā€ but canā€™t figure out how to do it. Among many others, this is what Iā€™ve tried (in a different, copied call):

which contains:

["product_url": "product_url_Value", "product_name": "product_name_Value"]

But the call wonā€™t initialize (the error is specific to this Postmark API: 422: ā€œErrorCodeā€:403,ā€œMessageā€:ā€œInvalid request field(s): $.TemplateModelā€).

Iā€™ve searched far and wide for an answer to this question:

Is there a way to get just this object array (and not the entire API body) into a single parameter I can use in a workflow? Basically, Iā€™d really like to just have to ā€˜format as textā€™ output for this single array/piece of the call (and not the entire call) in the workflow.

Hope that makes sense and appreciate any help!

Dynamically this should look like
Current product url is not empty:formatted as text
in formatted as text yes = ā€œproduct_urlā€:ā€œThis productā€™s url(dynamicā€, no = (empty)
OR
ā€œproduct_urlā€:Current productā€™url is empty:formatted as text
in yes = ā€œThis product urlā€ in no = null (most API will accept null for theses case

Hi @Jici and thanks for your super quick response!

I was hoping for a slightly different piece of the puzzle. Maybe this is a bit clearer description.

Currently, with no variables defined in the API Connector, the workflow action is this:

But if I define even a single variable in the API Connector, I only get that variable in the workflow:

<product_url_Value> is now the only thing I can access in the workflow:

Screenshot 2024-06-13 at 2.20.19 PM

What I was hoping is that I could define all the parameters in the API Connector, so I could have them all presented in individual inputs in the workflowā€™s dialog. I.e., in an ideal world (instead of the dummy ā€œproduct_url_Valueā€), that last screenshot would contain inputs for:

  • ā€œFromā€
  • List item
  • ā€œTemplateAliasā€
  • ā€œTemplateModelā€

And the ā€œTemplateModelā€ input would be the one where I could enter the array data, formatted as you see above in the ā€˜rawā€™ body.

Does that make sense? Iā€™m not trying to format the array inside the workflow yetā€“just get a field available to do it on one array/without having to do the entire request body.

In this case, and if accepted by the API server, only set: Itemā€™s url:is not empty:formatted as text
YES = "Item's url" (including double quotes) NO = null

Actually, you are not showing array but object (TemplateModel). Is ā€œproduct_urlā€ an array expected?
In this case, API connector should be set to "product_url":[<product_url_value>] and dynamic value should use: list of url:formatted as text, top box = ā€œthis urlā€``` and delimiter set to comma. Most API will accept an empty array if thereā€™s no url

Yeah, really cool huh. Bubble is very powerful, most people donā€™t even know the true potential.

Another tip: Use Arbitrary Text to build out your JSON in your workflow. So you can build out the JSON nicely. And of course, lists should be :formatted as text.

This helps when trouble shooting if your next step is a ā€˜dev logā€™ and pass along the arbitrary text. Then run it through a JSON validator to find the issues. :blush:

Hope that helps! :raised_hands:

Hi,

So I figured it out! Iā€™m sorry if my description were confusing, but this is what I was trying to accomplish:

Which then allows the workflow to look like this:

This gives me input fields in the workflow for both the individual parameters and the TemplateModel array, meaning I only have to worry about building/formatting the JSON in the TemplateModel workflow input field.

In previous attempts I was using [ ] instead of { } around <TemplateModel> in the API Connector, which did not work.

But thank you for the help!

2 Likes

Hi bro @pachocastillosr ,

How did you use the unique id? Is it given by bubble.io or we need to manually insert it? As I am having the same issue

Update:

I found the solution after going through above conversation.

image

Just need to add " " between the

1 Like

Thanks! Saved me i dunno how much time :slight_smile:

@ariffhakimichik Iā€™m using a backend workflow to loop over my list of users and build a JSON payload, then delivering it to the PDF api iā€™m using (docmosis) (SO EASY, highly recommend them)

This returns a single PDF file with a set of 2 documents per user in my list, each set is customized to each user in my list (so if i have a list of 10 users, iā€™m getting 20 documents back)

1 Like