Create API workflow to calculate or view data

Hi all,
I am trying to create API workflow that do either the following:

  1. Calculate (simple calculation), without saving anything to a database. The formula will be simple like this:

x,y,z are parameters
var 1 = x * 5
var 2 = y * 10
var 3 = max(x, y) get the maximum from either var 1 or var 2
var 4 = var 3 + z

  1. Do a search from a table with filter based on parameters.

When I tried to create API workflow, the options are usually to ‘do’ some actions (create/delete data, emails, etc.). I just want to run a query or do calculations.

Once the API are created I will call it from DropSource. and if possible can I also call it from the same bubble app?

Please advise. Thanks!

Ah i see what you’re trying to do.:smiley:
Because you can’t do calculations in dropsource you’re trying to create an workflow endpoint in bubble and call it from dropsource.

For now i’m going to ignore the var4=var3+4. This is because you can’t directly use the return value of the api call in another return value. You could replace var3 in the var4 so it becomes (max (x,y)) + z. But again you can’t use parenthesis in expressions for now in bubble. But there are several workarounds including using the javascript plugin or saving the intermediate value in a custom state. But lets leave that for another time and go to the main issue.

So here is one way to achieve your goal:

  1. Create a worfklow endpoint in bubble, lets call it calculate.

  2. Create you parameters, x,y,z, and choose number as their types like below

  3. On the endpoint workflow action, select Data (Things), then Return data from API as shown below:
    image
    4.Define the values you want to return from your api. Here the var1, var2 and var3. Their types should be numbers in this case. Once you add a parameter you will get a field below it (see var4 in the snapshot below) where you can enter your expression. The parameters (x and y) that you defined in the Step 2 above will be available for you to create your expression.

  4. Now when you call your workflow endpoint at /api/1.1/wf/calculate from dropsource with the parameters say x=2, y=3, you will get a response like below:

     {
         "status": "success",
         "response": {
             "var1": 10,
             "var2": 30,
             "var3": 3,
         }
     }
    

As i mentioned earlier for the var4 expression you can search the forum to find out how to do calculations involving parenthesis. There are plenty discussion and solution on this.

1 Like

For your second question you can do a similar thing but here instead of doing calculations and returning the result you will just do your search (as you will do in any normal bubble search) using the parameter sent to the endpoint (that you define when creating the endpoint) as your filter and return the result.
Both are very similar with the only difference being the expression you write (here one is calculation expression and the other is a search expression)

By the way for performing calculations you can also use the maths.js api.
You can create a swagger specification using stoplight (see dropsource documentation for instructions on this) and use it in your dropsource application.
If the values you’re using for your calculation will not be coming from your bubble backend, i think using the math.js api will be easier for complex calculations.
The caveat is that the maths.js api expect the expression to be a string and as you may be aware you can’t do string concatenations (joins) in dropsource Android projects. You can do it in IOS projects though.

Good luck

Thanks @seanhoots. It’s almost there. I typed the formula incorrectly.

var 3 should’ve been max(var1,var2) instead of max(x,y)

So when I wanted to create var 3 using value from var 1 and var 2, I could not find the variables.

Looked math.js.api, looks interesting but it doesn’t seem to be able to take parameters? it appears that it’s for straight calculations?

the maths.js api definitely takes a parameter. at least it requires the expression. as i said the expression parameter has to be submitted as a single string and because you can’t do string concatenation in droupsource (android) you might not be able to use it.

for the var3 calculation just search the forum for how to do complex calculations involving parenthesis. as i mentioned previously you can’t use the results of the returned type in the same call. so basically what you’re looking to do for var3 is max( (x5), (y10) ).
there are several posts on this. you can use the toolkit plugin and do all the calculations in javascript and get the results into your api return data.

Ah, so that’s what you meant :slight_smile:

That sounds too advance for me. I’ll get back to this again as I have been working on other parts of the app. Thank you for your guidance.

Have you solved this point ? Have found a way to retrieve the result in your bubble app ? I am insteresed in :grinning: