• About

On Technology

~ SOA and Integration blog

On Technology

Monthly Archives: April 2018

Zapier – Beyond the basics – Part II : POST data to an external API

21 Saturday Apr 2018

Posted by Padmarag Lokhande in Integration, Zapier

≈ Leave a comment

Tags

Zapier

In the last blog, I explained how it is possible to make custom calls to external API’s to fetch data. In this blog I’ll show how you can post data to an external API via a POST action.

First we add a “Code by Zapier” step as usual and then map required inputs. refer to my last blog for details.

Next we go the the actual ode section and add something like this :


var line_amount = parseFloat(inputData.line_amount);
var tax_amount = parseFloat(inputData.tax_amount);
var customer_id = Number(inputData.customer_id);

console.log("line_amount : " + line_amount + ", tax_amount : " + tax_amount + " , customer_id : " + customer_id);

var invoice =
{
"Line": [{
"Description": inputData.description,
"Amount": line_amount,
"DetailType": "SalesItemLineDetail",
"SalesItemLineDetail": {
"ItemRef": {
"value": "3",
"name": "Product"
},
"UnitPrice": line_amount,
"Qty": 1,
"TaxCodeRef": {
"value": inputData.tax_code
}
}
}],
"TxnTaxDetail": {
"TxnTaxCodeRef": {
"value": "3"
},
"TotalTax": tax_amount,
"TaxLine": [{
"Amount": tax_amount,
"DetailType": "TaxLineDetail",
"TaxLineDetail": {
"TaxRateRef": {
"value": "3"
},
"NetAmountTaxable": line_amount
}
}]
},
"CustomerRef": {
"value": customer_id,
"name": inputData.customer_name
}
}

console.log("Invoice :" + JSON.stringify(invoice));

var options = {
headers:{
"Authorization": "Bearer " + inputData.access_token,
"Accept":"application/json",
"Content-Type":"application/json"
},
method : "POST",
body : JSON.stringify(invoice)

};

fetch('https://quickbooks.api.intuit.com/v3/company/123123123/invoice',options)
.then(function(res) {
console.log("response : " + res);
return res.json();
})
.then(function(json) {
var output = [{error: json.Fault, invoice:invoice, response:json}];
callback(null, output);
})
.catch(callback);

The important part to focus is the “options” passed in to fetch command where we specify the POST method, Headers and the JSON.stringify for body payload when body is of type JSON as is in the above case.

Lastly, Zapier uses fetch which uses a promise based design to process results, so we have the bunch of action->then->then code.

Enjoy!

Advertisement

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to email a link to a friend (Opens in new window)
  • Click to print (Opens in new window)

Like this:

Like Loading...

Zapier – Beyond the basics – Part I : Fetching data from external API

06 Friday Apr 2018

Posted by Padmarag Lokhande in Integration, Zapier

≈ 1 Comment

Tags

api, code, Zapier

Zapier is an excellent tool for Integration for Small and Medium Business. It fills 80% of the needs for most of the scenarios.

However there are a few scenarios which require you to go out of the box that zapier provides. One such scenario is interacting with an API which is not available in Zapier or which has exposed only limited operations in their official Zapier App.

Thankfully Zapier has provided the tools to do so. In these series of posts, I’ll be covering some common use-cases for Zapier.

The primary tool or component for interacting with external API is the “Code by Zapier” component.

You can add this as an Action step.

1

I typically choose “Javascript” which is essentially Node JS.

2

Any data from other steps that you want to pass in can be added here and it’ll be available under “inputData” object. So if you add a property “first_name” then it’ll be available in code as “inputData.first_name”

3

And under the code section, you can include your actual code like

fetch('http://example.com/')
  .then(function(res) {
    return res.json();
  })
  .then(function(json) {
    var output = {id: 1234, response: json};
    callback(null, output);
  })
  .catch(callback);

Zapier uses Node Fetch module to get data. It is a promise based library and requires some then, callback logic.

This is a simple example to call “GET” operation on any API. If you need to pass some headers, do it as :

var options = {
 headers:{
 "Authorization": "Basic Abcdesjjfjfj="
 } 
};
fetch('https://us2.api.mailchimp.com/3.0/campaigns/1a2s3d/content',options) 
    .then(function(res) {
        return res.json();
    })
    .then(function(json) {
        var html_content = json.archive_html;
        var output = {"content":html_content}; 
        callback(null, output);
    })
 .catch(callback);

In the next posts, we’ll go into some more complex scenarios including use of OAuth based API and POST using Zapier.

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to email a link to a friend (Opens in new window)
  • Click to print (Opens in new window)

Like this:

Like Loading...

Subscribe

  • Entries (RSS)
  • Comments (RSS)

Archives

  • April 2020
  • February 2019
  • April 2018
  • July 2015
  • July 2013
  • October 2012
  • June 2012
  • May 2012
  • September 2011
  • April 2011
  • March 2011
  • December 2010
  • August 2010

Categories

  • Camel
  • Database
  • Devops
    • Amazon AWS
    • Docker
    • Kubernetes
  • Integration
  • Java
  • JMS
  • MuleSoft
  • Oracle
  • Siebel
  • SOA
    • BPEL
    • REST
  • Uncategorized
  • Zapier

Meta

  • Register
  • Log in

Blog at WordPress.com.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
  • Follow Following
    • On Technology
    • Already have a WordPress.com account? Log in now.
    • On Technology
    • Customize
    • Follow Following
    • Sign up
    • Log in
    • Report this content
    • View site in Reader
    • Manage subscriptions
    • Collapse this bar
 

Loading Comments...
 

    %d bloggers like this: