## Introduction
HubSpot is a popular CRM that offers a variety of features including lead management and export functionalities. One common use case for sales and marketing teams is exporting new leads daily into a CSV file stored on Google Drive for reporting, sharing, or further processing. However, HubSpot’s built-in lead export feature may be costly for startups or small companies especially if you need customized export frequency or format.
In this article, we will demonstrate how to replicate and replace HubSpot’s daily lead export feature using n8n, an open-source automation workflow tool. By building this workflow, you can save costs on HubSpot’s premium plans while gaining more control and customization options. This guide is intended for startup teams, automation engineers, and operations specialists who are comfortable working with APIs and automation tools.
—
## Tools and Services Integrated
– **HubSpot CRM**: Source of leads via their public API
– **Google Drive**: Destination for storing daily lead exports as CSV files
– **n8n**: Workflow automation platform to connect HubSpot and Google Drive and automate the export
—
## Use Case Overview
– **Problem solved:** Automate daily retrieval of new leads added in HubSpot and save them into Google Drive as a timestamp-based CSV file.
– **Who benefits:** Sales and marketing teams who want daily, automated lead data exports without manual work or costly HubSpot plans.
—
## Step-by-Step Technical Tutorial
### Prerequisites
1. An active HubSpot account with API access (an API key or OAuth credentials depending on your setup).
2. A Google account with access to Google Drive where you can save files.
3. n8n installed locally or hosted (https://n8n.io).
4. Basic understanding of HTTP APIs and JSON.
### 1. Configure Your HubSpot API Credentials
– Obtain your HubSpot API key or configure OAuth 2.0 credentials for API authentication.
– In n8n, create new credentials for HubSpot:
– Go to **Credentials** > **Create new** > choose HubSpot API credentials
– Enter your API key or OAuth credentials
### 2. Set up Google Drive Credentials in n8n
– In n8n, create new Google Drive credentials:
– Go to **Credentials** > **Create new** > Google Drive OAuth2
– Follow the OAuth consent to grant n8n access to your Google Drive
### 3. Build the Workflow
Open n8n Editor UI and follow these steps:
#### Step 3.1: Trigger Node – Cron
– Add a **Cron** node to schedule the workflow.
– Configure it to run once every day at your preferred time (e.g., 1:00 AM).
#### Step 3.2: HubSpot Node – Retrieve New Leads
– Add an **HTTP Request** node to call HubSpot’s API for contacts (leads).
– Configuration:
– HTTP Method: GET
– URL: `https://api.hubapi.com/crm/v3/objects/contacts`
– Query Parameters:
– `properties=firstname,lastname,email,phone` (select fields you want to export)
– `limit=100` (or more depending on expected volume)
– Use filters to retrieve only new leads added since the last run. For this, use the `createdAt` filter via the search endpoint or use incremental logic described below.
– Authentication: Use the HubSpot credentials you set in step 1.
**Incremental Fetching Tip:**
Store the timestamp of the last successful run (using n8n’s **Set** or **Function** nodes with persistent storage or external database) and only request leads created after that timestamp to avoid re-processing all leads every time.
#### Step 3.3: Process and Format Data
– Add a **Function** node that takes the hubspot contacts list and maps the data into a flat JSON array suitable for CSV export.
Example JavaScript function:
“`javascript
const leads = $json[“results”] || [];
return leads.map(lead => ({
firstName: lead.properties.firstname || ”,
lastName: lead.properties.lastname || ”,
email: lead.properties.email || ”,
phone: lead.properties.phone || ”
}));
“`
#### Step 3.4: Convert JSON to CSV
– Add the **Spreadsheet File** node to convert the JSON array from the previous step into CSV format.
– Configure it:
– Operation: `Write`
– File Format: `CSV`
– Options: Enable first row as header
#### Step 3.5: Upload the CSV to Google Drive
– Add the **Google Drive** node to upload the generated CSV.
– Configure:
– Operation: `Upload`
– File Name: Use a dynamic name like `leads_export_{{ $now.toISOString().slice(0,10) }}.csv` to include the current date
– Specify the folderId where the file will be stored
– Binary Data: Pass the CSV from the previous node
—
### 4. Handling Common Errors and Tips
– **API Rate Limits:** HubSpot API has usage limits; implement pagination and error retries in the HTTP Request node.
– **Authentication Failures:** Ensure your API keys and OAuth tokens are valid and refreshed if using OAuth.
– **Large lead volumes:** If you have thousands of leads, implement pagination in your HTTP calls and fetch leads in batches.
– **Data Consistency:** Use a persistent state or external storage for the last export timestamp to avoid duplicates.
– **File Storage Limits:** Monitor your Google Drive quota to ensure you don’t run out of storage.
—
### 5. Scaling and Customization
– **Add Filtering:** Filter leads by lifecycle stage or other HubSpot properties in the API query.
– **Multiple Exports:** Duplicate this workflow or add logic to export different lead segments.
– **Error Notifications:** Add Slack or email notification nodes to alert your team on workflow failures.
– **Data Enrichment:** Integrate with other APIs or databases to enrich leads before export.
– **Archive Old Files:** Automate deletion or archiving of old CSV files from Google Drive.
—
## Summary
By implementing this n8n workflow, you have replaced HubSpot’s built-in lead export feature with a flexible, cost-effective automation that exports new leads daily to Google Drive as CSV files. This gives startups and automation teams better control over data selection, frequency, and future extensibility without incurring additional HubSpot charges.
### Bonus Tip
To maintain incremental exports accurately, use n8n’s **Workflow Settings > Execute Once** state or external key-value stores (like Redis or Airtable) to track the last export timestamp. This lets you query HubSpot only for leads created after the last successful export, optimizing API usage and performance.