Skip to content

How to Create and Testing n8n Custom Node: A Beginner's Guide

Part of guide: N8N TutorialsNodes and Integrations

Watch the Video Tutorial

💡 Pro Tip: After watching the video, continue reading below for detailed step-by-step instructions, code examples, and additional tips that will help you implement this successfully.

Table of Contents

Open Table of Contents

Introduction: Unleash the Power of Custom Nodes in n8n

Are you looking to extend the capabilities of n8n? Do you need a specific integration that isn’t available out-of-the-box? Learning how to create and testing n8n custom node is the perfect solution. This guide will walk you through the process, step-by-step, making it easy even if you’re a beginner. Think of it like building your own LEGO set for automation!

The image shows a screen capture from a tutorial video about creating a custom node in n8n. On the left, there is text that reads 'Preview... We're going to build this' above a custom node icon. The icon is a cloud with a gear inside a rounded square. On the right, there is an n8n interface with the title 'City Weather default'. The 'Parameters' tab is selected. The interface includes fields such as 'Credential to connect with', 'City', 'Additional Fields', 'Format', and 'Metric'. A hand cursor is hovering over the 'Add Field' dropdown. A man is visible in the lower right corner of the image.

This guide provides a beginner-friendly approach to how to create and testing n8n custom node, empowering you to tailor the platform to your exact needs. We’ll focus on declarative nodes, which are the most common and easiest to manage. Trust me, you’ll be surprised how quickly you pick it up!

Why Create Custom Nodes in n8n?

When you need functionality that isn’t readily available, you have a few options:

  1. HTTP Request Node: For simple API calls. Think of this as your basic Swiss Army knife for grabbing data from the web.
  2. Code Node: For running JavaScript or Python code. This is where you can get fancy and write custom logic.
  3. Custom n8n Node: For reusable, integrated solutions. This is the ultimate power move – creating your own building block for n8n!

While the first two options are useful, creating a custom node offers several advantages:

The image is a screen capture from a video featuring a man in front of a bookshelf. A node labeled 'Custom Node' is displayed prominently on the screen. The node has an icon of a cloud with a gear inside a rounded square. Text on the screen reads 'Easier to Use'.

Custom nodes are especially helpful when you want to give clients a simplified interface, preventing accidental changes to critical settings. It’s like giving them a remote control with only the essential buttons.

Important: Custom nodes are only supported on self-hosted n8n instances due to security considerations. So, you’ll need to have your own n8n server running. If you don’t have one yet, check out the n8n installation guide.

Declarative vs. Programmatic Nodes

n8n offers two styles for custom nodes: declarative and programmatic. We’ll focus on declarative nodes, which are suitable for most use cases. Declarative nodes are like following a recipe – you define what you want, and n8n takes care of the how. Programmatic nodes, on the other hand, give you more control but require more coding.

Programmatic nodes are better for:

The image shows a man in the lower right corner of the frame, with a document visible in the background. The document lists the differences between declarative and programmatic node types. The text includes bullet points such as 'Introducing bugs,' 'Is more future-proof,' and 'Supports integration with REST APIs.' The programmatic style is described as more verbose and necessary for trigger nodes, non-REST-based APIs like GraphQL, nodes needing to transform incoming data, and full versioning.

Step-by-Step Guide: How to Create and Testing n8n Custom Node

This tutorial will guide you through creating a custom node that fetches weather data from the OpenWeatherMap API. It’s a classic example, and you’ll learn a ton! Let’s get started!

Step 1: Download the Starter Template

Start by downloading the declarative style template from GitHub:

github.com/n8n-io/n8n-nodes-starter

This template is your launchpad! It provides the basic structure and files you need to get started. Think of it as the chassis of your custom node.

The image shows a screen capture of a GitHub repository page. The repository is named 'n8n-nodes-starter' and is marked as a public template. The URL 'github.com/n8n-io/n8n-nodes-starter' is prominently displayed. The interface includes typical GitHub elements such as branch selection, code viewing options, and file listings. The presenter is visible in the lower portion of the frame.

Clone the repository or download the ZIP file. Rename the folder to something descriptive, like n8n-nodes-cityweather. This will help you keep things organized. I recommend using a name that reflects the purpose of your node.

Step 2: Organize the File Structure

Open the project in Visual Studio Code. You’ll see nodes and credentials folders. Delete the example files in these folders. We’re starting fresh!

The image shows a file explorer interface, likely from VS Code, displaying the file structure of an n8n custom node project. The directory 'NODES-CITYWEATHER' is visible, with subdirectories including '.vscode', 'credentials', and 'nodes'. Several files are listed, such as '.eslintrc.js', '.eslintignore', 'index.js', 'package.json', and 'README.md'. The presenter is visible in the lower portion of the frame.

Create the following files:

