Chaser Tech
News and views from the engineering team at Chaser

Calling the Xero API from the command line using Go

In a recent post we described getting started with the Xero API using Node.js, which is great if you’re building a full-stack web application with JavaScript. But if you’re more interested in using the API from the command line (for example to create or update data for testing purposes), or you’re considering a microservices architecture, then the Go programming language could be a good alternative.

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, 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:

./go-xero --consumer-key ... --private-key-path privatekey.pem \
--contact dd1dcb58-a767-4d49-b77c-eda94fbc2cf5 \
--description "Food for thought"
--amount 42

The code itself is fairly straightforward. First we parse the command line arguments:

Type := flag.String(
        "Invoice Type. See")
ContactID := flag.String(
        "Contact ID. See")
Description := flag.String(
        "Monthly rental for property at 56a Wilkins Avenue",
        "Line Item Description. See")
LineAmount := flag.Float64(
        "Line Item Amount. See")

Then we create an XML representation of an invoice (unfortunately you can’t send JSON to the Xero API yet):

type LineItem struct {
  Description string
  LineAmount  float64
type Invoice struct {
  Type      string
  ContactID string     `xml:"Contact>ContactID"`
  LineItems []LineItem `xml:"LineItems>LineItem"`
type Invoices struct {
  Invoices Invoice `xml:"Invoice"`
v := &Invoices{Invoice{*Type, *ContactID, []LineItem{{*Description, *LineAmount}}}}
output, _ := xml.Marshal(v)

And finally we POST the XML, with an appropriate OAuth signature (thanks to Gary Burd’s Go-OAuth library):

pem_data, _ := ioutil.ReadFile(*PrivateKeyPath)
block, _ := pem.Decode(pem_data)
private_key, _ := x509.ParsePKCS1PrivateKey(block.Bytes)
client := oauth.Client{
  Credentials:     oauth.Credentials{Token: *ConsumerKey},
  SignatureMethod: oauth.RSASHA1,
  PrivateKey:      private_key,
res, _ := client.Post(nil, &client.Credentials, "", url.Values{"xml": {string(output)}})

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!