Uploading Attachment to Xero using Xero node or HTTP

Hi There,

Been messing around with n8n for a few weeks now - good job guys!! Next best thing to sliced bread!

I’ve been trying to create a workflow where i can generate an invoice to a customer as well as attach an excel sheet. I run a query using the postgres node and from that data generate a spreadsheet. So far all good from here.

Next i want to create an invoice using the Xero node (also, so far so good). Then use the invoice ID that was created in the previous node and do an update to the earlier created invoice and attach the spreadsheet file (binary). The current node does not support this as according to xero’s documentation the scope required for uploading attachments is accounting.attachments.

I have also tried using the HTTP Request node - but not getting too much luck with the Authentication.

I guess my question is 2 fold:

  1. To add the accounting.attachment scope to the existing node - is this possible?
  2. Has anybody had success with the HTTP Request node to authenticate with Xero? And if so, would it be possible to share what you did to get it working?

Thanks in advance.

Hi @kevintee, welcome to the community!

I am sorry to hear you’re having trouble. Adding a new scope requires changing n8n’s source code, in particular this file: n8n/XeroOAuth2Api.credentials.ts at master · n8n-io/n8n · GitHub

You should, however, be able to use Generic OAuth2 credentials as well. The file above shows what n8n’s Xero credentials use and you should be able to configure the same setting in the UI. Can you confirm which problem exactly you are getting when trying to authenticate?

Sorry for the late reply.

I have been trying the Generic OAuth2 credentials as mentioned - the good thing is that I am not getting any unauthorised messages. However I am getting an error message saying that the scope is invalid, unknown or malformed. As per Xero’s documentation (Scopes — Xero Developer) I am using the accounting.attachments scope.

Have not tried playing around with the source code as that seems a little bit more complex for me.

Can you confirm how exactly you are setting these scopes in your generic OAuth2 credentials? The scopes should be separated by a space which might be counterintuitive. I tried out the settings below including the accounting.attachments scope and the authentication worked as it should:

If that’s not it, could you share the full error you are getting?

I was using Client Credentials as the Grant Type - this was giving me the error where is said that the “scope is invalid, unknown or malformed”.

I have changed to Authorization Code and was able to connect to my Xero account successfully with the required scopes. However when I try pushing a spreadsheet as an attachment to an invoice, I now get this:

{“status”:“rejected”,“reason”:{“message”:“403 - "{\"Type\":null,\"Title\":\"Forbidden\",\"Status\":403,\"Detail\":\"AuthenticationUnsuccessful\",\"Instance\":\"ca306b0c-3cf9-4db4-a74d-76f5026cb6cd\",\"Extensions\":{}}"”,“name”:“Error”,“stack”:“Error: Request failed with status code 403\n at createError (/usr/local/lib/node_modules/n8n/node_modules/axios/lib/core/createError.js:16:15)\n at settle (/usr/local/lib/node_modules/n8n/node_modules/axios/lib/core/settle.js:17:12)\n at IncomingMessage.handleStreamEnd (/usr/local/lib/node_modules/n8n/node_modules/axios/lib/adapters/http.js:269:11)\n at IncomingMessage.emit (node:events:539:35)\n at endReadableNT (node:internal/streams/readable:1345:12)\n at processTicksAndRejections (node:internal/process/task_queues:83:21)”}}

This is node:

Any ideas on what I may be doing wrong here?

I am not familiar with Xero unfortunately, so I don’t know why they’d throw this error. I’ll see if I can figure it out and will report back.

Hi @kevintee, is there a chance you weren’t sending the tenant ID when running into the problem? This seems to be something Xero requires as per their community forum.

With the tenant ID provided I could attach a file to an invoice like so (that’s on the latest version of n8n):

This example uses a hard coded invoice ID for the upload, so you’ll want to change that on your side when running this workflow. But once that’s done you should get the expected result:

The attachment also shows up in the Xero UI:

image

@MutedJam - you are a legend!! It is now working. Thank you very much for your patience in guiding this through with me. Really appreciate it!

1 Like

Awesome, glad to hear this works. Thanks so much for confirming!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.