Notes
This function allows you to create a batch of contacts as a serverless function in CMS Enterprise.
You'll need to send the data from the front end of your application/site. This specific code assumes that you're doing so in a payload very similar to how the HubSpot API expects it. If that's not the case, you'll want to add a function to transform the payload before triggering the axios.
Code
Language: JavaScript
const axios = require('axios');
const accessToken = `Bearer 1234`; // Make sure you're using secrets to populate this variable
exports.main = (context, sendResponse) => {
let objectType = "contact";
const url = `https://api.hubapi.com/crm/v3/objects/${objectType}/batch/create`;
const headers = {
'Authorization': accessToken,
'Content-Type': 'application/json'
};
const inputData = context.body.inputs; // Extract the inputs array from the received data
const formattedData = { inputs: [] }; // Initialize an empty object with inputs property
// Iterate over each input object and format it as required
// If your front-end isn't sending fully structured data, you might need to do additional processing with this.
// You will want to adjust this for loop to handle the properties of your records
for (const input of inputData) {
const formattedInput = {
properties: {
email: input.email,
firstname: input.firstname,
lastname: input.lastname
}
};
formattedData.inputs.push(formattedInput); // Push the formatted input to the new array
}
axios.post(url, formattedData, { headers })
.then(response => {
console.log('Batch create successful');
sendResponse({ body: response.data, statusCode: 200 });
})
.catch(error => {
console.log('Batch create error');
// You will want more sophisticated error handling here
sendResponse({ body: error.message, statusCode: 400 });
});
};
How can SpotDev support your business?
HubSpot Migrations
Move from Salesforce, Dynamics, Pipedrive or any CRM to HubSpot with SpotDev.
Learn moreHubSpot Integrations
Add advanced functionality to your HubSpot portal with API development services.
Learn moreRevOps
Align your sales, marketing and service teams to break down silos and trigger growth.
Learn more