Shippo to Redshift

This page provides you with instructions on how to extract data from Shippo 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.)

Pulling Data Out of Shippo

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

Data from the Shippo API can be retrieved programmatically. Shippo’s API offers access to useful endpoints like addresses, parcels, refunds, shipments and more. Using methods outlined in their API documentation, you can retrieve the data you’d like to load into Redshift.

Sample Shippo Data

Once you successfully query the Shippo API, it will return JSON data. Take a look at an example response from the from the shipments endpoint:

{
    "status": "SUCCESS",
    "object_created": "2013-12-01T06:24:20.121Z",
    "object_updated": "2013-12-01T06:24:20.121Z",
    "object_id": "5e40ead7cffe4cc1ad45108696162e42",
    "object_owner": "shippotle@goshippo.com",
    "address_from":{
        "object_id": "fdabf0abb93c4460b60aa596116872a7",
        "is_complete": true,
        "validation_results": {},
        "name": "Mrs Hippo",
        "street1": "1092 Indian Summer Ct",
        "city": "San Jose",
        "state": "CA",
        "zip": "95122",
        "country": "US",
        "phone": "4159876543",
        "email": "mrshippo@goshippo.com"
    },
    "address_to":{
        "object_id": "0476d70c612a423f9509ba5f807569db",
        "is_complete": true,
        "validation_results": {},
        "name": "Mr Hippo",
        "street1": "965 Mission St #572",
        "city": "San Francisco",
        "state": "CA",
        "zip": "94103",
        "country": "US",
        "phone": "4151234567",
        "email": "mrhippo@goshippo.com"
    },
    "address_return":{
        "object_id": "fdabf0abb93c4460b60aa596116872a7",
        "is_complete": true,
        "validation_results": {},
        "name": "Mrs Hippo",
        "street1": "1092 Indian Summer Ct",
        "city": "San Jose",
        "state": "CA",
        "zip": "95122",
        "country": "US",
        "phone": "4159876543",
        "email": "mrshippo@goshippo.com"
    },
    "parcels":[{
        "object_id": "ec952343dd4843c39b42aca620471fd5",
        "length": "10",
        "width": "15",
        "height": "10",
        "distance_unit": "in",
        "weight": "1",
        "mass_unit": "lb"
    }],
    "shipment_date": "2013-12-03T12:00:00.000Z",
    "extra":{
      "insurance": {
        "amount": "",
        "currency": ""
      },
      "reference_1":"",
      "reference_2":""
    },
    "customs_declaration": "",
    "rates": [
        {
            "object_created": "2013-12-01T06:24:21.121Z",
            "object_id": "545ab0a1a6ea4c9f9adb2512a57d6d8b",
            "object_owner": "shippotle@goshippo.com",
            "shipment": "5e40ead7cffe4cc1ad45108696162e42",
            "attributes": [],
            "amount": "5.50",
            "currency": "USD",
            "amount_local": "5.50",
            "currency_local": "USD",
            "provider": "USPS",
            "provider_image_75": "https://cdn2.goshippo.com/providers/75/USPS.png",
            "servicelevel": {
              "name": "Priority Mail",
              "token":"usps_priority",
              "terms": ""
            },
            "days": 2,
            "arrives_by": null,
            "duration_terms": "Delivery in 1 to 3 business days.",
            "messages": [],
            "carrier_account": "078870331023437cb917f5187429b093",
            "test": true
        },
        ...
    ],
    "carrier_accounts": [],
    "metadata": "Customer ID 123456",
    "messages": [],
    "test": true
}

Getting Shippo Data Ready 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.

Check out the Stitch Shippo Documentation to get a good sense of 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 Shippo 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 your Redshift table row-by-row. If you have any experience with SQL, this will be your gut reaction.  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, now what’s next? You’ve built a script that pulls data from Shippo and moves it into Redshift, but what happens on Monday when you have new and updated shipments and transactions?

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