Search code examples
zapierxero-api

Xero - oauth flow with zapier


I am investigating adding a webhook in zapier that would use the xero invoicing api to send an invoice. Xero don't have a zapier action to do this.

I was thinking of using the oauth-2 beta, but it has a complex flow that isn't ideal for my use case of just connecting to one organisation in a simple restful setup.

Any ideas on how I can set this up?


Solution

  • I’m no Zapier expert, but I’ve managed to create an OAuth 2.0 integration with an action that successfully sends an invoice. It did take a few tweaks. I did it via the UI but you could obviously use the CLI tool if you prefer. If anyone has any tips to improve this please let me know!

    Create your new Integration and select OAuth 2.0 for your authentication. Set up the redirect url, client id and client secret as normal. Add some scopes (accounting.transactions is required to email an invoice) and the

    Tick the option for “I want to automatically refresh on unauthorized error”

    For the test endpoint use GET https://api.xero.com/connections

    Now, go to Test Authentication and go through the authorization flow to connect your organisation. The response from the test call will include the tenantId which you can copy to your clipboard.

    enter image description here

    Click “Save and Finish” then go to the Advanced section from the left hand nav. Here you can add a TENANT_ID environment variable and paste in the tenantId value you have in your clipboard.

    enter image description here

    Now you’ve set up your authorization you can create the action to email an invoice. Click Add Action and fill out the name etc. On the Input Designer tab add InvoiceID as an input field.

    enter image description here

    Then go to the API Configuration tab and Configure your API Request. Here you’ll need to switch to code mode:

    enter image description here

    There are three things you’ll need to add/change from the standard template

    1. Add the InvoiceID input field into the URL
    2. Add the xero-tenant-id header with the TENANT_ID environment variable
    3. Hard code a response object e.g. {“Result”:”Success”}. This is because Zapier doesn’t like the empty response body you get back from the Xero API. There’s probably a less hacky way to do this but it does the job.

    Now if you test the API request with a valid InvoiceID from your organisation the email will be sent from Xero.

    I’ve put my integration up on Github which includes other basic examples like retrieving invoices and creating contacts. Hopefully it's helpful.