Shopify to Redshift

This page provides you with instructions on how to extract data from Shopify’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 in just a few clicks.)

Pulling Data Out of Shopify

The first step of getting your Shopify data into AWS Redshift is actually pulling that data off of Shopify’s servers. You can do this using the Shopify API, which is available to all Shopify customers. Full API documentation can be accessed here.

Data from the Shopify API can be retrieved programmatically via either REST requests or Webhooks. We’ll focus on the REST API here because it will allow you to retrieve all of your historical data rather than just new real-time data.

Shopify’s API offers numerous endpoints that can provide information on transactions, customers, refunds, and much, much more. Using methods outlined in their API documentation, you can retrieve the data you’d like to place into Redshift.

Sample Shopify Data

The Shopify API returns JSON-formatted data. Below is an example of the kind of response you might see when querying the transactions endpoint.

HTTP/1.1 200 OK
{
  "transactions": [
    {
      "id": 179259969,
      "order_id": 450789469,
      "kind": "refund",
      "gateway": "bogus",
      "message": null,
      "created_at": "2005-08-05T12:59:12-04:00",
      "test": false,
      "authorization": "authorization-key",
      "status": "success",
      "amount": "209.00",
      "currency": "USD",
      "location_id": null,
      "user_id": null,
      "parent_id": null,
      "device_id": null,
      "receipt": {},
      "error_code": null,
      "source_name": "web"
    },
    {
      "id": 389404469,
      "order_id": 450789469,
      "kind": "authorization",
      "gateway": "bogus",
      "message": null,
      "created_at": "2005-08-01T11:57:11-04:00",
      "test": false,
      "authorization": "authorization-key",
      "status": "success",
      "amount": "409.94",
      "currency": "USD",
      "location_id": null,
      "user_id": null,
      "parent_id": null,
      "device_id": null,
      "receipt": {
        "testcase": true,
        "authorization": "123456"
      },
      "error_code": null,
      "source_name": "web",
      "payment_details": {
        "credit_card_bin": null,
        "avs_result_code": null,
        "cvv_result_code": null,
        "credit_card_number": "•••• •••• •••• 4242",
        "credit_card_company": "Visa"
      }
    },
    {
      "id": 801038806,
      "order_id": 450789469,
      "kind": "capture",
      "gateway": "bogus",
      "message": null,
      "created_at": "2005-08-05T10:22:51-04:00",
      "test": false,
      "authorization": "authorization-key",
      "status": "success",
      "amount": "250.94",
      "currency": "USD",
      "location_id": null,
      "user_id": null,
      "parent_id": null,
      "device_id": null,
      "receipt": {},
      "error_code": null,
      "source_name": "web"
    }
  ]
}

Preparing Shopify Data for Redshift

Here’s the tricky part: you need to map the data that comes out of each Shopify API endpoint into a schema that can be inserted into 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 Shopify API documentation can give you a good sense of what fields will be provided by each endpoint, along with their corresponding datatypes.

Inserting Shopify 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 Shopify and loads it into Redshift, but what happens tomorrow when you have ten new transactions?

The key is to build your script in such a way that it can also identify incremental updates to your data. Thankfully, Shopify’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 Shopify 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 Shopify data via the API, structuring it in a way that is optimized for analysis, and inserting that data into your Amazon Redshift data warehouse.