javascriptnode.jshubspothubspot-api

HubspotClient - Update contact by email id is not working


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?


Solution

  • 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!