Skip to content

Instantly share code, notes, and snippets.

@equivalent
Last active September 29, 2022 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save equivalent/9e3174a471b138401b420bff506ac09e to your computer and use it in GitHub Desktop.
Save equivalent/9e3174a471b138401b420bff506ac09e to your computer and use it in GitHub Desktop.
Hubspot CRM API Delete a secondary email address

Hubspot CRM API Delete a secondary email address

how to delete secondary email from a Contact in Hubspot CRM via a API

module MyHubspotHelperLib
SecondaryEmailCannotBeRemoved = Class.new(StandardError)
def self.remove_secondary_email_from_contact(contact_id, email)
url = "https://api.hubapi.com/contacts/v1/secondary-email/#{contact_id}/email/#{email}"
headers = {
'Content-Type' => 'application/json',
'Authorization': "Bearer #{private_app_token}"
}
res = HTTParty.delete(url, { headers: headers }) # Ruby gem https://github.com/jnunemaker/httparty
case res.code
when 200, 201
res # all good
else
raise SecondaryEmailCannotBeRemoved.new(res['message'])
end
end
def self.private_app_token
# Rails.application.credentials.dig(:hubspot, Rails.env.to_sym, :private_app_token)
"pat-xxx-xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
end
end
begin
MyHubspotHelper.remove_secondary_email_from_contact('123456', 'secondary@email.com')
rescue MyHubspotHelper::SecondaryEmailCannotBeRemoved => e
if e.message.match("contact does not have that secondary email")
# this just means the email is no longer associated as secondary email
elsif e.message.match("is associated with a different vid")
# this just means the email is no longer secondary email but it's own contact
else
raise e
end
end