Recurly to Redshift

This page provides you with instructions on how to extract data from Recurly’s backend and load it into Amazon Redshift. (If this manual process is a bit more involved than you’d prefer, check out Stitch, which can do all the heavy lifting for you in just a few clicks.)

Pulling Data Out of Recurly

In order to get your Recurly data into AWS Redshift, you have to start by extracting it from Recurly’s servers. You can do this using the Recurly API, which is available to all Recurly customers. Full API documentation can be accessed at this link.

As with most billing system APIs, Recurly’s API goes in both directions and can be used for actually sending them new data and changing things like customer billing information or payment terms. However, for our purposes (getting reporting data into an analytical database), we only need to focus on endpoints that return data to us. Recurly has no shortage of these, including endpoints focused on Billing Info, Coupons, Plans, Invoices, and more.

Sample Recurly Data

The Recurly API returns XML-formatted data. Below is an example of the kind of response you might see when querying the accounts endpoint.

<account href="https://your-subdomain.recurly.com/v2/accounts/1">
 <adjustments href="https://your-subdomain.recurly.com/v2/accounts/1/adjustments"/>
 <billing_info href="https://your-subdomain.recurly.com/v2/accounts/1/billing_info"/>
 <invoices href="https://your-subdomain.recurly.com/v2/accounts/1/invoices"/>
 <redemption href="https://your-subdomain.recurly.com/v2/accounts/1/redemption"/>
 <subscriptions href="https://your-subdomain.recurly.com/v2/accounts/1/subscriptions"/>
 <transactions href="https://your-subdomain.recurly.com/v2/accounts/1/transactions"/>
 <account_code>1</account_code>
 <state>active</state>
 <username nil="nil"></username>
 <email>verena@example.com</email>
 <first_name>Verena</first_name>
 <last_name>Example</last_name>
 <company_name></company_name>
 <vat_number nil="nil"></vat_number>
 <tax_exempt type="boolean">false</tax_exempt>
 <cc_emails>cc1@example.com,cc2@example.com</cc_emails>
 <address>
 <address1>123 Main St.</address1>
 <address2 nil="nil"></address2>
 <city>San Francisco</city>
 <state>CA</state>
 <zip>94105</zip>
 <country>US</country>
 <phone nil="nil"></phone>
 </address>
 <accept_language nil="nil"></accept_language>
 <hosted_login_token>a92468579e9c4231a6c0031c4716c01d</hosted_login_token>
 <created_at type="datetime">2015-10-25T12:00:00Z</created_at>
</account>

Preparing Recurly Data for Redshift

All that data you’re pulling down from Recurly’s API now needs to be inserted into a table inside a Redshift database. This means that, for each value in the response, you need to identify a predefined datatype (i.e. INTEGER, DATETIME, etc.) and build a table that can receive them. The Recurly API documentation can give you a good sense of what fields will be provided by each endpoint, along with their corresponding datatypes.

Inserting Recurly Data into Redshift

Once you have identified all of the columns you will want to insert, you can use the CREATE TABLE statement in Redshift to create a table that can receive all of this data.

With a table built, it may seem like the easiest way to add your data (especially if there isn’t much of it), is to build INSERT statements to add data to your Redshift table row-by-row. If you have any experience with SQL, this will be your gut reaction. But beware! Redshift isn’t optimized for inserting data one row at a time, and if you have any kind of high-volume data being inserted, you would be much better off loading the data into Amazon S3 and then using the COPY command to load it into Redshift.

Keeping Data Up-To-Date

So, now what? You’ve built a script that pulls data from Recurly and loads it into Redshift, but what happens tomorrow when you have a hundred new transactions?

The key is to build your script in such a way that it can also identify incremental updates to your data. Thankfully, Recurly’s API results 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 Recurly to Postgres

Easier and Faster Alternatives

If all this sounds a bit overwhelming, don’t be alarmed. 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 Recurly data via the API, structuring it in a way that is optimized for analysis, and inserting that data into your Amazon Redshift data warehouse.