## Introduction
HubSpot is a leading CRM platform widely used by sales and marketing teams to manage leads and customer interactions. One of its useful features is the ability to export new leads daily to Google Drive in CSV format for further processing, reporting, or integration with other tools. However, HubSpot’s pricing for this advanced automation and export functionality can become costly for startups and small teams.
In this guide, we will demonstrate how to create a robust, scalable, and cost-efficient automation workflow using **n8n**, an open-source automation tool, to replicate HubSpot’s lead export feature. This tutorial is aimed at startup CTOs, automation engineers, and operations specialists looking to reduce SaaS expenses while maintaining operational workflows.
—
## Problem Statement
**What problem does this solve?**
HubSpot’s daily lead export feature allows teams to export new leads into Google Drive automatically. This feature helps sales teams keep updated offline backups or trigger further actions like emailing lead lists, feeding data into other CRMs, or running external analytics.
However, the HubSpot plans that include automated exports and integration capabilities can be expensive. Smaller companies or startups may find themselves paying for features they barely use or want more control over the export format, timing, or triggers.
By building an **n8n automation**, you:
– Save subscription fees for HubSpot advanced features.
– Gain full control over the export schedule, CSV format, and destinations.
– Easily integrate additional services in the workflow.
**Who benefits?**
– Startup sales and marketing teams who want daily lead exports without paying extra.
– Operations teams looking for customizable data export flows.
– Automation engineers seeking extensible workflows with fine-tuned control.
—
## Tools and Services Integrated
– **HubSpot CRM API**: To query new leads daily.
– **n8n Automation Platform**: To build and orchestrate the workflow.
– **Google Drive API**: To save exported lead data as CSV files.
Additional optional integrations:
– Slack or Email for notifications.
– Google Sheets for alternative export destinations.
—
## Overview of the Workflow
### Workflow Trigger
– A **Cron** node triggers the workflow once per day at a specific time.
### Main Steps
1. Query HubSpot’s API to retrieve newly created leads since the last export.
2. Process and transform the JSON lead data into CSV format.
3. Upload the CSV file to a designated Google Drive folder with a date-stamped filename.
4. Optionally notify the team of the export completion.
—
## Step-by-Step Technical Tutorial
### Prerequisites
– An n8n instance (self-hosted or hosted service).
– HubSpot API access with an API key or OAuth credentials.
– Google Drive API access with OAuth credentials set up in n8n.
### Step 1: Set up the Cron Trigger
– Add a **Cron** node.
– Configure it to trigger at your desired time (e.g., every day at 7 AM).
### Step 2: Retrieve Last Export Timestamp
– To export only new leads, the workflow needs to know the last time leads were exported.
– Use n8n’s **Set** node or **Variable Store** feature to store the timestamp of the last successful export.
– Initially, set a reasonable default timestamp (e.g., “1970-01-01T00:00:00Z”).
### Step 3: Fetch New Leads from HubSpot
– Add an **HTTP Request** node.
– Configure the node to make a GET request to HubSpot’s Contacts API endpoint:
– **Endpoint:** `https://api.hubapi.com/crm/v3/objects/contacts`
– Use query parameters to filter contacts created after the last export timestamp, e.g., `properties=firstname,lastname,email,createdate` and `createdate__gt=last_export_timestamp`.
– Use the HubSpot API key or OAuth to authenticate.
– This node fetches only leads created since the last export.
### Step 4: Transform JSON Response to CSV
– Add a **Function** node to convert the JSON array of lead data into CSV format.
– Example JavaScript code for the node:
“`javascript
const leads = items[0].json.results;
if (!leads || leads.length === 0) {
return [];
}
const csvHeaders = [‘First Name’, ‘Last Name’, ‘Email’, ‘Created Date’];
let csv = csvHeaders.join(‘,’) + ‘\n’;
leads.forEach(lead => {
const firstName = lead.properties.firstname || ”;
const lastName = lead.properties.lastname || ”;
const email = lead.properties.email || ”;
const createdDate = lead.properties.createdate || ”;
csv += `”${firstName}”,”${lastName}”,”${email}”,”${createdDate}”\n`;
});
return [{ json: { csvData: csv } }];
“`
### Step 5: Upload CSV to Google Drive
– Add a **Google Drive** node.
– Use the **Upload File** operation.
– For the file content, set from the previous node’s CSV data.
– Set the file name dynamically using the current date, e.g., `hubspot-leads-{{ $now.toISOString().slice(0,10) }}.csv`.
– Select the appropriate folder in Google Drive where these exports will be saved.
### Step 6: Update Last Export Timestamp
– After a successful upload, update the last export timestamp store to the current date/time.
– Use a **Set** node or n8n’s **Workflow Settings** to store this timestamp for the next run.
### Optional Step 7: Notify Team
– Add a **Slack** or **Email** node to send a notification indicating the export succeeded.
—
## Error Handling and Tips for Robustness
– **API Rate Limits:** HubSpot limits API requests, so use pagination for lead queries and handle rate limitations by adding retry nodes or delays.
– **No New Leads Scenario:** Ensure the workflow gracefully handles empty lead results by skipping CSV upload.
– **Data Validation:** Trim and validate lead properties to avoid CSV injection or malformed files.
– **OAuth Token Refresh:** If using OAuth credentials, configure n8n to refresh tokens automatically.
– **File Overwrites:** Include timestamps in filenames to prevent overwriting previous exports.
– **Logging:** Log workflow runs and errors via n8n’s built-in logs or external monitoring.
—
## Scaling and Adaptations
– **Multiple HubSpot Portals:** Extend the workflow to iterate over multiple HubSpot accounts by adding loops.
– **Additional Lead Properties:** Modify the JSON to CSV transformation to include custom lead fields.
– **Export Frequency:** Adjust the Cron trigger to run more or less frequently as needed.
– **Alternative Destinations:** Export leads to Google Sheets, Amazon S3, or other cloud storage.
– **Enrich Data:** Add enrichment nodes to fetch more info on leads before export.
—
## Summary
Replacing HubSpot’s daily lead export feature with a self-hosted n8n workflow offers significant cost savings and operational flexibility. By integrating HubSpot’s API, JSON-to-CSV transformation, and Google Drive upload nodes, teams can automate daily lead exports customized exactly to their needs.
**Bonus Tip:** Combine this workflow with automated data quality checks or lead scoring steps in n8n to build a more complex lead management ecosystem without additional SaaS costs. With n8n’s extensibility, this is just the foundation for a fully automated sales stack tailored to your startup’s growth.
—
This approach demonstrates the power of open-source automation platforms like n8n in replacing expensive SaaS features while empowering teams with full control and transparency over their workflows.