Skip to content

Advanced APIs in Celigo

In one of our previous articles, an introduction to APIs in Celigo, we discussed the API capabilities in Celigo, and highlighted how exposing a flow using an API only runs the flow asynchronously. So how do we get over this? How can we have a full logic of retrieving data, massaging it, performing some logic, and finally sending this data to an external system, all exposed through a synchronous API? In this article, advanced APIs in Celigo, we’ll see how this is actually doable via the scripting capability that could be used to synchronously orchestrate calling multiple exports, imports, as well as performing any logic along the way.

In another article, expose an export as an API in Celigo, we have seen how to create an API and write a script to invoke an export synchronously to retrieve some data and return it back in the API response. In this article we’ll use a similar tactic, however our script will do much more to orchestrate calling multiple exports and performing some logic to amend the information retrieved by the exports.

Our API will receive fulfillment information in the request, will internally call an export to lookup further information for the sales order, then will call another export to retrieve inventory information for each fulfillment item, aggregate all information, and return the final result back in the API response. Here’s a sample request

[
   {
     "OrderNbr": "ORD874856",
     "SKU": "546564",
     "Item Description": "450ml pepsi bottle",
     "Quantity": "3"
   },
   {
     "OrderNbr": "ORD984994",
     "SKU": "676586",
     "Item Description": "1000ml sprite bottle",
     "Quantity": "2"
   }
]

Let’s start.

Step 1: Create a Script

First step is to create a script that would contain all of the orchestration logic. In short, the script iterates over all incoming fulfillment records, for each record calls an existing export to retrieve the sales order details and amend some attributes to the item fulfillment record, then calls another export to retrieve the inventory details and only add them if they have the same location. To do that, add the following code to the script object. We have included comments to explain what the script is doing exactly

import { exports } from 'integrator-api'
function handleRequest (options) {
  // Declare variables
 let response = {};
 let statusCode = 200;
 let invokeExportResponse;
 let exportResponse = options.body;
 let tempResponses = [];
 let tempInvResponses = [];
 let currentRecord;

 try {
   // Get the incoming request
   let request = options.body;

   // Iterate over each fulfillment item
   for (var i=0 ; i < request.length; i++) {
     // Invoke the export which receives the sales order details, in order to add extra fields to our incoming data
     invokeExportResponse = exports.run({_id:'***<export_id>***', data: request[i]});
     exportResponse = invokeExportResponse.data;

     // Amend extra details to the original fulfillment record
     request[i].internalId = exportResponse[0].id;
     request[i].location = exportResponse[0].Location;

     // Call another export to retrieve inventory details and add them to the original object if are in the same location
     invokeExportResponse = exports.run({_id:'***<export_id>***', data: request[i]});
     exportResponse = invokeExportResponse.data;
     // iterate over inventory details and add them to the fulfillment record if they have the same location
     for (var j=0 ; j < exportResponse.length; j++){
       currentRecord = exportResponse[j];
       if (currentRecord.Location == request[i].location) {
         currentRecord = {
           "id": currentRecord.id,
           "bin": currentRecord["Bin Number"],
           "lotNumber": currentRecord["Inventory Number"]
         }
         tempInvResponses.push(currentRecord);
       }
     }
     request[i].inventoryDetails = tempInvResponses;
     tempResponses.push(request[i]);
   }
   statusCode = 200;
 } catch (e) {
   exportResponse = JSON.stringify(e);
   statusCode = 500;
 }
  // Create body response
 response.body =  tempResponses;
 response.statusCode = statusCode;

  return {
   statusCode: response.statusCode,
   headers: { },
   body: response.body
 }
}

From here on, we follow the standard steps needed to create an API, as explained in detail in our previously published article, expose an export as an API in Celigo.

Step 2: Create an API

Second step is to create an API which calls our previously created script, as follows:

Advanced APIs in Celigo

After saving the API, the API endpoint is automatically generated, keep it handy as we’ll use it to invoke the API later.

Advanced APIs in Celigo

Step 3: Create an API Token

Third step is to create a token to secure our API. 

Advanced APIs in Celigo

Once the API Token is created, you can get actual security token from the create token displayed in the tokens list, as shown below

Advanced APIs in Celigo

Testing the API

Finally, we can head to Postman and attempt to call the API. First we have to set the authentication header using the token we generated in the previous step

Then we can call the API using its generated endpoint. You should be able to view the additional data amended to each fulfilment record.

Conclusion

Despite the fact that Celigo doesn’t support exposing a flow synchronously out of the box as of now, we have seen in this article a tactic to implement such functionality using the advanced scripting features of Celigo. So, if you need such a requirement in your projects, don’t stress out, you can still implement APIs with advanced logic in Celigo using this approach. That’s it for now. Happy learning!

Leave a Reply

Discover more from ihub4us

Subscribe now to keep reading and get access to the full archive.

Continue reading