Last active
March 18, 2025 21:09
-
-
Save kaelri/93a428e2bca942d365495237a2945915 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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