This article takes you through on how to sync your Xero to Amazon Redshift for further analysis. If these instructions look cumbersome you can simplify the process of syncing data from Xero to Redshift by using Blendo, where the whole process will be handled by Blendo and you can focus on what matters, business analytics.
Extract your data from Xero
Xero has an excellent API, or to be more precise a number of APIs, and encourages developers to build applications that can be sold on their add-on marketplace. The APIs that they expose are the following:
- Xero Core (Accounting) API – exposes accounting and related functions of the main Xero application and can be used for a variety of purposes such as creating transactions like invoices and credit notes, right through to extracting accounting data via our reports endpoint.
- Xero Payroll API – exposes payroll related functions of Payroll in Xero and can be used for a variety of purposes such as syncing employee details, importing timesheets etc.
- Files API – provides access to the files, folders, and the association of files within a Xero organization.
- Fixed Assets API – which is under review, this feature is not yet available but users can vote for it to become publicly available.
- Xero Practice Manager API – a recently released product built on the WorkflowMax product, which is an API for managing workflows.
In this post, we’ll focus on the Xero Core (Accounting) API. Which exposes the core accounting functionalities of the Xero product. The Xero API is a RESTful web service and uses the OAuth (v1.0a) protocol to authenticate 3rd party applications. As a RESTful API, interacting with it can be achieved by using tools like CURL or by using HTTP clients for your favorite language or framework. A few suggestions:
- Apache HttpClient for Java
- Spray-client for Scala
- Hyper for Rust
- Ruby rest-client
- Python http-client
As a product and consequently an API that has to deal with sensitive data, Xero API takes really good care of security. For this reason, there are a number of different applications that can be developed and integrate with it, where the main difference is how the application authenticates, how often the tokens expire and in general security-related aspects. For more about the different applications types, you can consult the application types guides on their documentation.
Xero API requests limits
The Xero API has three different types of limits that enforces on the usage of their API. It’s extremely important to keep those in mind when developing against its API and a reason for many headaches when someone attempts to build an infrastructure for extracting data.
- Daily limit – of 1000 API calls per organization.
- Requests per minute – each OAuth access token can be used up to 60 times in any 60 second period. This rate limit is based on a rolling 60-second window.
- Request Size Limit – A single POST to the Accounting or Payroll APIs has a size limit of 5MB.
For more information about the API limitations, please consult the documentation for API limits.
Xero API Resources
The Xero API has a very rich data model of 31 resources. It is important to know that by default the response type of the API calls is of type text/xml but you can override this option and request JSON responses if preferred.
- Bank Statements
- Bank Transactions
- Bank Transfers
- Branding Themes
- Contact Groups
- Credit Notes
- Expense Claims
- Invoice Reminders
- Linked Transactions
- Manual Journals
- Purchase Orders
- Repeating Invoices
- Tax Rates
- Tracking Categories
Requesting data from the Xero API
Let’s assume that you would like to retrieve all the invoices that you have issued through Xero and put the information in a data warehouse to perform analytics and reporting. To do that you should perform a GET request to the https://api.xero.com/api.xro/2.0/Invoices endpoint. A typical result, in XML, from performing such action is like the following:
<Invoices> <Invoice> <Type>ACCREC</Type> <Contact> <ContactID>025867f1-d741-4d6b-b1af-9ac774b59ba7</ContactID> <ContactStatus>ACTIVE</ContactStatus> <Name>City Agency</Name> <Addresses> <Address> <AddressType>STREET</AddressType> </Address> <Address> <AddressType>POBOX</AddressType> <AddressLine1>L4, CA House</AddressLine1> <AddressLine2>14 Boulevard Quay</AddressLine2> <City>Wellington</City> <PostalCode>6012</PostalCode> </Address> </Addresses> <Phones> <Phone> <PhoneType>DEFAULT</PhoneType> </Phone> <Phone> <PhoneType>DDI</PhoneType> </Phone> <Phone> <PhoneType>MOBILE</PhoneType> </Phone> <Phone> <PhoneType>FAX</PhoneType> </Phone> </Phones> <UpdatedDateUTC>2009-08-15T00:18:43.473</UpdatedDateUTC> <IsSupplier>false</IsSupplier> <IsCustomer>true</IsCustomer> </Contact> <Date>2009-05-27T00:00:00</Date> <DueDate>2009-06-06T00:00:00</DueDate> <Status>AUTHORISED</Status> <LineAmountTypes>Exclusive</LineAmountTypes> <LineItems> <LineItem> <Description>Onsite project management </Description> <Quantity>1.0000</Quantity> <UnitAmount>1800.00</UnitAmount> <TaxType>OUTPUT</TaxType> <TaxAmount>225.00</TaxAmount> <LineAmount>1800.00</LineAmount> <AccountCode>200</AccountCode> <Tracking> <TrackingCategory> <TrackingCategoryID>e2f2f732-e92a-4f3a9c4d-ee4da0182a13</TrackingCategoryID> <Name>Activity/Workstream</Name> <Option>Onsite consultancy</Option> </TrackingCategory> </Tracking> <LineItemID>52208ff9-528a-4985-a9ad-b2b1d4210e38</LineItemID> </LineItem> </LineItems> <SubTotal>1800.00</SubTotal> <TotalTax>225.00</TotalTax> <Total>2025.00</Total> <UpdatedDateUTC>2009-08-15T00:18:43.457</UpdatedDateUTC> <CurrencyCode>NZD</CurrencyCode> <InvoiceID>243216c5-369e-4056-ac67-05388f86dc81</InvoiceID> <InvoiceNumber>OIT00546</InvoiceNumber> <Payments> <Payment> <Date>2009-09-01T00:00:00</Date> <Amount>1000.00</Amount> <PaymentID>0d666415-cf77-43fa-80c7-56775591d426</PaymentID> </Payment> </Payments> <AmountDue>1025.00</AmountDue> <AmountPaid>1000.00</AmountPaid> <AmountCredited>0.00</AmountCredited> </Invoice> </Invoices>
It is possible to paginate your results by using the paging support of the Xero API. Which is very useful when you have to work with a large number of invoices. Also, it is possible to request from the API only the latest invoices. This is done by providing the “Modified After” parameter on the GET request to the API. The ModifiedAfter filter is actually an HTTP header: ‘If-Modified-Since‘.
A UTC timestamp (yyyy-mm-ddThh:mm:ss) . Only invoices created or modified since this timestamp will be returned e.g. 2009-11-12T00:00:00.
Xero exposes a very rich API which offers you the opportunity to get very granular data about your accounting activities and use it for analytics and reporting purposes. This richness comes with a price though, a large number of resources that have to be handled where some of them allow fetching updates and some other not.
Xero provides online accounting software and services for small and medium businesses. It includes a full accrual accounting system with a cashbook, automated daily bank feeds, invoicing, debtors, creditors, sales tax and reporting. Xero’s Software as a Service business model where software is hosted on the Internet as an innovative means of supplying solutions to large fragmented markets such as small businesses.
Prepare your Xero Data for Amazon Redshift
Amazon Redshift is built around industry-standard SQL with added functionality to manage very large datasets and high-performance analysis. So, in order to load data into it, you will have to follow its data model which is a typical relational database model.
The data you extract from a data source should be mapped into tables and columns. Where you can consider the table as a map to the resource you want to store and columns the attributes of that resource. Also, each attribute should adhere to the datatypes that are supported by Redshift, currently the datatypes that are supported are the following:
- DOUBLE PRECISION
As your data are probably coming in a representation like JSON that supports a much smaller range of data types you have to be really careful about what data you feed into Redshift and make sure that you have mapped your types into one of the datatypes that are supported by Redshift.
Designing a Schema for Redshift and mapping the data from a data source to it is a process that you should take seriously as it can both affect the performance of your cluster and the questions that you can answer. It’s always a good idea to have in your mind the best practices that Amazon has published regarding the design of a Redshift database. When you have concluded on the design of your database you need to load your data on one of the data sources that are supported as input by Redshift, these are the following:
About Amazon Redshift
Amazon Redshift is one of the most popular data warehousing solutions which is part of the Amazon Web Services (AWS) ecosystem. It is a petabyte scale, fully managed data warehouse as a service solution that runs on the cloud. It is SQL based and you can communicate with it as you would do with PostgreSQL, actually, you can use the same driver although it would be better to use the drivers recommended by Amazon.
Load data from Xero to Redshift
The first step to sync data from Xero to Redshift is to put them in a source that Redshift can pull it from. As it was mentioned earlier there are three main data sources supported, Amazon S3, Amazon DynamoDB, and Amazon Kinesis Firehose, with Firehose being the most recent addition as a way to insert data into Redshift.
To upload data to Amazon S3 you will have to use the AWS REST API, as we see again APIs play an important role in both the extraction but also the loading of data into our data warehouse. The first task that you have to perform is to create a bucket, you do that by executing an HTTP PUT on the Amazon AWS REST API endpoints for S3. You can do this by using a tool like CURL. Or use the libraries provided by Amazon for your favorite language. You can find more information by reading the API reference for the Bucket operations on Amazon AWS documentation.
After you have created your bucket you can start sending data to Amazon S3, using again the same AWS REST API but by using the endpoints for Object operations. As in the Bucket case you can either access the HTTP endpoints directly or use the library of your preference.
DynamoDB imports data again from S3, it adds another step between S3 and Amazon Redshift so if you don’t need it for other reasons you can avoid it.
Firehose is the latest addition as a way to insert data into Redshift and offers a real-time streaming approach into data importing. The necessary steps for adding data to Redshift through Kinesis Firehose are the following:
- create a delivery stream
- add data to the stream
whenever you add new data to the stream, Kinesis takes care of adding these data to S3 or Redshift, again going through S3, in this case, is redundant if your goal is to move your data to Redshift. The execution of the previous two steps can be performed either through the REST API or through your favorite library just as in the previous two cases. The difference here is that for pushing data into the stream you’ll be using a Kinesis Agent.
Amazon Redshift supports two methods for loading data into it. The first one is by invoking an INSERT command.
The way you invoke the INSERT command is the same as you would do with any other SQL database, for more information you can check the INSERT examples page on the Amazon Redshift documentation.
Redshift is not designed for INSERT like operations, on the contrary, the most efficient way of loading data into it is by doing bulk uploads using a COPY command. You can perform a COPY command for data that lives as flat files on S3 or from a DynamoDB table.
When you perform COPY commands, Redshift is able to read multiple files in simultaneously and it automatically distributes the workload to the cluster nodes and performs the load in parallel. As a command COPY is quite flexible and allows for many different ways of using it, depending on your use case. Performing a COPY on Amazon S3 is as simple as the following command:
copy listing from 's3://mybucket/data/listing/' credentials 'aws_access_key_id=;aws_secret_access_key=';
For more examples on how to invoke a COPY command, you can check the COPY examples page on Amazon Redshift documentation. As in the INSERT case, the way to perform the COPY command is by connecting to your Amazon Redshift instance and then invoke the commands you want using the SQL Reference from Amazon Redshift documentation.
What is the best way to load data from Xero to Redshift and what are the possible alternatives?
So far we just scraped the surface of what can be done with Amazon Redshift and how to load data into it. The way to proceed relies heavily on the data you want to load, from which service they are coming from and the requirements of your use case. Things can get even more complicated if you want to integrate data coming from different sources.
A possible alternative, instead of writing, hosting and maintaining a flexible data infrastructure, is to use an ETL as a service product like Blendo that can handle this kind of problems automatically for you.
Blendo integrates with multiple sources or services like databases, CRM, email campaigns, analytics and more. Quickly and safely move all your data from Xero to Redshift and start generating insights.