# Trying to upload PDF file- node.js

**URL:** <https://community.hubspot.com/t/trying-to-upload-pdf-file-node-js/125312>\
**Category:** APIs & Integrations\
**Tags:** starter\
**Created:** [February 3, 2025, 4:37pm UTC](https://community.hubspot.com/t/trying-to-upload-pdf-file-node-js/125312 "2025-02-03T16:37:39Z")\
**Posts on this page:** 4\
**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 3, 2025, 4:37pm UTC](https://community.hubspot.com/t/trying-to-upload-pdf-file-node-js/125312/1 "2025-02-03T16:37:39Z")

</div>

I am trying to upload PDF file using Files API endpoint, with the below code I can upload the file, but the “File Preview” shows “Failed to load PDF document”. I need help. Here’s the code

const axios= require(‘axios’);

const fs = require(‘node:fs’);

let buf;

async function readPdfFile(){

```
fs. readFile("xyz.pdf", (err,data) =>{

    if(err){

      console.error(err);

      return;

    } buf= (Buffer.from(data).toString('base64'));

  });

  const file\_blob = new Blob(\[buf], {

          type: "application/pdf",

        });

          const folderId= 'hsFolderID';

          const form\_data= new FormData();

          form\_data.append("file", file\_blob, "random3.pdf");

          form\_data.append("folderId", folderId);

          form\_data.append(

          "options",

          JSON.stringify({

          access: "PRIVATE",

          overwrite: true,

          duplicateValidationStrategy: "NONE",

          duplicateValidationScope: "EXACT\_FOLDER",

          }))

          const request\_config= {

          headers:{

              "Authorization": "Bearer " + "\*\*\*\*\*\*\*\*\*\*\*",

              "Content-Type": "multipart/form-data"

          },

          data: form\_data

          };

          const url= 'https://api.hubapi.com/files/v3/files'

          return axios.post(url, form\_data, request\_config);

}

readPdfFile()

```

 ![AngelKanna_0-1738600020502.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/0/05bff4f8a2c01e3a0f3f13ad74cd2dca00b67ed1.png)

FYI- The input file is provided by our another system and the pdf file produced by this system looks like this

{  
fileName: ‘Abc.pdf’,  
buffer: \<Buffer 24 57 45 83 89 2e 43 0a 32 21 67 34 6f 63 6a 0a 4c 3c 0a 2f 65 32 46 6c … 51377 more bytes\>  
}. Using this as input I have to upload it to hubspot and view the pdf file in hubspot

---

<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 4, 2025, 6:19pm UTC](https://community.hubspot.com/t/trying-to-upload-pdf-file-node-js/125312/2 "2025-02-04T18:19:06Z")

</div>

Hey, @AngelKanna 👋 I hope you got this worked out. If no, have you tried a simplified test by using Postman?

I just did this and was successful in viewing my uploaded PDF.

Steps:

- Made a quick test PDF using Google Docs
- Request:  
`POST https://api.hubapi.com/files/v3/files/​`  
 ![CleanShot 2025-02-04 at 11.11.47@2x.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/c/3/c3e4fe90271152b5495c79057f1c73841b9bc003.png)

 ![CleanShot 2025-02-04 at 11.17.38@2x.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/1/1b667bbb58a2c9c1ed45ecff79ffee0e6bd715fc.png)

- File preview

 ![CleanShot 2025-02-04 at 11.15.03@2x.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/a/c/ac3b874fa15992721e9466c036cdfa2d5fcf766e.png)

- This is the Node example using Axios that Postman provided

I hope this helps get you moving forward! — 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 5, 2025, 2:05pm UTC](https://community.hubspot.com/t/trying-to-upload-pdf-file-node-js/125312/3 "2025-02-05T14:05:31Z")

</div>

Thank you Jaycee\_Lewis, it worked

---

<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 7, 2025, 4:25pm UTC](https://community.hubspot.com/t/trying-to-upload-pdf-file-node-js/125312/4 "2025-02-07T16:25:27Z")

</div>

The above 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-1738945390271.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/e/e3bb157b4893f2e5e705c747dac9f36968926b41.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();
