API Builder 3.0 – API First Payload Optimization Example

0 Flares 0 Flares ×

In this third blog post in the series, we’ll create an API that returns an array of records from an API Builder Salesforce Account model. Then we’ll show how to optimize the payload by reducing the number of fields returned. While we can easily do this via model-first API development and this has been covered in prior blog posts, this time we’ll accomplish the same goal using an API First approach and use the API Builder Flow Editor so we’ll get more exposure to flow-nodes and dot.js templates.

The API I am creating is called as follows:

GET /myaccount

and returns an array of accounts in the following format:

[
  {
    name:
    type:
    phone:
  },
  {
    name:
    type:
    phone:
  },
  ...
]

The Axway Salesforce connector Account Model returns all fields for an account as follows:

{
  "id": "001i000000PscewAAB",
  "IsDeleted": false,
  "Name": "GenePoint",
  "Type": "Customer - Channel",
  "BillingStreet": "345 Shoreline Park
Mountain View, CA 94043
USA",
  "BillingCity": "Mountain View",
  "BillingState": "CA",
  "BillingAddress": {
    "city": "Mountain View",
    "country": null,
    "geocodeAccuracy": null,
    "latitude": null,
    "longitude": null,
    "postalCode": null,
    "state": "CA",
    "street": "345 Shoreline Park
Mountain View, CA 94043
USA"
  },
  "ShippingStreet": "345 Shoreline Park
Mountain View, CA 94043
USA",
  "ShippingAddress": {
    "city": null,
    "country": null,
    "geocodeAccuracy": null,
    "latitude": null,
    "longitude": null,
    "postalCode": null,
    "state": null,
    "street": "345 Shoreline Park
Mountain View, CA 94043
USA"
  },
  "Phone": "(650) 867-3450",
  "Fax": "(650) 867-9895",
  "AccountNumber": "CC978213",
  "Website": "www.genepoint.com",
  "PhotoUrl": "/services/images/photo/001i000000PscewAAB",
  "Sic": "3712",
  "Industry": "Biotechnology",
  "AnnualRevenue": 30000000,
  "NumberOfEmployees": 265,
  "Ownership": "Private",
  "Description": "Genomics company engaged in mapping and sequencing of the human genome and developing gene-based drugs",
  "Rating": "Cold",
  "OwnerId": "005i0000001iEVhAAM",
  "CreatedDate": "2013-09-30T21:36:31.000Z",
  "CreatedById": "005i0000001iEVhAAM",
  "LastModifiedDate": "2016-10-28T08:40:21.000Z",
  "LastModifiedById": "005i0000001iEVhAAM",
  "SystemModstamp": "2017-11-17T11:30:34.000Z",
  "LastViewedDate": "2017-11-16T21:57:50.000Z",
  "LastReferencedDate": "2017-11-16T21:57:50.000Z",
  "lb_sftest__CustomerPriority__c": "Low",
  "lb_sftest__SLA__c": "Bronze",
  "lb_sftest__Active__c": "Yes",
  "lb_sftest__NumberofLocations__c": 1,
  "lb_sftest__UpsellOpportunity__c": "Yes",
  "lb_sftest__SLASerialNumber__c": "7324",
  "lb_sftest__SLAExpirationDate__c": "2013-04-28T00:00:00.000Z"
}

Find All (No Optimization)

Let’s start simple and simply return the data just as defined in the Account model. Assuming that we installed the Salesforce connector in our API Builder project and expose the Account model and API, then there will be an account model in our Flow Editor in the models section.

If you don’t recall how we get started with API-First API creation, refer to the prior two blog posts: part 1 and part 2.

The following screen shots illustrate the simple flow and contains two flow-nodes: account model and HTTP as shown in the screen shots below:


The important points in the screen shots above are:

  • Select FindAll as the method for the account model flow-nodes
  • Create an output for the result (next) of the FindAll. I called it $.accountresponse
  • Use $.accountresponse as the body of the API response

Once we save the flow, we can test the API as shown in the screen shot below:


Payload Optimization

The Salesforce account model has too many fields; we only need 3: “Name”, “Type” and “Phone”. Furthermore, we want these to be lower case in our response: “name”, “type” and “phone”.

Continuing from the example above, we are going to insert a compose flow-node after the account model flow-node. In the compose flow node, we’ll use a dot.js template that traverses the array of accounts and selects the three desired fields from each.


As previously mentioned, the key point above is the dot.js template in the Payload Optimize compose flow-node. This dot.js template to optimize (reduce) the output from a model FindAll is shown below:

{{var accounts = JSON.parse(JSON.stringify(it));}}
[
{{~accounts :account:index }}
    {{? index > 0}}
    ,
    {{?}}
    {
        "name": {{=JSON.stringify(account.Name)}},
        "type": {{=JSON.stringify(account.Type)}},
        "phone": {{=JSON.stringify(account.Phone)}}
    }
{{~}}
]

What does this dot.js template code do?

  • The first line converts the list of models objects into plain data objects, accounts, which is an array of account objects
  • Then, we format the template so that it generates valid json. The square brackets, [], enclose the array the template will write
  • {{~accounts :account:index }} iterates over the array – account is the current element, and index is its index
  • Then, I check for index > 0 in a conditional section {{? index > 0}} and add the , separator
  • Then, we simply construct the json formatted fields

Once we save the flow, we can test the API as shown in the screen shot below:


Conclusion

In this post, we saw how to take a model’s output and return it directly. Then, we looked at extending this by performing a payload optimization and transformation (of field names). This was achieved with a small dot.js template.

In future posts in this series, we’ll continue to demonstrate features of API Builder 3.0, API-First API Creation and look at mode flow-nodes. Stay tuned.

Find previous posts in this series here:

0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×

Sign up for our blog!

computer and tablet showing Appcelerator software
Start free, grow from there.
0 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 0 Email -- 0 Flares ×