Xero to Redshift

This will help you extract data from Xero 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 Xero

For starters, you need to get your data out of Xero.  That can be done by making calls to Xero’s REST API. The full documentation for the API can be found here.

To use the Xero REST API, your script needs to make HTTP requests, and parse the response. The API returns XML formatted objects. The standard HTTP methods like GET, PUT, POST and DELETE are going to be your major tools here.

Xero’s API offers access to data endpoints like xero_accounts, xero_invoices, and xero_payments. Using methods outlined in the API documentation, you can retrieve the data you’d like to move to Redshift.

Sample Xero Data

When you query the Xero API, it will return JSON formatted data.  Below is an example response from the accounts endpoint.


<Accounts>
  <Account>
    <AccountID>297c2dc5-cc47-4afd-8ec8-74990b8761e9</AccountID>
    <Code/>
    <Name>BNZ Cheque Account</Name>
    <Type>BANK</Type>
    <TaxType>NONE</TaxType>
    <EnablePaymentsToAccount>false</EnablePaymentsToAccount>
    <BankAccountNumber>3809087654321500</BankAccountNumber>
    <BankAccountType>BANK</BankAccountType>
    <CurrencyCode>NZD</CurrencyCode>
  </Account>
  <Account>
    <AccountID>5040915e-8ce7-4177-8d08-fde416232f18</AccountID>
    <Code>200</Code>
    <Name>Sales</Name>
    <Type>REVENUE</Type>
    <TaxType>OUTPUT</TaxType>
    <Description>Income from any normal business activity</Description>
    <EnablePaymentsToAccount>false</EnablePaymentsToAccount>
  </Account>
</Accounts>

Preparing Xero Data for Redshift

Now that you have the data, 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 Xero 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 Xero 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 first reaction.  It will work, but isn’t the most efficient way to get the job done.

Redshift offers some helpful 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 an INSERT statement, and then use EXECUTE as many times as required. This avoids some of the overhead of repeatedly parsing and planning INSERT.

Easier and Faster Alternatives

If you have all the skills necessary to go through this process, you  might have other projects that you need to be focusing on.

Luckily, powerful tools like Stitch were built to solve this problem automatically. With just a few clicks, Stitch starts extracting your Xero data via the API, structuring it in a way that is optimized for analysis, and inserting that data into your Redshift data warehouse.