These files are the blueprints for your node. The .ts files contain the code, the .json file contains the metadata, and the .svg file is the icon that will appear in the n8n interface.

Step 3: Populate cityweather.node.ts

Add the necessary import statements and the main class definition to cityweather.node.ts. Remember to replace nasapics with cityweather. This is where the magic happens! You’ll be defining the logic of your node.

Next, add the node details, including the base URL for the Open Weather Map API. This tells n8n where to fetch the weather data from.

Finally, define the resources that will be displayed in the n8n user interface. This includes required fields (e.g., city name) and optional fields (e.g., format, language). These are the settings that users will see when they use your node.

The image shows a screen capture of code in an IDE, likely VS Code, with a man's face visible in the lower right corner. The code appears to be related to creating a custom node for n8n, with file names like 'CityWeather.node.ts' and 'CityWeather.node.json' visible in the file explorer on the left. The code itself includes JavaScript syntax with properties like 'displayName', 'name', 'type', 'default', 'placeholder', 'required', and 'description'. The man is looking towards the code and appears to be explaining it.

Step 4: Set Up Authentication in cityweatherapi.credentials.ts

Edit the cityweatherapi.credentials.ts file to configure authentication. Copy the code from the tutorial and modify the name and display name to City Weather API. Make sure the API key title matches the Open Weather Map API documentation. The API key is called app ID. This is how your node will securely access the OpenWeatherMap API. Without it, you won’t be able to get any weather data!

The image shows a screen capture of code in an IDE, likely VS Code, with a man's face visible in the lower right corner. The code appears to be related to creating a custom node for n8n, with file names like 'CityWeatherApi.credentials.ts' visible in the file explorer on the left. The code itself includes TypeScript syntax with properties like 'displayName' and 'name'. The man is looking towards the code and appears to be explaining it.

Step 5: Add Node Metadata in cityweather.node.json

Populate the cityweather.node.json file with node metadata, such as the name and version. Remember to change NASA pics to city weather. This file tells n8n about your node – its name, version, and other important details.

The image shows a screen capture of code in an IDE, likely VS Code, with a man's face visible in the lower right corner. The code appears to be related to creating a custom node for n8n, with file names like 'CityWeather.node.json' visible in the file explorer on the left. The code editor is currently empty.

Step 6: Update NPM Package Details (Important!)

This step is crucial. The starter template may use pnpm instead of npm. To ensure compatibility, revert to a previous version of the package.json file (before July 2024) to use NPM.

  1. Go to the package.json file in the n8n node starter template on GitHub.
  2. View the history and select the version from May 2nd, 2024.
  3. Copy the code from that version and paste it into your local package.json file.
  4. Change the names to reflect your project (e.g., city weather).
  5. Delete the pnpm-lock.yaml file.

Why are we doing this? Because npm and pnpm are different package managers, and we want to make sure everything works smoothly. This is a bit of a technical detail, but it’s important to get right!

The image shows a screen capture of a GitHub repository page for 'n8n-nodes-starter'. The 'Commits' section is displayed, showing a history of commits to the 'package.json' file on the 'master' branch. The most recent commit is 'feat: replace npm with pnpm (#41)' from July 4, 2024. Other commits include 'make n8n-workflow a peerDependency' from May 2, 2024, and 'stop using n8n-core in nodes' also from May 2, 2024. On the right side of the image, a person is partially visible.

Testing Your Custom Node

Now that you’ve created your custom node, it’s time to test it! This is where you see if all your hard work has paid off.

  1. Ensure n8n is installed (npm install n8n -g). This installs n8n globally on your system, so you can run it from anywhere. After running this command, you should see a message confirming that n8n has been installed.

  2. In your n8n-nodes-cityweather directory, run npm install, npm run build, and npm link. Let’s break this down:

    • npm install: Installs all the dependencies required by your node.
    • npm run build: Compiles your TypeScript code into JavaScript.
    • npm link: Creates a symbolic link to your node, so n8n can find it.

    You should see messages indicating that the dependencies have been installed, the code has been compiled, and the node has been linked.

  3. Navigate to your local n8n instance directory (e.g., cd ~/.n8n on macOS/Linux). This is where n8n stores its data and configuration files.

  4. If a custom directory doesn’t exist, create one (mkdir custom) and initialize it with npm init. The custom directory is where n8n looks for custom nodes. The npm init command creates a package.json file in the directory, which is required for npm link to work.

  5. Link your node: npm link n8n-nodes-cityweather. This creates a symbolic link from your node’s directory to the custom directory, so n8n can find it. You should see a message confirming that the node has been linked.

  6. Run n8n. You can do this by running the n8n command in your terminal. This will start the n8n server. You should see a message indicating that n8n is running and listening on a specific port (usually 5678).

