Hi!
Im trying to create a serverless function that uploads a file with the hubspot api. I tried alot but can not get it to work. Please help me.
exports.main = async (context, sendResponse) => {
const hs = require('@hubspot/api-client')
const FormData = require('form-data');
const HUBSPOT_API_KEY = process.env.HUBSPOT_API_KEY;
const { file } = context.body;
const content = Buffer.from(file.split(',')[1], 'base64');
const hubspot = new hs.Client({
apiKey: HUBSPOT_API_KEY
});
var fileOptions = {
access: 'PUBLIC_INDEXABLE'
};
const formData = new FormData();
formData.append("folderId", '<folderID>');
formData.append("options", JSON.stringify(fileOptions));
formData.append("file", content);
try {
const response = await hubspot.apiRequest({
method: 'POST',
path: '/filemanager/api/v3/files/upload',
body: formData
});
sendResponse({
body: {
message: 'File uploaded successfully.',
},
statusCode: 200,
});
} catch (error) {
// Limit log to 4k characters
const errorMessage = error.message.substring(0, 2000);
sendResponse({
body: {
message: 'An error occurred while uploading the file.',
error: errorMessage,
},
statusCode: 500,
});
}
};
I have tried alot with the endpoint https://api.hubapi.com/files/v3/files as well but can´t get it to work.
Thanks in advance!
/Mats