There are a couple of existing examples of calling the API from Go (ae0000/goxero, jpillora/xero-api.go), but we’ll demonstrate how to do it using a simple self-contained command line application that just creates invoices. The full source is available on GitHub, along with pre-built executables for Mac, Linux and Windows. If you’d like to build it yourself and you’ve set your
GOPATH then you can simply run
go get github.com/chasertech/go-xero, otherwise you can clone the repository manually and run
go build. It’s a Xero Private Application so you’ll need private and consumer keys to use it (see the previous post for details on how to obtain these). Then to create an invoice just run:
The code itself is fairly straightforward. First we parse the command line arguments:
Then we create an XML representation of an invoice (unfortunately you can’t send JSON to the Xero API yet):
And finally we POST the XML, with an appropriate OAuth signature (thanks to Gary Burd’s Go-OAuth library):
This is a very basic example, so there’s plenty of room for improvement. Potential features include error handling (which was omitted for brevity but should always be used in a real application!) and support for creating and updating other entities such as contacts, payments and credit notes. But hopefully this application provides some inspiration - and we’d love to hear your ideas!