npm install n8n -g
npm install
npm run build
npm link
mkdir custom
cd custom
npm init -y
npm link n8n-nodes-cityweather

The image shows a terminal window on what appears to be a macOS system, displaying the contents of the 'n8n-nodes-cityweather' directory. The files listed include 'CODE_OF_CONDUCT.md', 'credentials', 'LICENSE.md', 'README.md', 'README_TEMPLATE.md', 'nodes', 'package.json', 'gulpfile.js', 'tsconfig.json', and 'tslint.json'. The current directory in the terminal is 'benyoung@Bens-MacBook-Pro n8n-nodes-cityweather'. To the left is a portion of a documentation page with sections like 'Integrations', 'Credentials', 'Community nodes', and 'Creating nodes'. A person is partially visible on the right.

The image displays a terminal window on a macOS system, showing the output of npm commands within the 'n8n-nodes-cityweather' directory. Several 'npm warn deprecated' messages are visible, related to various packages. The output also indicates that 581 packages were added and 582 packages were audited in 25 seconds. It also shows '13 vulnerabilities (7 moderate, 6 high)'. The terminal prompt shows the user 'benyoung@Bens-MacBook-Pro' in the 'n8n-nodes-cityweather' directory. To the left, a portion of a documentation page is visible, with sections like 'Integrations', 'Community nodes', and 'Creating nodes'. A person is partially visible on the right.

The image shows a person looking at a terminal window on a computer screen. The terminal displays a directory listing with files such as 'binaryData', 'config', 'database.sqlite', 'n8nEventLog-1.log', 'n8nEventLog-2.log', 'n8nEventLog-3.log', and 'ssh'. The person is positioned to the right of the screen, partially visible. The background includes elements of the n8n Docs interface, such as 'Integrations', 'Credentials', 'Custom API actions for existing nodes', 'Handle rate limits', 'Community nodes', 'Installation and management', 'Risks', 'Blocklist', 'Using community nodes', 'Troubleshooting', 'Building community nodes', 'Creating nodes', 'Overview', 'Plan your node', 'Build your node', 'Set up your development environment', 'Tutorial: Build a declarative-style node', and a search bar.

The image shows a person looking at a terminal window on a computer screen. The terminal displays commands and file information. The person is positioned to the right of the screen, partially visible. The background includes elements of the n8n Docs interface, such as 'Integrations', 'Credentials', 'Custom API actions for existing nodes', 'Handle rate limits', 'Community nodes', 'Installation and management', 'Risks', 'Blocklist', 'Using community nodes', 'Troubleshooting', 'Building community nodes', 'Creating nodes', 'Overview', 'Plan your node', 'Build your node', 'Set up your development environment', 'Tutorial: Build a declarative-style node', and a search bar.

Open your n8n instance in a browser and refresh. Add a workflow and search for your new node (e.g., City Weather). If you’ve done everything correctly, you should see your new node in the list!

The image shows a person looking at the n8n interface. The interface displays a workflow editor with a node labeled 'When clicking Test workflow'. To the right, a search bar labeled 'What happens next?' is visible with the text 'weather' entered. Below the search bar, two options are displayed: 'OpenWeatherMap' and 'City Weather'.

The image shows a screen recording of code being edited in what appears to be Visual Studio Code. The code relates to a 'CityWeather' node, with the file name 'CityWeather.node.ts' visible in the tab. The code includes properties such as 'displayName', 'name', 'type', 'default', 'placeholder', 'required', and 'description'. A man is visible in the lower right corner, seemingly explaining the code.

Making Changes to Your Custom Node

To implement changes, rerun npm run build in your n8n-nodes-cityweather directory, followed by npm link and the package name in your n8n instance. This is how you update your node after making changes to the code.

For instance, to make the format field mandatory, move its code block from the optional fields section to the required fields section in cityweather.node.ts. Then, rebuild and relink the node. This will ensure that users always have to specify the format of the weather data.

The image shows a screen capture of a custom node configuration interface, labeled 'City Weather default'. The interface includes fields for 'Credential to connect with', 'City', and 'Format'. The 'Format' field is highlighted with a red arrow, and the text 'Format is now required' is superimposed on the image. A man is visible in the lower right corner, seemingly explaining the interface.

Best Practices and Additional Tips

The image displays a software interface, possibly a settings or configuration panel, with a list of various modules or integrations. The list includes items such as 'Formstack', 'Freshdesk', 'FreshworksCrm', 'Ftp', 'Function', 'FunctionItem', 'GetResponse', 'Ghost', 'Git', 'Github', 'Gitlab', 'GoToWebinar', 'Gong', 'Google', 'Gotify', 'Grafana', 'GraphQL', 'Grist', and 'Gumroad'. Each item has a corresponding build or fix description. A man is visible in the lower right corner, seemingly explaining the interface.

