Chaser Tech
News and views from the engineering team at Chaser

Getting started with the Xero API

The very first step in Chaser becoming Xero’s Add-on Partner of the Year was taken over two years ago when we wrote some simple code to access the Xero Accounting API. Building an app such as CHASER obviously requires a lot of time and effort but getting started with the Xero API is actually quite straightforward, even if you’re relatively new to coding. This post is a 5-step quick-start guide using JavaScript to hopefully inspire the next generation of add-on partners, and perhaps even a future Xero Developer of the Year!

Our app will be a “Private Application” (in Xero terms), which isn’t suitable for public use but does provide easy access to the whole API for personal and/or testing purposes. The instructions have been tested on Mac OS X and Linux and shouldn’t take more than 15 minutes to complete. The only prerequisite is installing Node.js. The LTS release is fine - the code will work with any version from 0.10 upwards.

The steps are:

  1. Create a new folder for your application and cd into it in a terminal window.
  2. Create a Private Application for your organisation (Demo Company or otherwise). Note: You can ignore step 3 of “Create a public/private key pair” (i.e. openssl pkcs12 ...)
  3. Initialise your application: npm init -y
  4. Install an NPM module to simplify making API calls: npm install --save request
  5. Create a main.js file with the following contents. Note that consumer_key should be the value of your 30-character Consumer Key from step 3 above.
var consumer_key = '••••••••••••••••••••••••••••••';
var request = require('request').defaults({
  oauth: {
    consumer_key: consumer_key,
    token: consumer_key,
    signature_method : 'RSA-SHA1',
    private_key: require('fs').readFileSync('privatekey.pem')
  },
  json: true
});
request('https://api.xero.com/api.xro/2.0/Organisation', function(e, r, organisation) {
  console.log(e || organisation);
});

You should now be able to run your code using node main.js and see a JSON representation of your organisation!

So, now you’ve got a super-simple app. But what’s next? Here some potential next steps in your journey as a Xero developer:

At Chaser we’re always interested in hearing from other Xero developers. So do get in touch @chaser_io if you’d like to share what you’ve built or ask about our experiences. We’d love to hear from you!

Update: Join a discussion about this post in the Xero Developer Community