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!
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.
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.
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):
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.
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
<product_url_Value> is now the only thing I can access in the workflow:
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.
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.
@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)