Back to listing

Create an Excel export from a HubSpot object

Category:
Custom-Coded Workflow Action, Operations Pro/Ent

ETL:
Extract

Notes

The imports and exports API is pretty new in HubSpot but it adds many new capabilities. 

The exports API is particularly useful for grabbing an Excel file of a specific object type. This code snippet does exactly that and is ideally combined with a scheduled workflow action in Operations Hub Professional/Enterprise. A common use case is to back up data in another system or handle some internal reporting process that depends on static snapshots at specific time intervals. 

Code

        

Language: JavaScript



 
const axios = require('axios');
const accessToken = process.env.secretName;
 
exports.main = async (event, callback) => {
const propertyName = "email";
  const propertyValue = event.inputFields['propertyValue'];
  const objectType = "contact";
 
  const apiUrl = 'https://api.hubapi.com/crm/v3/exports/export/async';
 
  const headers = {
    authorization: `Bearer ${accessToken}`,
    'content-type': 'application/json'
  };
 
  const data = {
    exportType: 'VIEW',
    exportName: `Recurring export of ${objectType} from HubSpot`,
    format: 'xlsx',
    language: 'EN',
    objectType: objectType,
    objectProperties: [
      'email',
      'hs_object_id'
    ],
    publicCrmSearchRequest: {
      filters: [
        {
          value: propertyName,
          propertyName: propertyValue,
          operator: 'EQ'
        }
      ],
      sorts: [
        {
          propertyName: 'createdate',
          order: 'DSC'
        }
      ]
    }
  };
 
  axios
    .post(apiUrl, data, { headers })
    .then(response => {
      const exportId = response.data.id;
      console.log("The export ID is: " + exportId);
      
      callback({
        outputFields: {
          exportId: exportId
        }
      });
    })
    .catch(error => {
      console.error(error.message);
    });
};

How can SpotDev support your business?

HubSpot Migrations

Move from Salesforce, Dynamics, Pipedrive or any CRM to HubSpot with SpotDev.

Learn more

HubSpot Integrations

Add advanced functionality to your HubSpot portal with API development services.

Learn more

RevOps

Align your sales, marketing and service teams to break down silos and trigger growth.

Learn more