Transforming Webhooks responses with Zapier

  • 31 March 2017
  • 0 replies
  • 519 views

Userlevel 7
Badge

Looker’s scheduled webhooks have limitless applications. One powerful way to use them is with Zapier.


Zapier allows you to mash together modular blocks of pre-defined functionality in a point & click way. This mashup is called a Zap, and once you build it in Zapier’s UI, if you select webhooks as the “trigger”, the Zap will have a URL that you can paste into Looker’s schedule to kick off the Zap whenever the schedule runs.


But what if the JSON output webhook is not exactly what you need? Maybe you have [{"service.tier":"premium","orders.status":"complete", "orders.count":500}, ...], but you want {"premium":{"complete":500},...},...} . This will walk you through setting up a transformation step using arbitrary javascript.


We’ll start at https://zapier.com/developer/builder/ . From here, “Create a New Web Builder App”. Note that here we are creating an App (containing one Trigger) that you or others can then use as one component of a Zap, rather than a Zap itself.


For now, we’ll skip Authentication since the URL endpoint of the webhook can simply be kept secret.


The first important part of your app is the Trigger definition. First fill out the name and metadata, using any “key” you want, but note the key because we’ll use it in a later step. After the basic Trigger metadata, Zapier will ask you to set up Trigger Fields. These are essentially parameters that will be prompted to the person that builds a Zap from this Trigger. For now, skip this step, as it will make more sense once you have written your code. The last thing to do is to specify the source of the trigger - “Static Webhook”. Once you’re done, you should have something like this:


Next, scroll down to Scripting, and click Edit code. You’ll be taken to a code editor. The full documentation is here, but for our webhook scenario, we just need to declare one property on the Zap object, whose name should be the key from our Trigger + _catch_hook , like so:


The property should provide the function that we want Zapier to run on the incoming webhook request. This sample code below takes an array of records and transforms them into a hierarchical object:


var Zap = {
as_object_catch_hook: function(bundle) {
var byKeys=(
bundle.cleaned_request.querystring.by ||
bundle.trigger_fields.byKeys ||
"").split(",");
var ofKeys=(
bundle.cleaned_request.querystring.of ||
bundle.trigger_fields.ofKeys ||
"").split(",");
if(ofKeys.length==1){ofKeys=ofKeys[0];}

return objByOf(bundle.cleaned_request.data,byKeys,ofKeys);

function set(obj,path,val){
var head=path[0];
if(!path.length){
obj.push(val);
}
if(path.length==1){
obj[head]=(obj[head]||[]).concat([val]);
}
if(path.length>1){
obj[head]=obj[head]||{};
set(obj[head],path.slice(1),val);
}
return obj;
}
function get(obj,keys){
if(!keys){return obj;}
if(!keys.reduce){return obj[keys];}
return keys.reduce(function(accum,x,i){
accum[x]=obj[x];
return accum;
},{});
}

function objByOf(arr,byKeys,ofKeys){
byKeys=(byKeys.map ? byKeys : [byKeys]);
return arr.reduce(function(accum,x,i){
return set(accum,
byKeys.map(function(k){return get(x,k);}),
(ofKeys?get(x,ofKeys):x)
);
},
byKeys.length? {} : []);
}
}
};

In this example, our script needs to know which fields to group by (and optionally which fields to keep at the “leaf” level of our hierarchy). So, we pull in these values from one of two sources, the request querystring, or more importantly bundle.trigger_fields What is this? It’s an object representing the values that a user can provide while configuring a Zap with our Trigger. In our example, we know we want to collect two parameters for our script to run. We can go back to our Trigger definition and declare these fields:



Testing our App to build a Zap


Once you’ve saved your script, you can now test this out. In a new tab, head over to https://zapier.com/app/explore and click “Make a zap”. Since your App is private by default until you publish it, you’ll need to be signed in, but if you are, you should be able to find your App to choose as a trigger:


When you choose it (and select the only Trigger we’ve made available in our App), you’ll then be prompted to provide values for the trigger fields. At this point, you can start to see the reusable value of this Zap, which can be used in a similar way for many different Looks/datasets.


Before you proceed to your actions, Zapier will want you to test the Trigger:


Copy the URL, and then head over to Looker. From your Look, click “Schedule…” from the gear menu, choose Webhook as the destination, paste in the URL, choose “Inline JSON” as the format, and click “Test”



Wait some time for the Look to finish running, and then back in Zapier, click to continue and you should now see this:


You can then continue on to use whichever Zapier action you had in mind, with the input data transformed exactly as you want it.


0 replies

Be the first to reply!

Reply