For the complete documentation index, see llms.txt. This page is also available as Markdown.

Tutorial: Build a declarative-style node

This tutorial walks through building a declarative-style node. Before you begin, make sure this is the node style you need. Refer to Choose your node building approach for more information.

Prerequisites

You need the following installed on your development machine:

  • Node.js and npm. Minimum version Node 18.17.0. You can find instructions on how to install both using nvm (Node Version Manager) for Linux, Mac, and WSL here. For Windows users, refer to Microsoft's guide to Install NodeJS on Windows.

You need some understanding of:

  • JavaScript/TypeScript

  • REST APIs

  • git

Build your node

In this section, you'll clone n8n's node starter repository, and build a node that integrates the NASA API. You'll create a node that uses two of NASA's services: APOD (Astronomy Picture of the Day) and Mars Rover Photos. To keep the code examples short, the node won't implement every available option for the Mars Rover Photos endpoint.

Existing node

n8n has a built-in NASA node. To avoid clashing with the existing node, you'll give your version a different name.

Step 1: Set up the project

n8n provides a starter repository for node development. Using the starter ensures you have all necessary dependencies. It also provides a linter.

Clone the repository and navigate into the directory:

  1. Generate a new repository from the template repository.

  2. Clone your new repository: shell git clone https://github.com/<your-organization>/<your-repo-name>.git n8n-nodes-nasa-pics cd n8n-nodes-nasa-pics

The starter contains example nodes and credentials. Delete the following directories and files:

  • nodes/Example

  • nodes/GithubIssues

  • credentials/GithubIssuesApi.credentials.ts

  • credentials/GithubIssuesOAuth2Api.credentials.ts

Now create the following directories and files:

nodes/NasaPics nodes/NasaPics/NasaPics.node.json nodes/NasaPics/NasaPics.node.ts credentials/NasaPicsApi.credentials.ts

These are the key files required for any node. Refer to Node file structure for more information on required files and recommended organization.

Now install the project dependencies:

Step 2: Add an icon

Save the NASA SVG logo from here as nasapics.svg in nodes/NasaPics/.

n8n recommends using an SVG for your node icon, but you can also use PNG. If using PNG, the icon resolution should be 60x60px. Node icons should have a square or near-square aspect ratio.

Don't reference Font Awesome

If you want to use a Font Awesome icon in your node, download and embed the image.

Step 3: Create the node

Every node must have a base file. Refer to Node base file for detailed information about base file parameters.

In this example, the file is NasaPics.node.ts. To keep this tutorial short, you'll place all the node functionality in this one file. When building more complex nodes, you should consider splitting out your functionality into modules. Refer to Node file structure for more information.

Step 3.1: Imports

Start by adding the import statements:

Step 3.2: Create the main class

The node must export an interface that implements INodeType. This interface must include a description interface, which in turn contains the properties array.

Class names and file names

Make sure the class name and the file name match. For example, given a class NasaPics, the filename must be NasaPics.node.ts.

Step 3.3: Add node details

All nodes need some basic parameters, such as their display name, icon, and the basic information for making a request using the node. Add the following to the description:

n8n uses some of the properties set in description to render the node in the Editor UI. These properties are displayName, icon, description, and subtitle.

Step 3.4: Add resources

The resource object defines the API resource that the node uses. In this tutorial, you're creating a node to access two of NASA's API endpoints: planetary/apod and mars-photos. This means you need to define two resource options in NasaPics.node.ts. Update the properties array with the resource object:

type controls which UI element n8n displays for the resource, and tells n8n what type of data to expect from the user. options results in n8n adding a dropdown that allows users to choose one option. Refer to Node UI elements for more information.

Step 3.5: Add operations

The operations object defines the available operations on a resource.

In a declarative-style node, the operations object includes routing (within the options array). This sets up the details of the API call.

Add the following to the properties array, after the resource object:

This code creates two operations: one to get today's APOD image, and another to send a get request for photos from one of the Mars Rovers. The object named roverName requires the user to choose which Rover they want photos from. The routing object in the Mars Rover operation references this to create the URL for the API call.

Step 3.6: Optional fields

Most APIs, including the NASA API that you're using in this example, have optional fields you can use to refine your query.

To avoid overwhelming users, n8n displays these under Additional Fields in the UI.

For this tutorial, you'll add one additional field, to allow users to pick a date to use with the APOD endpoint. Add the following to the properties array:

Step 4: Set up authentication and a credential test

The NASA API requires users to authenticate with an API key. You can also send a request to check that the API key works.

Add the following to nasaPicsApi.credentials.ts:

For more information about credentials files and options, refer to Credentials file.

Step 5: Add node metadata

Metadata about your node goes in the JSON file at the root of your node. n8n refers to this as the codex file. In this example, the file is NasaPics.node.json.

Add the following code to the JSON file:

For more information on these parameters, refer to Node codex files.

Step 6: Update the npm package details

Your npm package details are in the package.json at the root of the project. It's essential to include the n8n object with links to the credentials and base node file. Update this file to include the following information:

You need to update the package.json to include your own information, such as your name and repository URL. For more information on npm package.json files, refer to npm's package.json documentation.

Test your node

You can test your node as you build it by running it in a local n8n instance.

  1. Install n8n using npm:

  1. When you are ready to test your node, publish it locally:

  1. Install the node into your local n8n instance:

Check your directory

  1. Start n8n:

  1. Open n8n in your browser. You should see your nodes when you search for them in the nodes panel.

Node names

Troubleshooting

If there's no custom directory in your .n8n local installation, you have to create the custom directory manually and run npm init.

The .n8n directory location depends on your operating system:

  • For Windows: C:\Users\<username>\.n8n\custom

  • For Linux: /home/<username>/.n8n/custom

  • For MacOS: /Users/<username>/.n8n/custom

Note: The .n8n folder is a hidden folder so it may not appear in your file browser.

Next steps

Last updated

Was this helpful?