Braintree to Redshift

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

Pulling Data Out of Braintree

The first step of getting Braintree data into Redshift is actually pulling that data off of Braintree’s servers. This is possible using the Braintree API, which is available to all Braintree customers. Full API documentation can be accessed here.

Data from Braintree can be accessed using Webhooks or the Braintree API. Webhooks will effectively push data to a defined HTTP endpoint as events happen. Contrast that to the API, where you can find specific data via an API request. The main difference here is that the API method will allow you to retrieve all of your historical data, instead of just new data. You’ll want the historical data, so lets focus on the API.

By using the guidelines laid out in the documentation, you will be able to retrieve the data you wish to load into Redshift. Useful data endpoints like customer, discount, and of course the settlements batch summary are readily accessible through the API.

Sample Braintree Data

After you are able to successfully query the Braintree API, it will return JSON formatted data. Take a look at an example response for the settlement batch summary:

[
  {
    "custom_field_1": "your_first_custom_value",
    "card_type": "Mastercard",
    "count": "24",
    "merchant_account_id": "your_merchant_account_id",
    "kind": "sale",
    "amount_settled": "1200.00"
  },
  {
    "custom_field_1":  "your_second_custom_value",
    "card_type": "Mastercard",
    "count": "42",
    "merchant_account_id": "your_merchant_account_id",
    "kind": "sale",
    "amount_settled": "1234.00"
  }
]

Preparing Braintree Data for Redshift

Now that you’ve got the desired data in JSON format, you need to map those data fields to a schema that can be inserted into your Redshift database. Consider each value in the API response, identify a predefined data type (i.e. INTEGER, DATETIME, etc.) and build a table that can receive them.

The Braintree API documentation can help you define what fields and data types will be provided by each endpoint. Once you have identified all of the columns you will want to insert, use the CREATE TABLE statement in Redshift to build a table that will receive all of this data.

Inserting Braintree Data 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 a Redshift table row-by-row. If you have been writing SQL for a while, you will be tempted to take this route. It will work, however it isn’t the most efficient way to go.

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 can help you avoid the overhead of constantly planning and parsing the INSERT statement.

Keeping Data Up-To-Date

Great! You have now built a script that pulls data from Braintree and moves it into Redshift.  Now the question is: What happens on Monday, when you have new and updated payment activity from the weekend?

The key is to build your script in such a way that it can also identify incremental updates to your data. Some API’s include fields like created_at that allow you to quickly identify records that are new since your last update (or since the newest record you’ve copied into Redshift). You can set your script up as a cron job or continuous loop to keep pulling down new data as it appears.

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 Braintree 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 Braintree data via the API, structuring it in a way that is optimized for analysis, and inserting that data into your Redshift data warehouse.