2

In NodeJS, I'm using "@hubspot/api-client": "^7.1.2".

Created hubspot client using accessToken as follows

const hubSpotClient = new hubspot.Client({ accessToken });

When I try to update the contact using email it's throwing error

Request:

const idProperty = 'email';
    
const response = await hubSpotClient(store).crm.contacts.basicApi.update(email, idProperty, contact);

Response:

ERROR   {
  "statusCode": 404,
  "body": {
    "status": "error",
    "message": "Object not found.  objectId are usually numeric.",
    "correlationId": "71e911d3...",
    "context": {
      "id": [
        "testemail@..."
      ]
    },
    "category": "OBJECT_NOT_FOUND"
  }

Create contact is working fine with this client but updating by email is not working. Anything out of place or syntax error in passing the idProperty?

Shea Hunter Belsky
  • 2,815
  • 3
  • 22
  • 31
Mokesh S
  • 683
  • 6
  • 11

2 Answers2

2

The problem is in your implementation, because it seems like you are not using properly the Hubspot API.

If you check the function signature of the basicApi.update

public async update(contactId: string, simplePublicObjectInput: SimplePublicObjectInput, idProperty?: string, _options?: Configuration): Promise<RequestContext> {

Basically, you need to pass down a contactId, and then a simplePublicObjectInput that is basically an object that represents your update.

Your code should look like this:

import { Client } from "@hubspot/api-client";
const hubspotClient = new Client({ accessToken: YOUR_ACCESS_TOKEN });

const contactID = 1234;
const response = await hubspotClient.crm.contacts.basicApi.update(contactID, {
  properties: { email: 'my-new-email@gmail.com' },
})

Keep in mind that Hubspot always tries to follow their same guidelines as their endpoints. If your check the endpoint specification you will see the following:

Hubspot Contact Update API

Think about the Hubspot node client as just an abstraction of some http client, but at the end does exactly the same as the endpoints described in their implementations.

For that reason, in your implementation, Hubspot is returning an appropriated error, since you are not giving the contactId in the first argument, Hubspot is telling you: "Object not found. objectId are usually numeric." Because indeed a Contact ID is numeric and you are using the value of an email --string-- instead.

If you want to "update by email" I think that there is no direct way to do it, you might need to do a previous search of the contact by email. You could use the searchApi.

Hubspot Contact Search API

And after getting the id just run the update.

const searchResponse = await hubspotClient.crm.contacts.searchApi.doSearch({
  filterGroups: [
    {
      filters: [
        {
          value: 'email-to-search@gmail.com',
          propertyName: 'email',
          operator: 'EQ',
        },
      ],
    },
  ],
  sorts: [],
  properties: [],
  limit: 1,
  after: 0,
});

// Off course you need to improve a lot the error handling here and so on.
// This is just an example
const [contactID] = searchResponse.results;

const contactUpdateResponse = await hubspotClient.crm.contacts.basicApi.update(contactID, {
  properties: { email: 'my-new-email@gmail.com' },
})

I hope this helps you!

0

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;
  }
});