Master n8n in 1 Hour: Automate Workflows & Build AI Agents

The AI AutomatorsAbout 8 min readMay 19, 2025Watch original
THE SUMMARYAI-generated

Key Concepts

  • N8N: A no-code automation platform for building workflows and AI agents.
  • Workflows: Automated sequences of actions triggered by events or schedules.
  • Nodes: Individual steps within a workflow, performing specific tasks.
  • Triggers: Starting points of workflows, initiating execution based on events or schedules.
  • Actions: Operations performed on data or external applications within a workflow.
  • Credentials: Securely stored authentication details for connecting to external services.
  • Expressions: JavaScript code snippets used to manipulate data and create dynamic workflows.
  • Data Transformation: Modifying data formats or values using dedicated nodes.
  • Routing: Directing workflow execution based on conditions or data values.
  • AI Integration: Incorporating artificial intelligence models for tasks like spam detection and content generation.
  • LLM Chains: N8N's implementation of Langchain, enabling integration with various AI models.
  • AI Agents: Intelligent entities within N8N capable of interacting with users and external tools.
  • Looping: Iterating over data sets to perform actions on each item.
  • HTTP Request Node: A versatile node for interacting with external APIs.
  • Debugging: Identifying and resolving errors within workflows.

N8N Basics and Setup

  • Two Deployment Options:
    • N8N Cloud: Cloud-hosted, easy setup, but can be expensive with many active workflows.
    • Self-Hosting: Virtually unlimited active workflows, cost-effective, requires managing the application.
  • Railway.com: Recommended for easy self-hosting with a one-click N8N template.
  • Allesio: A service for deploying N8N with advanced management features (link in description).
  • Initial Setup: Create a workflow, which presents a blank canvas. Use Ctrl + Mouse Wheel to zoom.

Workflow Triggers

  • Trigger Selection: Start a workflow by selecting the "+" icon and choosing a trigger.
  • Trigger Types:
    • Manual: Starts the workflow manually.
    • External App Event: Triggers based on events in external applications (e.g., form submissions, database updates).
    • Scheduled: Runs workflows on a defined schedule (e.g., daily, weekly).
    • Webhook: Triggers upon receiving a webhook call.
    • Chat Message: Uses N8N's built-in chat interface.
    • N8N Form Submission: Triggers when an N8N form is submitted (used as the initial example).
  • Example: Using an N8N form submission as a customer support form.

Building a Basic Workflow: Form Submission to Google Sheets

  • Form Creation: Create a form with fields like "Full Name," "Email Address," "Issue Type" (dropdown), and "Message."
  • Form Element Types: Use appropriate element types (e.g., "email" for email validation).
  • Testing the Workflow: Use the "Test Workflow" button to generate an N8N form for testing.
  • Output Panel: The right-hand panel displays the output of each node in table, JSON, and schema views.
  • Pinning Data: Use the "Pin" feature to save the output of a node, allowing you to reuse the data for testing without re-entering it.
  • Connecting to Google Sheets:
    • Add an "Action in App" node and select "Google Sheets."
    • Choose "Append Row in Sheet."
    • Create a new credential to connect to your Google account using OAuth 2.
    • For self-hosted N8N, setting up a Google Cloud Console project is required.
    • Select the target spreadsheet and sheet.
    • Manually map form fields to Google Sheet columns using drag-and-drop.
    • N8N creates JavaScript expressions ($json) to reference data from previous nodes.
  • Testing the Step: Click "Test Step" to append the data to the Google Sheet.

Data Transformation: Formatting Dates

  • Adding a Transformation Node: Insert a "Date & Time" -> "Format Date" node between the form submission and Google Sheets nodes.
  • Mapping Updated Values: Update the Google Sheets node to use the formatted date from the new "Format Date" node.
  • Importance of Correct Mappings: Ensure that the correct data fields are mapped after adding or modifying nodes.

Routing: Handling Different Issue Types

  • Using the Switch Node: Add a "Switch" node to route workflow execution based on the "Issue Type" field.
  • Defining Routing Rules: Create rules to check if the "Issue Type" is equal to "Technical Issues" or not.
  • Sending Emails Based on Issue Type:
    • Add a "Gmail" -> "Send Message" node for each route.
    • Create a new Gmail credential using OAuth 2.
    • Configure the email subject, recipient, and body.
    • Include relevant form data in the email body.
  • Duplicating Nodes: Use the "Duplicate" function to quickly create similar nodes for different routes.
  • Testing Different Routes: Update the test data to trigger different routes and ensure they function correctly.

