Best answer

Processing / Mapping REST Hook Payloads

  • 16 November 2021
  • 6 replies
  • 269 views

Hi,

 

I’m switching from using polling based integrations to REST hook ones. However there’s a few things the documentation for `zapier-platform-core` doesn’t make clear to me.

With polling I can process the response data from my application before returning it to a Zap to reshape it, I’m wondering how best to do this with instant triggers?

 

My application has a single set of endpoints for managing subscriptions, where hook can subscribe to one or more events, and will receive all events of those types within the user’s scope, the actual event payload is wrapped to aid processing multiple types:

 

{
"timestamp": "2021-11-16T16:29:00Z", // Event Timestamp
"event": "resource.action", // The Event Type
"organization": { //
"id": "someid", // "Organization" represents the scope in which
"name": "Some Name" // the event happened
},
"payload": { /* object */ } // The "payload" object
// contains the resource to which the event
// happened. It's type depends on the value of 'event'
}

For Zapier I’ve created a helper which will create a subscription, with a Zapier trigger mapping to a single event type in my application.

How should I best pass these payloads back to the user such that they can access the payload data without needing to wrap all/most fields with `payload` explicitly?

icon

Best answer by christina.d 26 January 2022, 00:56

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

6 replies

Userlevel 7
Badge +9

You can change the shape or contents of the object returned from your trigger to the Zap in your perform method.  Simple example.

Be aware that you’ll need a GET endpoint (the performList function) that returns data for the user to use for field mapping in the Zap editor. It’ll need to match the structure of what you’ll return from your hook trigger’s perform exactly, or your users will get errors when the Zap runs. What fields get mapped at Zap “config time” need to be there at Zap “run time”. 

Hi,

Yes, my webhooks contain the same data as the list API, but those fields are wrapped in an event container. I’m asking how, in my Zapier platform integration, I can transform the webhook data to extract the payload out of the event container for users.

Userlevel 7
Badge +9

In your perform method, of both the perform and performList, you can perform any transforms on the data you need to. You’ll need to write some JavaScript code.

Could be as simple as return response.payload. (Or using a map on the array of responses in your performList)

Looks like the event wrapper has some useful info. Maybe keep it? If the field mapping experience is funky for users with the embedded payload maybe just flatten that object - bring its fields to the top level of the result object alongside that meta data. Something along the lines of:

 

const sample = {

    "timestamp": "2021-11-16T16:29:00Z",     

    "event": "resource.action",          

    "organization": {                    

        "id": "someid",                  

        "name": "Some Name"              

    },

    "payload": { 

      "animal-type": "monkey",

      "name": "chunky" 

    }                                                                                   

}

const result = {    

    "timestamp": sample.timestamp, 

    "event": sample.event,         

    "organization": sample.organization,

    ...sample.payload

}

console.log(JSON.stringify(result));

 

Again, JavaScript code in the perform and performList functions. There you can transform the data however makes sense for your users. If you’re building in the UI, you’ll need to switch to code mode for the performList handler.

If that doesn’t make sense maybe post some more of your trigger configuration and we can give more specific guidance.

 

 

Hi,

Yes that’s what I want, but I want to do it on webhook payloads, (so when Zapier receives a webhook call)

Userlevel 7
Badge +9

Do that in the perform function of the rest hook trigger. See a full example here.  This will take the hook payload, and give you the opportunity to transform it before it’s made available to the Zap.

Userlevel 7
Badge +9

Hi all!

I wanted to pop in and consolidate some of Zane’s super helpful replies into one answer for ya’ll!


 

In your perform method, of both the perform and performList, you can perform any transforms on the data you need to. You’ll need to write some JavaScript code.

Could be as simple as return response.payload. (Or using a map on the array of responses in your performList)

Looks like the event wrapper has some useful info. Maybe keep it? If the field mapping experience is funky for users with the embedded payload maybe just flatten that object - bring its fields to the top level of the result object alongside that meta data. Something along the lines of:

const sample = {



    "timestamp": "2021-11-16T16:29:00Z",     

    "event": "resource.action",          

    "organization": {                    

        "id": "someid",                  

        "name": "Some Name"              

    },

    "payload": { 

      "animal-type": "monkey",

      "name": "chunky" 

    }                                                                                   

}

const result = {    

    "timestamp": sample.timestamp, 

    "event": sample.event,         

    "organization": sample.organization,

    ...sample.payload

}

console.log(JSON.stringify(result));

Again, JavaScript code in the perform and performList functions. There you can transform the data however makes sense for your users. If you’re building in the UI, you’ll need to switch to code mode for the performList handler.

If that doesn’t make sense maybe post some more of your trigger configuration and we can give more specific guidance.

 

 

 


If you’re specifically looking for how to execute this on webhook payloads: 

 

Do that in the perform function of the rest hook trigger. See a full example here.  This will take the hook payload, and give you the opportunity to transform it before it’s made available to the Zap.