Woodshop for old agent generation
Try the new agent generation
  • What is woodshop
  • How to's
    • Smart Relationship
      • GetIdsFromRequest
    • Smart views
      • Display a calendar view
      • Create a custom tinder-like validation view
      • Create a custom moderation view
      • Create a dynamic calendar view for an event-booking use case
    • Configure environment variables
      • NodeJS/Express projects
    • Elasticsearch Integration
      • Interact with your Elasticsearch data
      • Elasticsearch service/utils
      • Another example
    • Zendesk Integration
      • Authentication, Filtering & Sorting
      • Display Zendesk tickets
      • Display Zendesk users
      • View tickets related to a user
      • Bonus: Direct link to Zendesk + change priority of a ticket
    • Dwolla integration
      • Display Dwolla customers
      • Display Dwolla funding sources
      • Display Dwolla transfers
      • Link users and Dwolla customers
      • Dwolla service
    • Make filters case insensitive
    • Use Azure Table Storage
    • Create multiple line charts
    • Create Charts with AWS Redshift
    • View soft-deleted records
    • Send Smart Action notifications to Slack
    • Authenticate a Forest Admin API against an OAuth protected API Backend
    • Translate your project into TypeScript
      • V8
        • Migrate Mongoose files
        • Migrate Sequelize files
      • v7
        • Migrate Mongoose files
        • Migrate Sequelize files
      • v6
    • Geocode an address with Algolia
    • Display/edit a nested document
    • Send an SMS with Zapier
    • Hash a password with bcrypt
    • Display a customized response
    • Search on a smart field with two joints
    • Override the count route
    • Make a field readOnly with Sequelize
    • Hubspot integration
      • Create a Hubspot company
      • Display Hubspot companies
    • Impersonate a user
    • Import data from a CSV file
    • Import data from a JSON file
    • Load smart fields using hook
    • Pre-fill a form with data from a relationship
    • Re-use a smart field logic
    • Link to record info in a smart view
    • Display data in html format
    • Upload files to AWS S3
    • Display AWS S3 files from signed URLs
    • Prevent record update
    • Display, search and update attributes from a JSON field
    • Add many existing records at the same time (hasMany-belongsTo relationship)
    • Track users’ logs with morgan
    • Search on relationship fields by default
    • Export related data as CSV
    • Run automated tests
  • Forest Admin Documentation
Powered by GitBook
On this page
  • Requirements
  • How it works
  • Directory: /forest
  • Directory: /routes

Was this helpful?

  1. How to's
  2. Hubspot integration

Display Hubspot companies

PreviousCreate a Hubspot companyNextImpersonate a user

Last updated 4 years ago

Was this helpful?

This example shows you how to create a smart collection to list the companies of your Hubspot account.

Requirements

  • An admin backend running on forest-express-sequelize

  • a Hubspot account

How it works

Directory: /forest

This directory contains the hubspot-companies.js file where the collection is declared.

/forest/hubspot-companies.js
const collection = require('forest-express-sequelize');

collection('hubspot_companies', {
  isSearchable: true,
  fields: [
    {
      field: 'id',
      type: 'Number',
    }, {
      field: 'name',
      type: 'String',
    }, {
      field: 'hubspot_link',
      type: 'String',
    },
  ],
});

Directory: /routes

This directory contains the hubspot-companies.js file where the serializer for the collection and logic to get records is defined.

The Hubspot API key is defined in the .env file and requested through the expression process.env.HUBSPOT_API.

/routes/hubspot-companies.js
const Liana = require('forest-express-sequelize');
const express = require('express');
const superagent = require('superagent');
const JSONAPISerializer = require('jsonapi-serializer').Serializer;

const router = express.Router();

// define the serializer used to format the payload
const hubspotCompaniesSerializer = new JSONAPISerializer('hubspotCompanies', {
  attributes: ['name', 'hubspotLink'],
  keyForAttribute: 'underscore_case',
  id: 'companyId',
  transform(record) {
    record.name = record.properties.name.value;
    record.hubspotLink = `https://app.hubspot.com/contacts/6332498/company/${record.companyId}`;
    return record;
  },
});

function getHubspotCompaniesList(limit, offset) {
  return superagent
    .get(`https://api.hubapi.com/companies/v2/companies/paged?hapikey=${process.env.HUBSPOT_API}&properties=name&limit=${limit}&offset=${offset}`)
    .then((response) => JSON.parse(response.res.text));
}

async function getAllHubspotCompanies() {
  let allHubspotCompanies = [];
  let hasMore = true;
  let offset = '';
  while (hasMore) {
    let getCompaniesResponse = await getHubspotCompaniesList(250, offset);
    allHubspotCompanies = allHubspotCompanies.concat(getCompaniesResponse.companies);
    offset = getCompaniesResponse.offset;
    hasMore = getCompaniesResponse['has-more'];
  }
  return allHubspotCompanies;
}

function searchHubspotCompanies(companies, search) {
  return companies.filter((item) => {
    return item.properties.name.value.toUpperCase().includes(search.toUpperCase());
  });
}

router.get('/hubspot_companies', Liana.ensureAuthenticated, async (req, res, next) => {
  // set pagination parameters when exist (default limit is 250 as it is the max allowed by Hubspot)
  let limit = req.query.page ? parseInt(req.query.page.size) : 20;
  let offset = req.query.page ? (parseInt(req.query.page.number) - 1) * limit : 0;

  // set search terms when exist
  let search = null;
  search = req.query.search ? req.query.search : search;

  let hubspotCompanies = await getAllHubspotCompanies();
  if (search) {
    hubspotCompanies = searchHubspotCompanies(hubspotCompanies, search);
  }
  const count = hubspotCompanies ? hubspotCompanies.length : null;
  const paginateHubspotCompanies = hubspotCompanies.slice(offset, offset + limit);
  const serializedCompanies = hubspotCompaniesSerializer.serialize(paginateHubspotCompanies);
  return res.send({ ...serializedCompanies, meta: { count } });
});

module.exports = router;

npm package

Companies information are obtained by making a call to the Hubspot API.

superagent
get all companies