Update contact with email as ID through the Hubspot API

Hi

I’m trying to update a property of a contact through the api.

My issue is that I can’t get it to work with the email as the id.

Here it says that it is possible to use the email as the id when updating a contact:

it states that I have to set the “idProperty” to email in order for it to work, but I can’t figure out where to set it?

I have tried setting it as a regular property but that throws an error.

This is the code that I’m using:

const hubspotClient = new hubspot.Client({“apiKey”:“???”});

const properties = {

    "newsletter\_opened": dateValue,

};

const SimplePublicObjectInput = { properties };

const contactId = "emailGoesHere";

try {

    const apiResponse = await hubspotClient.crm.contacts.basicApi.update(contactId, SimplePublicObjectInput);

    console.log(JSON.stringify(apiResponse.body, null, 2));

} catch (e) {

    e.message === 'HTTP request failed'

    ? console.error(JSON.stringify(e.response, null, 2))

    : console.error(e)

}

@Chrintz

You will set the Id property as a query param. eg: ?idProperty=email

It is buried in the middle of this doc

Thanks.

Using the “@hubspot/api-client” i would assume that it is done like this:

const idProperty = "email";

 try {
 const apiResponse = await hubspotClient.crm.contacts.basicApi.update(contactId, SimplePublicObjectInput, idProperty);

but it gives me the error:

2021-12-16T12:47:22.535Z ERROR {
 "statusCode": 404,
 "body": {
 "status": "error",
 "message": "Object not found. objectId are usually numeric.",
 "correlationId": "71e911d3...",
 "context": {
 "id": [
 "mike.thomas@..."
 ]
 },
 "category": "OBJECT_NOT_FOUND"
 },

@Teun , you see anything out of place here?

I’m getting the same error using @hubspot/api-client and trying to get a contact using the idProperty of email.

Can anyone share how it’s supposed to be done with the api-client? It works fine with ?idProperty=email using the REST api, but we need to migrate to the api-client.

If anyone has figured this out it would be highly appreciated!

@dennisedson @Teun @Chrintz Ok I figured this out. I could not find any documentation and some of the StackOverflow comments are completely wrong, but through a lot of trial and error, I was able to figure out what you need to do.

You CAN use email as the idProperty for the hubspot/api-client get contact function, but it only works if you fill in all the other query fields before idProperty, even if they are undefined.
Here is my example of a getContactByEmail as a Google Cloud Function in Node, using the api-client, and it works great!

exports.getContactByEmail = functions.https.onCall(async (data, context) => {
 const email = data.email;
 const contactId = email;
 const properties = ["firstname", "lastname", "company"];
 const propertiesWithHistory = undefined;
 const associations = undefined;
 const archived = false;
 const idProperty = "email";

 try {
 const apiResponse = await hubspotClient.crm.contacts.basicApi.getById(
 contactId,
 properties,
 propertiesWithHistory,
 associations,
 archived,
 idProperty
 );

 console.log(JSON.stringify(apiResponse.body, null, 2));

 return apiResponse.properties;

 } catch (error) {
 error.message === "HTTP request failed"
 ? console.error(JSON.stringify(error.response, null, 2))
 : console.error(error);
 return error;
 }
});

Thank you for sharing, @brown2020 :raising_hands:

This looks like exactly what I need! since I need some more details on how the response looks like, since I using a different language (Python) are you around to answer?

This is the URL you have to use (it is based on explanation below from somebody else):
https://api.hubapi.com/crm/v3/objects/contacts/xxx@yyy.com?idProperty=email

xxx@yyy.com is the email ID you use to find the record. Its amazing that this simple and basic instructions are not included in the documentation of the API interface.
Your problem has already been solved, but if somebody like me uses google and finds your thread it could be helpful for that person.
Cheers!

The original question—how to update a contact using email address as the idProperty—has not been addressed. The solution presented shows how to get a contact using email address as idProperty, which works fine. When I use (copied from the codebase on GitHub):

 # Update
 # Perform a partial update of an Object identified by `{contactId}`. `{contactId}` refers to the internal object ID. Provided property values will be overwritten. Read-only and non-existent properties will be ignored. Properties values can be cleared by passing an empty string.
 # @param contact_id [String] 
 # @param simple_public_object_input [SimplePublicObjectInput] 
 # @param [Hash] opts the optional parameters
 # @option opts [String] :id_property The name of a property whose values are unique for this object type
 # @return [SimplePublicObject]
 def update(contact_id, simple_public_object_input, opts = {})
 data, _status_code, _headers = update_with_http_info(contact_id, simple_public_object_input, opts)
 data
 end

as:

 @helper.api_client.crm.contacts.basic_api.update(
 # contact_id: properties[:hs_object_id],
 contact_id: properties[:email],
 body: {
 properties: properties,
 },
 idProperty: "email",
 )

I receive:

"status":"error","message":"Object not found. objectId are usually numeric."

I’m able to update custom objects similarly using idProperty, but not contacts. I really don’t want to have to fetch a contact to get its hs_object_id every time I push an update. The “solution” here implies doing a GET to fetch a contact and then use its objectId for updates.