Site iconAxway Blog

Integration Builder Flow as a Resource (FaaR): Part 1

In a prior blog post, we described how to set up a manually triggered flow and how the flow instance is exposed as an API in AMPLIFY Central. When the Central API is called by a client, the flow instance is triggered and a response is immediately sent to the client and the flow executes asynchronously.

FaaR (Flow as a Resource)

FaaR enables you to use the flow as an asynchronous API request with the flow building the response to the API request. After you update the formula to be used as a resource, you can make API requests to it at https://api.cloud-elements.com/elements/api-v2/<resourceName>.

As described above, the API will also be exposed in Central in order to govern it. Using a flow as a resource enables you to remove logic from your applications further. It also provides the ability to more efficiently chain requests together. You are basically using Integration Builder to create an API.

Create a FaaR Flow

We’ll follow the instructions on how to use Flow as a resource.

First, we’ll create a very simple flow to illustrate the steps for setting up the FaaR and then we’ll modify the flow to process a passed in parameter.

In future posts, we’ll implement a more complex aggregate API.

Create a FaaR Flow

done({
  statusCode: 200,
  result: "Hello"
});

Note that response has a status code and a result

At this point we are done without simple FaaR flow.

Create an Instance of the FaaR Flow

Now let’s create an instance so that we can call this API.

Test API in AMPLIFY Central

We discussed how manually triggered flows are exposed as API Proxies in AMPLIFY Central and the flow we just created is a manually triggered flow so let’s go to Central and find our API.

Note that the status is 200 and the result is “Hello” since that’s we coded in the prepareResponse JS Script step in our flow.

Reviewing the API doc in Central, you can see that we need to pass in a header, Elements-Formula-Instance-Id with a value of the instance id. In my example, the instance id is 417240. This is all already populated in the Central API doc as shown in the screenshot above.

The curl command is:

curl -X GET 'https://test-e4f77ce669fd0b1a016a22c82f8013c9.apicentral.axwayamplify.com/SimpleFaaR_sandbox_flow_417240-/greeting' -H 'Elements-Formula-Instance-Id: 417240'

Pass in a Parameter

Let’s see how we can pass in a parameter in the API call. It’s actually quite simple, we just append the query parameter to the URL as follows:

curl -X GET 'https://test-e4f77ce669fd0b1a016a22c82f8013c9.apicentral.axwayamplify.com/SimpleFaaR_sandbox_flow_417240-/greeting?name=Leor' -H 'Elements-Formula-Instance-Id: 417240'

with response:

“Hello”

Note that in the example above, I am passing in my name (Leor) in the query parameter name.

If we review the executions of our flow and click on the trigger step, we can see that the query parameters are in trigger.args.request.query as described here:

{
  "request": {
    "headers": {
      "accept": "*/*",
      "authorization": "User IPJGviY88318IZwhKWr3rIJ5sgXrxK6imXotv869NW4=, Organization 79eb34ad57c0465d21b1d6064fac2a05",
      "connection": "keep-alive",
      "elements-formula-instance-id": "417240",
      "host": "staging.cloud-elements.com",
      "user-agent": "curl/7.54.0",
      "x-axway-proxy-handler-count": "1",
      "x-axway-tenant-id": "100000142",
      "x-consumer-custom-id": "7861:18281:21107",
      "x-consumer-id": "48ae2b60-3694-4fcf-bbc5-e57afe27673f",
      "x-consumer-username": "lbrenman@appcelerator.com@18281.7861.generated",
      "x-forwarded-host": "staging.cloud-elements.com",
      "x-forwarded-port": "8000",
      "x-forwarded-proto": "http",
      "x-real-ip": "127.0.0.1"
    },
    "query": {
      "id": "1111111"
    }
  }
}

Let’s modify our flow to use this parameter.

Modify the code in the prepareResponse JS Step as follows:

done({
  statusCode: 200,
  result: "Hello "+trigger.args.request.query.name
});

Now when we make the API request below:

curl -X GET 'https://test-e4f77ce669fd0b1a016a22c82f8013c9.apicentral.axwayamplify.com/SimpleFaaR_sandbox_flow_417240-/greeting?name=Leor' -H 'Elements-Formula-Instance-Id: 417240'

the response is:

“Hello Leor”

Summary

In this blog post, we saw how we can use a Flow as a Resource (FaaR) and call it through AMPLIFY Central. We also saw how to pass in a parameter and use it to prepare the response. In future posts, we’ll implement a more complex aggregate API.

Exit mobile version