Tags

, ,

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.

Advertisements