AI Integration: Spam Detection

  • Adding an OpenAI Node: Insert an "OpenAI" -> "Message that Model" node to analyze form submissions for spam.
  • Creating an OpenAI Credential:
    • Create an OpenAI account and generate an API key.
    • Copy the API key into the N8N credential settings.
  • Configuring the OpenAI Prompt:
    • Define a prompt to determine if the message is spam.
    • Request a structured JSON response with a "spam_rating" field (true or false).
  • Using an If Node: Add an "If" node to check the "spam_rating" value.
  • Creating a "Spam" Sheet: Create a separate Google Sheet to store suspected spam submissions.
  • Routing Spam Submissions: Route spam submissions to the "Spam" sheet instead of the main sheet.

Advanced Spam Checks: Module Conversions

  • Adding a Text Check: Implement a text check before the OpenAI node to identify obvious spam keywords.
  • Using the "Text Match" Node: Configure the "Text Match" node to use a regular expression (regex) to match spam keywords.
  • Converging Flows: Route spam submissions identified by the text check directly to the "Spam" sheet, bypassing the OpenAI node.
  • Benefits of Converging Flows: Reduces AI costs and improves efficiency by quickly filtering out common spam.

LLM Chains: Integrating with Various AI Models

  • Replacing OpenAI with LLM Chain: Remove the OpenAI node and add an "LLM Chain" node.
  • Connecting to Anthropic Chat Model:
    • Create an Anthropic account and generate an API key.
    • Create a new credential in N8N and paste the API key.
    • Select the desired Anthropic model (e.g., Claude 3.5 Sonnet).
  • Using Structured Output Parser:
    • Add a "Structured Output Parser" to the LLM Chain.
    • Provide a JSON example to define the desired output format.
  • Updating the If Node: Update the "If" node to use the "spam_rating" from the LLM Chain's output.

Debugging Workflows

  • Identifying Errors: Look for error messages in the bottom right corner of the N8N interface.
  • Using the Assistant: The assistant can analyze errors and suggest solutions.
  • Checking Executions: Review past executions to identify the source of errors.
  • Retrying Executions:
    • Retry from the node with the error.
    • Retry with the currently saved workflow (after fixing the error).
  • Debugging in Editor: Use the "Debug in Editor" feature to import execution data and test the workflow with that data.

HTTP Request Node: Interacting with External APIs

  • Purpose: Connect to external systems that do not have built-in N8N modules.
  • Example: Integrating with file.ai, an AI image generation tool.
  • Finding API Endpoints: Consult the API documentation for the target service.
  • Authentication:
    • Use predefined credentials for supported services.
    • Use generic credential types (e.g., "Header Auth") for custom authentication.
    • Common authentication methods: Basic Auth, OAuth, Header Auth.
  • HTTP Methods: Use appropriate HTTP methods (e.g., GET, POST, DELETE, PATCH).
  • Request Body: Provide a request body in JSON format when using the POST method.

AI Agent Node: Building Intelligent Agents

  • Purpose: Create intelligent entities within N8N capable of interacting with users and external tools.
  • Key Features:
    • Built-in memory (retains conversation history).
    • Ability to call external tools and workflows.
  • Creating a Basic AI Agent:
    • Add a "Chat Trigger" node to receive user input.
    • Add an "AI Agent" node.
    • Assign an OpenAI chat model (or other supported model).
    • Select "Window Buffer Memory" for storing conversation history.
  • Adding Tools:
    • Add an "HTTP Request" node to call external APIs (e.g., file.ai for image generation).
    • Use placeholders to define input parameters for the tool.
  • System Prompt: Provide a system prompt to guide the agent's behavior and define its role.
  • Chat URL: Make the chat publicly available by activating the workflow and accessing the chat URL.

Multiple Input Triggers

  • Purpose: Allow a workflow to be triggered by multiple different events or sources.
  • Example: A social media agent that can be triggered by WhatsApp or N8N's chat trigger.
  • Implementation:
    • Add multiple trigger nodes to the workflow.
    • Use an "If" node to determine which trigger was activated.
    • Merge the data from the different triggers into a single flow.

Looping and Data Flow

  • Handling Multiple Items: N8N often handles multiple items (e.g., rows from a Google Sheet) automatically in the background.
  • Split Out Node: Use the "Split Out" node to separate an array of items into individual items.
  • Loop Over Items Node: Use the "Loop Over Items" node to process items one by one or in batches.
  • Rate Limiting: Use the "Loop Over Items" node with a "Wait" node to avoid exceeding API rate limits.
  • Error Handling: Configure the "Loop Over Items" node to continue on error or continue using error output.
  • Always Output Data: Select the "Always Output Data" option to prevent the workflow from stopping prematurely if the loop responds with no data.

AI summaries can miss context or contain errors. Check important details against the original video.

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.