Xero Accounting API - Upload File
@sergio
code:
data:privatelast updated:3 years ago
today
Build integrations remarkably fast!
You're viewing a public workflow template.
Sign up to customize, add steps, modify code and more.
Join 1,000,000+ developers using the Pipedream platform
steps.
trigger
HTTP API
Deploy to generate unique URL
This workflow runs on Pipedream's servers and is triggered by HTTP / Webhook requests.
steps.
download_file_to_tmp
auth
to use OAuth tokens and API keys in code via theauths object
params
Url path

URL path where the file to download is located, without the filename.

 
string ·params.url_path
Target download_filename

Filename of the file to download.

 
string ·params.target_download_filename
code
Write any Node.jscodeand use anynpm package. You can alsoexport datafor use in later steps via return or this.key = 'value', pass input data to your code viaparams, and maintain state across executions with$checkpoint.
async (event, steps, params) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
}
20
const axios = require('axios')

if (!params.url_path || !params.target_download_filename) {
  throw new Error("Must provide url_path, and target_download_filename parameters.");
}

const config = {
  method: 'GET',
  url: `${params.url_path}${params.target_download_filename}`,
  responseType: 'arraybuffer',
}

const resp = await axios(config)
const fs = require("fs");
const buffer = Buffer.from(resp.data.toString('base64'), "base64");
fs.writeFileSync(`/tmp/${params.target_download_filename}`, buffer);
this.attachment_filename = params.target_download_filename;
steps.
xero_accounting_api_upload_file
Uploads a file to the specified document.
auth
(auths.xero_accounting_api)
params
Tenant id

Id of the organization tenant to use on the Xero Accounting API. See Get Tenant Connections for a workflow example on how to pull this data.

 
string ·params.tenant_id
Attachment filename

Name of the file to upload as an attachment to the Xero document.

 
string ·params.attachment_filename
Document type

Document type of where the attachment will be sent to. This is used in as part of the Xero Account API endpoint where the request is sent against.

string ·params.document_type
Document id

Xero identifier of the document where the attachment will be sent to.

 
string ·params.document_id
code
async (params, auths) => {
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
}
26
//See the API docs: https://developer.xero.com/documentation/api/invoices#upload-attachment
//See a workflow example of this action: https://pipedream.com/@sergio/xero-accounting-api-upload-file-p_rvCqADQ/edit

if (!params.tenant_id) {
  throw new Error("Must provide tenant_id parameter.");
}

//Creates a multipart/form-data object, adds file to upload
const FormData = require('form-data');
const fs = require("fs");
data = new FormData();
const file = fs.createReadStream(`/tmp/${params.attachment_filename}`);
data.append('file', file);

//Sends the request against Xero Accounting API
return await require("@pipedreamhq/platform").axios(this, {
  method: "post",
  url: `https://api.xero.com/api.xro/2.0/${params.document_type}/${params.document_id}/Attachments/${attachment_filename}`,
  headers: {
    Authorization: `Bearer ${auths.xero_accounting_api.oauth_access_token}`,
    "xero-tenant-id": params.tenant_id
  },
  data
});