# Need to upload multiple pdf files using File API- Node

**URL:** <https://community.hubspot.com/t/need-to-upload-multiple-pdf-files-using-file-api-node/125883>\
**Category:** APIs & Integrations\
**Tags:** starter\
**Created:** [February 11, 2025, 7:12pm UTC](https://community.hubspot.com/t/need-to-upload-multiple-pdf-files-using-file-api-node/125883 "2025-02-11T19:12:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![AngelKanna](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@AngelKanna](https://community.hubspot.com/u/AngelKanna)\
**Post date:** [February 11, 2025, 7:12pm UTC](https://community.hubspot.com/t/need-to-upload-multiple-pdf-files-using-file-api-node/125883/1 "2025-02-11T19:12:10Z")

</div>

The below code works for a single file upload, but if I have an array of file paths

filePaths = [‘abc.pdf’, ‘sample.pdf’,‘xyz.pdf’] all files are in current directory. When I loop over the array, the code just uploads the last file 3 times to hubspot folder. I think fs.createReadStream() should be async. please advise

 ![AngelKanna_0-1739300992719.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/7/e/7e3b44367f4f9866f2e08e8bc1f645c0463be79d.png)

const axios = require(‘axios’);

const FormData = require(‘form-data’);

const fs = require(‘fs’);

let data = new FormData();

const filePaths = [‘abc.pdf’, ‘sample.pdf’,‘xyz.pdf’];

async function toCall(){

for(var i=0; i\< filePaths.length; i++){

await getFormData(filePaths[i]);

let config = {

method: ‘post’,

maxBodyLength: Infinity,

url: ‘[https://api.hubapi.com/files/v3/files/](https://api.hubapi.com/files/v3/files/)’,

headers: {

```
'Authorization': 'Bearer \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*',

...data.getHeaders()

```

},

data : data

};

axios.request(config)

.then((response) =\> {

console.log(JSON.stringify(response.data));

})

.catch((error) =\> {

console.log(error);

});

}

}

async function getFormData(path)

{

```
data.append('file', fs.createReadStream(path));

data.append('folderId', '\*\*\*\*\*\*\*\*\*\*');

data.append('options', '{"access":"PRIVATE"}');

```

}

toCall();

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [February 12, 2025, 6:50pm UTC](https://community.hubspot.com/t/need-to-upload-multiple-pdf-files-using-file-api-node/125883/2 "2025-02-12T18:50:06Z")

</div>

Hey, @AngelKanna 👋

Here’s what I’d look at:

- Creating new FormData for each file vs. reusing for the same instance
- Use proper async/await handling versus mixing with .then()
- Look into using Promise.all for doing concurrent uploads
- Add error handling to both the individual files and the batch process

Best,

Jaycee

---

<div class="post-metadata">

**Author:** ![AngelKanna](https://avatars.discourse-cdn.com/v4/letter/a/54ee81/32.png) [@AngelKanna](https://community.hubspot.com/u/AngelKanna)\
**Post date:** [February 19, 2025, 1:38pm UTC](https://community.hubspot.com/t/need-to-upload-multiple-pdf-files-using-file-api-node/125883/3 "2025-02-19T13:38:49Z")

</div>

Yes, I used async/await and error handling, so I can upload each file perfectly. Thanks for the suggestions, it helped me to reach a perfect solution
