Xero is a popular cloud based accounting software. Slack is popular cloud based Team chat tool. Slack is also very suitable for building integrations into it and automating business processes.

Here is a small example of how to build simple integration where when an invoice is created in Xero then notification with details is posted to a certain Slack channel. I’m going to use Google Cloud Functions with Nodejs to process Xero webhooks and post them to Slack.

Steps to get this to work are the following:

  • Register webhooks in Xero to send notification to Google Cloud Functions every time a new invoice is created.
  • Process these notifications in Google Cloud Functions log them and then send them to Slack.
  • Receive the notifications in Slack channel.
  1. Registering webhooks in Xero

In Xero we are going to register Private Application. You can do it here.

To register private app we need to upload certificate to Xero.

You can get the client certificate for Google Cloud Functions from here. Choose Json and download the file. Alt Text

Xero requires the certificate to be in x509 format saved as .cert file which Google doesn’t seem to offer. But in the downloaded JSON file you can find link to the x509 cert which you can copy and paste to Xero.

Cert url

Paste it to Xero:

XERO private app certicicate with Google Cloud Function X509

Now that we have the certificate succesfully saved to Xero we can create the webhook.

Creating webhook in Xero

Xero wants to verify that the webhook is working and this brings to part 2.

  1. Creating the Google Cloud Function to process the incoming Xero webhook.

First lets edit the package.json with dependencies so it looks like this:

Dependencies

Now lets add the code for the cloud function which is the following:

const crypto = require('crypto');
const fetch = require('node-fetch');

exports.invoiceToSlack = async (req, res) => {
  try {
    const xeroSignature = req.headers['x-xero-signature'];
    const webhookKey = 'YOUR_KEY';
    const slackWebhookUri = 'https://hooks.slack.com/services/DSADSAD/SDLJ#LLKJJL/DASKJJSAJÖDSAÖJDSA';
    const hash = crypto.createHmac('sha256', webhookKey).update(req.rawBody.toString()).digest('base64');

    if (hash === xeroSignature) {
      const data = req.body;
      for (const event of data.events) {
        if (event.eventCategory === 'INVOICE' && event.eventType === 'CREATE') {
          let invoiceUrl = `https://go.xero.com/AccountsReceivable/Edit.aspx?InvoiceID=${invoice.resourceId}`;
          
          const response = await fetch(slackWebhookUri, {
            method: 'POST',
            headers: {
              'Content-type': 'application/json',
            },
            body: JSON.stringify({
              text: `New invoice has been posted to Xero. 
              ${invoiceUrl}
              `
            })
          });
          if (response.ok) {
            console.log("Successfully processed Xero Webhook");
          }
        }
      }
      res.status(200).send();
    } else {
        console.log("Unauthorized access try")
        res.status(401).send();
    }
  } catch (error) {
    console.log(error)
  }
};

Here we first get Xero Signature from the request headers. Then we calculate the hash to and compare it against the signature using our key.

If it matches then we loop through the data of the request body and make postings to Slack.

Slack webhook Url can be attained by registering your app to process webhooks in Slack

This is the function in google cloud:

Function

Now the function is ready we can save and activate it and start sending invoices from Xero to Slack.

  1. Receiving the notification in Slack:

Receiving the webhook in Slack