Rails To Anything — Integrating Zapier via Webhooks

Tinus Wagner
4 min readApr 19, 2017

--

Over the last few days I’ve had a client introduce me to a pretty crispy service called Zapier. If you’ve never heard of it, it’s basically an automation tool for tasks between apps.

Naturally it also supports webhooks which in the bigger scheme of things is pretty cool. Instead of having to integrate a whole handful of API’s for whatever services my client would like to use. I can create one POST request to Zapier, and give the client the power to do whatever he/she desires with that data. Wether it be scheduling a slack notification when an invoice is created, adding a newly registered user to a google doc, or subscribing a user to a mailchimp list after they’ve created X amount of content.

Before we get started

Pretty much every service Zapier integrates with already has some sort of public API you can use, I’m aware of that. But if you need to integrate with many in a small timeframe and don’t want to get bogged down with collecting a small bag full of api keys, authorizations, and tests, for multiple services, then this might be helpful.

It’s a lot more fun letting the customer know that the data is in zapier and that they can now pretty much do whatever they want with it. And as you’ll see in this example, it’s pretty simple.

Setting this up in a Rails application

So I opted to use a pretty amazing gem called httParty, which “makes http fun again” according to the authors. In all seriousness it’s a great wrapper for all things GET, POST & PUT. So let’s get started.

Head over to their github page and give the documentation a quick read, then jump into the console and run:

gem install httparty

Onto our Zapier Service

Let’s keep things nice and tidy, let’s set up a service. Head on over to your services directory and create a new folder for zapier. We’re going to need a few things:

  1. A base.rb file that can deal with initialising attributes & resources, dealing with the base response from zapier, and in this case, doing a light bit of error handling.
  2. a File for each individual zap you’d like to create, along with whatever params and functions you’d like to cram into it.
  3. Some way of saving the url hook provided by zapier. Could be a file in config/initializers, or you could use the brilliant gem dotenv.
I’ve named my Zaps after the model the inherit from, but if you have multiple events firing off from the same model/controller, it’s probably better to name them specific to their purpose (e.g. new_place_created.rb)

So let’s get started on our base.rb file.

We’re doing a couple of things here:

  • initialising httParty
  • setting a few attribute accessor & reader methods to manipulate the request and response
  • Initialising the resource we’ll be using as a source for the POST request
  • Laying out the format the request will take
  • Saving the response code returned from Zapier upon a successful (200)
  • A little sanity check test to catch oddities & failures

Keep in mind that every file we create from here on out will inherit from base.rb

Zap Example:
In the example I’ve created, I have zap’s that are firing off when specific model methods are triggered.

Whenever a user creates a space, I want to send a few pieces of data to Zapier.

  1. define the POST request in our call_operation which executes when the service is fired. The first argument here being the hook url that is supplied when creating a new zap, and the second being the parameters we’d like it to send.
  2. We define which parameters the POST request will…POST. You can format your params as either JSON as I’ve done, or XML.

And that’s about it, rinse and repeat

All you need now is an action to fire the hook with! Whether that be a hook somewhere into the callback_order when creating a record, or as part of a method somewhere in your model, or both as I’ve done!

…How do I know if it’s working?

Jump back to Zapier and give it a test of course. If the Zap receives the post request successfully, you’ll be able to view the sent data as well.

A successful JSON POST request with the 3 params I included

And that’s it. Enjoy.

Tip

Thanks for reading and let me know if I got anything wrong!

--

--