Giter Site home page Giter Site logo

Comments (3)

ksvirkou-hubspot avatar ksvirkou-hubspot commented on June 2, 2024

Hi @alex-mironov
It hasn't supported yet.

from hubspot-api-nodejs.

rnwolfe avatar rnwolfe commented on June 2, 2024

@alex-mironov I was looking into this API endpoint support, as well, but you can use the manual call functions to handle the endpoints for now. Here's what I did in my Node.js app as an example:

/**
 * Attempt to opt-in a HubSpot Contact to marketing emails. This should **only** be done with user consent.
 * 
 * @param {email} email The email to attempt to opt in to marketing subscription.
 * @returns {Promise} The HubSpot API response object if successful, otherwise logs an error to system.
 */
exports.optInToMarketingEmails = async email => {
  // Have to do a manual API call because subscription preferences are not yet wrapped in the SDK
  // @see: https://github.com/HubSpot/hubspot-api-nodejs/issues/71
  return hubspotClient
    .apiRequest({
      method: 'POST',
      path: '/communication-preferences/v3/subscribe',
      body: {
        emailAddress: email,
        subscriptionId: await this.getMarketingSubscriptionId(),
        legalBasis: 'LEGITIMATE_INTEREST_PQL',
        legalBasisExplanation:
          email +
          ' expressed interest in further emails when requesting resources at an event.'
      }
    })
    .then(response => {
      logging.systemInfo(`${email} gave promotional consent and was opted into HubSpot Marketing emails.`)
      return response.body;
    })
    .catch(e => {
      const body = e.response.body;
      if (body.message.includes('they have unsubscribed') || body.message.includes('already subscribed')
      ) {
        // Log as informational if contact has previously unsubscribed or is already subscribed.
        logging.systemInfo(`${e.response.body.category}: ${e.response.body.message}`, LOG_TYPE);
      } else {
        logging.systemError(`${e.response.body.category}: ${e.response.body.message}`, LOG_TYPE);
      }
    });
};

/**
 * Gets all existing subscription definitions in the HubSpot instance.
 * 
 * @returns {Promise} All existing subscription definitions in the HubSpot instance.
 */
exports.getSubscriptionDefinitions = async () => {
  return hubspotClient
    .apiRequest({
      method: 'GET',
      path: '/communication-preferences/v3/definitions'
    })
    .then(response => response.body);
};

/**
 * Get the subscription statuses for any of the Contact's subscriptions.
 * 
 * @param {email} email The HubSpot Contact's email 
 * @returns {Promise} A HubSpot API response object with the Contact's communciation preferences.
 */
exports.getUserCommunicationPreferences = async email => {
  return hubspotClient
    .apiRequest({
      method: 'GET',
      path: '/communication-preferences/v3/status/email/' + email
    })
    .then(response => response.body);
};

/**
 * Get the subscription ID for the *first* Marketing subscription returned.
 * 
 * @returns {Promise<string>} The first Marketing subscription's ID.
 */
exports.getMarketingSubscriptionId = async () => {
  const definitions = await this.getSubscriptionDefinitions();
  // This can likely be handled better if multiple subscriptions exist
  return definitions.subscriptionDefinitions.find(sub => sub.purpose === 'Marketing').id;
};

The function names could probably be improved, but the usage of communication preferences, subscriptions, and other terminology is a bit intermixed in the documentation.

Also, I'm sure the use of .then().catch() as well as async/await is triggering to many, but I just don't like the try/catch paradigm.. 😄

Hope this helps someone.

from hubspot-api-nodejs.

ksvirkou-hubspot avatar ksvirkou-hubspot commented on June 2, 2024

HI @alex-mironov
The API was add in version 4.1.0

hubspotClient.communicationPreferences

from hubspot-api-nodejs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.