Hammer
Hammer

Reputation: 13

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?

Upvotes: 1

Views: 701

Answers (2)

AndrewGL
AndrewGL

Reputation: 28

Thanks for posting this! Been wanting to do some custom Xero integrations in Zapier and this was immensely helpful.

I can post, but can't comment on the above thread. It's super helpful and is the right answer, but here's a few of things I needed to update to make it work:

  1. Add offline_access to the scope in order to receive the refresh_token parameter. Without that scope, I received an error in Zapier about not receiving a refresh_token.

  2. As part of configuring the API request, remove params, line 11 in the screenshot here.

  3. Zapier's input (line 2 of the last screenshot) should be in the format {{bundle.inputData.BankTransactionID}}, not ${bundle.inputData.BankTransactionID}

  4. There's no longer a need to "hack" the const results line; however, you do need to wrap results in a bracket so that it returns an array, not an object: return [results];

Upvotes: 0

Adam Moore
Adam Moore

Reputation: 371

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.

Upvotes: 8

Related Questions