Automated Email Categorization with Google Sheets and OpenRouter AI - n8n Workflow

Use this powerful n8n workflow to automatically categorize and label incoming Gmail threads using AI, based on custom definitions stored in a Google Sheet. Explore n8n templates for AI email processing.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Business Owners: Who need systematic organization of large volumes of inbound emails (e.g., support, sales leads, press inquiries).
Technical Users and Developers: Seeking practical n8n templates demonstrating advanced AI integration (Langchain/OpenRouter) with core services like Gmail and Google Sheets.
Automation Specialists: Looking for a reusable n8n node sequence to implement robust, scheduled email sorting routines.
Teams: That define their email organization rules externally (in Google Sheets) and require automated enforcement.

Overview

Managing high volumes of incoming emails can quickly become overwhelming, leading to missed priorities and manual administrative work. This n8n workflow solves this by creating a hands-off categorization system. It periodically checks for new, uncategorized emails in Gmail and uses a powerful AI agent (via OpenRouter) to determine the correct category based on definitions you provide in a Google Sheet.

This setup allows for dynamic rule changes—just update your Google Sheet definitions, and the n8n automation adapts immediately. By leveraging a scheduled n8n trigger, this entire process runs efficiently in the background, ensuring your inbox remains organized and fully labeled without manual intervention. This advanced use of the n8n node system greatly improves efficiency in email management.

How it Works

This automation runs on a scheduled n8n trigger, configured to execute every hour.


  1. Configuration Retrieval: The n8n workflow first retrieves configuration settings, including the Google Sheet URL containing categorization rules, a Gmail search filter, and the maximum number of emails (limit) to process in a single run.

  2. Fetching Categories: The workflow reads the category names and their detailed definitions from the specified Google Sheet via the Google Sheets n8n node.

  3. Fetching Unlabeled Emails: A Gmail n8n node searches for email threads that currently has:nouserlabels (are uncategorized), applying the optional extra_filter.

  4. AI Categorization: Each retrieved email thread is passed to the AI Agent n8n node. This agent, connected to the OpenRouter Chat Model (like Deepseek), uses the email subject and body text, along with the category definitions retrieved from Google Sheets, to determine the single, best-fitting category name.

  5. Preparation for Loop: The identified category name and the thread ID are temporarily stored.

  6. Looping and Labeling: The workflow then enters a batch loop.

  7. Label Creation (Idempotent): It attempts to create the category as a new Gmail label. If the label already exists, the onError setting ensures the flow continues without disruption.

  8. Filtering Label ID: The workflow retrieves all existing Gmail labels and uses a Filter n8n node to match the AI's output category name to the actual Gmail Label ID.

  9. Applying Label: Finally, a Gmail n8n node uses the retrieved Label ID to add the corresponding label to the original email thread, completing the automated organization task.

Installation Guide

To deploy this comprehensive n8n workflow, follow these steps:


  1. Import the n8n Workflow: Copy the provided JSON and import it into your n8n instance via the 'Workflows' > 'New' > 'Import from JSON' option.

  2. Google Sheets Setup: Duplicate the template Google Sheet linked in the 'Sticky Note' (or create your own) to define your categories and descriptions. Ensure the column headers are 'Name' and 'Definition'.

  3. Configure Credentials:

Google Sheets: Set up or select your Google Sheets OAuth2 credential.
Gmail: Set up or select your Gmail OAuth2 credential, ensuring it has necessary permissions to read messages and manage labels.
* OpenRouter/AI: Create an OpenRouter API key and configure the 'OpenRouter Chat Model' n8n node with this credential.

  1. Update Configuration: Navigate to the 'Config' n8n node and update the sheetsurl with the URL of your new category definitions sheet. Adjust the limit or extrafilter as needed.

  2. Activate: Enable the 'Schedule Trigger' n8n trigger to start the scheduled automation.

Node Details

Schedule Trigger (n8n trigger): Sets the execution cadence for the n8n workflow, configured here to run every hour.
Config (Set n8n node): Acts as a central configuration hub. Key parameters include sheets_url (Source of truth for categories) and limit (Controls API usage by restricting the number of emails processed per run).
Get Labels (Google Sheets n8n node): Reads structured data (Name and Definition) from the configured spreadsheet, providing the AI agent with the context needed for categorization.
Get Messages (Gmail n8n node): Queries Gmail for uncategorized emails using the search filter has:nouserlabels to ensure the n8n workflow only acts on new or unprocessed items.
AI Agent (Langchain n8n node): Orchestrates the decision-making process. Its prompt dynamically injects the email content and the Google Sheets category data, compelling the LLM to output a precise category name.
OpenRouter Chat Model (AI Model n8n node): Supplies the intelligence for this n8n automation. It uses a specific, often free, LLM model (like Deepseek) hosted on the OpenRouter service.
Create Label if Doesn't exist (Gmail n8n node): A crucial step that uses error handling (onError: continueRegularOutput) to ensure the n8n workflow is idempotent, creating the label only if it doesn't exist yet.
Filter (Filter n8n node): Matches the plain-text output from the AI agent to the unique system id of the corresponding Gmail label, which is required for the final labeling action.

Related n8n Workflows

Free

Nodes: 9 Nodes
Updated: December 26 2025
View all
Created by
Milan Vasarhelyi
Milan Vasarhelyi

Featured*