Skip to content

Instantly share code, notes, and snippets.

@kaelri
Last active March 18, 2025 21:09
Show Gist options
  • Save kaelri/93a428e2bca942d365495237a2945915 to your computer and use it in GitHub Desktop.
Save kaelri/93a428e2bca942d365495237a2945915 to your computer and use it in GitHub Desktop.
/**
* Create or update a list of contacts in Hubspot.
* @param registrants Array of contact objects, e.g. from an external data source.
* @returns The JSON response from Hubspot.
*/
async function hubspotCreateOrUpdateContacts( contacts, hubspotAPIKey ) {
let hubspotContacts = contacts.map( registrant => {
return {
email: registrant.email,
properties: [
{ property: 'firstname', value: registrant.firstName },
{ property: 'lastname', value: registrant.lastName },
]
}
});
let result = await fetch( 'https://api.hubapi.com/contacts/v1/contact/batch/', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${hubspotAPIKey}`
},
body: JSON.stringify( hubspotContacts )
})
.then( response => response );
if ( result.statusText !== 'Accepted' ) {
let errorDescription = await result.text();
throw new Error( `hubspot-update-failed: ${errorDescription}` );
}
console.info( `${contacts.length} contacts were created or updated.` );
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment