# Display Hubspot companies

{% hint style="warning" %}
Please be sure of your agent type and version and pick the right documentation accordingly.
{% endhint %}

{% tabs %}
{% tab title="Node.js" %}
{% hint style="danger" %}
This is the documentation of the `forest-express-sequelize` and `forest-express-mongoose` Node.js agents that will soon reach end-of-support.

`forest-express-sequelize` v9 and `forest-express-mongoose` v9 are replaced by [`@forestadmin/agent`](https://docs.forestadmin.com/developer-guide-agents-nodejs/) v1.

Please check your agent type and version and read on or switch to the right documentation.
{% endhint %}
{% endtab %}

{% tab title="Ruby on Rails" %}
{% hint style="success" %}
This is still the latest Ruby on Rails documentation of the `forest_liana` agent, you’re at the right place, please read on.
{% endhint %}
{% endtab %}

{% tab title="Python" %}
{% hint style="danger" %}
This is the documentation of the `django-forestadmin` Django agent that will soon reach end-of-support.

If you’re using a Django agent, notice that `django-forestadmin` v1 is replaced by [`forestadmin-agent-django`](https://docs.forestadmin.com/developer-guide-agents-python) v1.

If you’re using a Flask agent, go to the [`forestadmin-agent-flask`](https://docs.forestadmin.com/developer-guide-agents-python) v1 documentation.

Please check your agent type and version and read on or switch to the right documentation.
{% endhint %}
{% endtab %}

{% tab title="PHP" %}
{% hint style="danger" %}
This is the documentation of the `forestadmin/laravel-forestadmin` Laravel agent that will soon reach end-of-support.

If you’re using a Laravel agent, notice that `forestadmin/laravel-forestadmin` v1 is replaced by [`forestadmin/laravel-forestadmin`](https://docs.forestadmin.com/developer-guide-agents-php) v3.

If you’re using a Symfony agent, go to the [`forestadmin/symfony-forestadmin`](https://docs.forestadmin.com/developer-guide-agents-php) v1 documentation.

Please check your agent type and version and read on or switch to the right documentation.
{% endhint %}
{% endtab %}
{% endtabs %}

## Display Hubspot companies

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

{% embed url="<https://1726799947-files.gitbook.io/~/files/v0/b/gitbook-28427.appspot.com/o/assets%2F-M0vHiS-1S9Hw3djvoTw%2F-MJBsYyaWtLJBqiVmjEn%2F-MJBuFL3UHMbW0EMqiJi%2Fhubspot%20list%20companies.gif?alt=media&token=f19240f1-fbee-49bf-ac60-e76261d165d4>" %}

### Requirements

* An admin backend running on forest-express-sequelize
* [superagent](https://www.npmjs.com/package/superagent) npm package
* a Hubspot account

### How it works

#### Directory: /forest

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

{% code title="/forest/hubspot-companies.js" %}

```javascript
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',
    },
  ],
});
```

{% endcode %}

#### Directory: /routes

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

Companies information are obtained by making a [get all companies](https://developers.hubspot.com/docs/methods/companies/get-all-companies) call to the Hubspot API.

{% hint style="info" %}
The Hubspot API key is defined in the `.env` file and requested through the expression `process.env.HUBSPOT_API`.
{% endhint %}

{% code title="/routes/hubspot-companies.js" %}

```javascript
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;
```

{% endcode %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.forestadmin.com/documentation/reference-guide/integrations/hubspot/display-hubspot-companies.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
