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:
- Create a new folder for your application and
cd
into it in a terminal window. - 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 ...
) - Initialise your application:
npm init -y
- Install an NPM module to simplify making API calls:
npm install --save request
- Create a
main.js
file with the following contents. Note thatconsumer_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:
- Explore the other API endpoints by referring to the documentation in the Xero Developer Centre (and especially the really useful API previewer)
- Check out the prebuilt Code Samples and Libraries and well as the various third-party modules
- Get help and inspiration from the Developer Community (all 37,513 of them!)
- Make your Private application Public by mastering OAuth. See API Application Types for more information.
- Read more about Xero’s plans for the platform, including real-time updates via webhooks
- Find more advanced tutorials such as building a Xero app using React
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