APIs & Integrations

Rowan450
Member

How to upload a file using Hubspot File Uploading API with Nodejs, Axios AWS Lambda,

i,m using a lambda function to send clients side uploaded files to the hubspot api. On the client side I encode the file to base64 and then invoke the lambda function with the data. I,m trying to upload the base64 Encoded file, but I can't find the proper way to pass the file or what the api expects. I was only able to find this line:

"“file”: {binary data}"

The error I receive is:

 

Error: Request failed with status code 400

 

I think the only thing that goes wrong is passing the file, but i dont know what format the api expects because the documentation are not that clear. Also the error response isn't very helpfull either.

  1. Are all the values of the var options correctly filled in?
  2. How to correctly pass the file  file: Buffer.from(event.data.drawing, 'base64'),?
  3. Is there any way to get more helpfull axios error responses or debugging advice?

DOCS: https://developers.hubspot.com/docs/api/files/files

'use strict';
const axios = require("axios");

exports.handler = async (event, context) => {
    const apiKey = "myApiKey";

   //event.data.drawing the BASE64 encoded string

    var options = {
        method: 'POST',
        baseURL: 'https://api.hubapi.com',
        url: '/files/v3/files',
        params: { hapikey: apiKey },
        headers: { accept: 'application/json', 'content-type': 'multipart/form-data' }, 
        data: {
            options: {
                access: "PRIVATE",
                ttl: 'P2W',
                overwrite: false,
                duplicateValidationStrategy: 'NONE',
                duplicateValidationScope: 'NONE',
            },
            folderPath: 'offerte_aanvragen',
            file: Buffer.from(event.data.drawing, 'base64'),
        },
        responseType: 'json',
    };


    return new Promise((resolve, reject) => {
        axios(options).then((response) => {
            if (response.statusText !== 'OK') {
                return reject(response);
            } else {
                resolve(response)
            }

        }).catch(error => {
            reject(error);
            console.log(error)
        });
    })
}

Source:
https://stackoverflow.com/questions/68378935/how-to-upload-a-file-using-hubspot-file-uploading-api-w...

I,m having a hard time figuring out what goes wrong or in what format I need to pass the file.

Thanks in advance!

0 Upvotes
1 Reply 1
webdew
Guide | Diamond Partner
Guide | Diamond Partner

How to upload a file using Hubspot File Uploading API with Nodejs, Axios AWS Lambda,

Hi @Rowan450 ,

Please use below code in node function :

var postUrl = 'https://api.hubapi.com/filemanager/api/v3/files/upload?hapikey='+HAPI_KEY;

var filename = 'example_file.txt';

var fileOptions = {
access: 'PUBLIC_INDEXABLE',
ttl: 'P3M',
overwrite: false,
duplicateValidationStrategy: 'NONE',
duplicateValidationScope: 'ENTIRE_PORTAL'
};

var formData = {
file: fs.createReadStream(filename),
options: JSON.stringify(fileOptions),
folderPath: 'docs'
};

request.post({
url: postUrl,
formData: formData
}, function optionalCallback(err, httpResponse, body){
return console.log(err, httpResponse.statusCode, body);
});

Hope this helps!


If we were able to answer your query, kindly help the community by marking it as a solution.

Thanks and Regards.