Webhooks to Redshift

This page provides you with instructions on how to collect data from Webhooks and load it into Redshift. (If this manual process sounds onerous, check out Stitch, which can do all the heavy lifting for you in just a few clicks.)

Receiving Data From Webhooks

Webhooks send data through user-defined HTTP POST callbacks. An application that uses Webhooks will POST data to a your custom endpoint when an event occurs.  This method of sending data is widely used by many popular services.  Trello, Segment, Stripe and Github all make data available using this method.

The first step for getting data from a webhook into Redshift is setting up callbacks from the application you’re gathering data from. Webhooks work in the same general way for most use cases.  Github has good documentation for Webhooks, which can be found here. Tools like UltraHook can help you get a public endpoint for development.  

Sample Webhook Data

Once the you’ve set up HTTP endpoints, data from the source will be enclosed in the body of the request in JSON format.  Below is a sample of what that data looks like for a Github Webhook.

{
  "action": "opened",
  "issue": {
    "url": "https://api.github.com/repos/octocat/Hello-World/issues/1347",
    "number": 1347,
    ...
  },
  "repository" : {
    "id": 1296269,
    "full_name": "octocat/Hello-World",
    "owner": {
      "login": "octocat",
      "id": 1,
      ...
    },
    ...
  },
  "sender": {
    "login": "octocat",
    "id": 1,
    ...
  }
}

Preparing Webhook Data for Redshift

With the JSON in hand, you now need to map all those data fields into a schema that can be inserted into your Redshift database. This means that, for each value in the response, you need to identify a predefined data type (i.e. INTEGER, DATETIME, etc.) and build a table that can receive them.

Refer to the documentation for the SaaS app or service you are using to get a good sense of what fields will be provided by each endpoint, along with their corresponding data types. Once you have identified all of the columns you will want to insert, use the CREATE TABLE statement in Redshift to define a table that can receive all of this data.

Inserting Data from Webhooks into Redshift

It may seem like the easiest way to add your data is to build tried-and-true INSERT statements that add data to your Redshift table row-by-row. This will be your gut reaction If you have any experience with SQL. It will work, but isn’t the most efficient way to get the job done.

Redshift actually offers some good documentation for how to best bulk insert data into new tables. The COPY command is particularly useful for this task, as it allows you to insert multiple rows without needing to build individual INSERT statements for each row.

If you cannot use COPY, it might help to use PREPARE to create a prepared INSERT statement, and then use EXECUTE as many times as required. This avoids some of the overhead of repeatedly parsing and planning INSERT.

Keeping Data Up-To-Date

So what’s next? You’ve built a script that collects data from the Webhook and moves it into Redshift.  What about when your script doesn’t recognize a new data type, or when a record needs to be updated to a new value? The key is to build your script in such a way that it can identify incremental updates to your data.

Other Data Warehouse Options

Redshift is totally awesome, but sometimes you need to start smaller or optimize for different things. In this case, many people choose to get started with Postgres, which is an open source RDBMS that uses nearly identical SQL syntax to Redshift. If you’re interested in seeing the relevant steps for loading this data into Postgres, check out Webhooks to Postgres

Easier and Faster Alternatives

If you have all the skills necessary to go through this process, chances are building and maintaining a script like this isn’t a very high-leverage use of your time.

Thankfully, products like Stitch were built to solve this problem automatically. With just a few clicks, Stitch starts extracting your data via Webhooks, structuring it in a way that is optimized for analysis, and inserting that data into your Redshift data warehouse.