Resources and Cost-Benefit Analysis

Resources List

ResourceDescriptionCost
n8nWorkflow automation platformFree/Paid
Node.jsJavaScript runtime environmentFree
npm/pnpmPackage manager for Node.jsFree
Visual Studio CodeCode editorFree
Open Weather Map API KeyRequired for accessing weather data.Free/Paid
n8n-nodes-starter repoStarter template for creating custom nodesFree

Cost-Benefit Analysis

FeatureDIY Custom NodeCommercial Node Development Service
Initial CostTime investment (learning, development, debugging)Monetary investment (hiring a developer)
Time to ImplementLonger (days/weeks depending on complexity)Shorter (days/weeks depending on complexity)
CustomizationFully customizable to specific needsLimited to service offerings
MaintenanceRequires ongoing maintenance and updates by youMaintenance typically included in service agreement
Long-Term CostLower monetary cost, higher time investmentHigher monetary cost, lower time investment
Skill DevelopmentEnhances coding and problem-solving skillsNo skill development
ControlFull control over the node’s functionality and securityLess control; reliance on the service provider

Safety and Best Practices

FAQ

Q: Can I use a custom node created by someone else? A: Yes, if it’s a community node or installed via npm run build.

Q: Are custom nodes supported on n8n Cloud? A: No, only on self-hosted instances. This is due to security considerations.

Q: What if I encounter errors? A: Double-check your code, API keys, and consult the n8n documentation or community forums. The n8n community is very helpful!

Key Takeaways

Conclusion

This guide has shown you how to create and testing n8n custom node, tailoring the platform to your needs. Explore n8n’s GitHub repository and stay updated with best practices to ensure your nodes are secure and efficient.

Share your custom node creations and insights below!

Frequently Asked Questions (FAQ)

Q: What are the main differences between declarative and programmatic nodes?

A: Declarative nodes are easier to create and maintain, suitable for most use cases. Programmatic nodes offer more flexibility and control but require more coding.

Q: How can I debug my custom node if it’s not working as expected?

A: Use console.log statements in your code to track the flow of data and identify any errors. You can also use a debugger in Visual Studio Code to step through your code.

Q: Can I use environment variables in my custom node?

A: Yes, you can access environment variables using process.env in your code. This is useful for storing sensitive information such as API keys.

Q: How do I share my custom node with the n8n community?

A: You can publish your node to the npm registry and then submit it to the n8n community nodes repository on GitHub.

Q: What kind of testing should I perform on my custom node?

A: You should perform unit tests to verify that each function in your code works correctly. You should also perform integration tests to ensure that your node works correctly with n8n.

Q: Is it possible to create a custom node that triggers automatically based on an event?

A: Yes, you can create a trigger node using the programmatic node style. Trigger nodes can listen for events from external services and automatically start a workflow.


Related Tutorials

Connect n8n to Any LLM in 2 Mins with OpenRouter: A Comprehensive Guide

Unlock seamless access to almost 100 different Large Language Models (LLMs) within your n8n workflows using a single API key from OpenRouter. This guide details the setup process and highlights the be

HANDBOOK: Nodes And Integrations • DIFFICULTY: BEGINNER

Mastering N8N and Google Sheets Integration: A Step-by-Step Guide

Unlock powerful automation by seamlessly connecting N8N with Google Sheets. This guide provides a detailed, step-by-step tutorial to set up your integration in under 5 minutes, boosting your workflow

HANDBOOK: Nodes And Integrations • DIFFICULTY: BEGINNER

Connect N8N to Telegram: A 2-Minute Step-by-Step Guide for Automation

Learn how to seamlessly integrate n8n with Telegram in under 2 minutes to automate your workflows. This guide covers everything from setting up your Telegram bot to securing your connection.

HANDBOOK: Nodes And Integrations • DIFFICULTY: BEGINNER

Mastering WhatsApp Automation with n8n: A Step-by-Step Guide for Business

Unlock the power of automated WhatsApp communication for your business. This comprehensive guide details how to integrate WhatsApp Business with n8n, enabling seamless message triggers and automated r

HANDBOOK: Nodes And Integrations • DIFFICULTY: BEGINNER

Mastering n8n: Essential Concepts for AI Agents, JSON, and Workflow Logic

Unlock the full potential of n8n by mastering its foundational concepts, including JSON data handling, dynamic expressions, and advanced workflow logic for building powerful AI-driven automations. Lea

HANDBOOK: Core Concepts • DIFFICULTY: BEGINNER

Unlocking Efficiency: A Beginner's Guide to n8n Workflow Automation

Discover how n8n, a powerful open-source automation tool, can save you countless hours by automating repetitive tasks. Learn its unique advantages over traditional platforms and how to get started.

HANDBOOK: Getting Started • DIFFICULTY: BEGINNER
Share this post on: