Code Node Mastery: Data Processing, API Calls, and File Generation - n8n Workflow

Master advanced JavaScript scripting in your n8n workflow using the n8n code node. Learn data aggregation, external API calls, and binary file creation with this interactive n8n template.

Workflow Preview

Ready to automate?

Download this n8n workflow template and start using it instantly.

Who is this best for?

Developers and Technical Users looking to leverage custom JavaScript within n8n.
Users who need to perform complex data transformations or aggregations not possible with standard n8n nodes.
Automation specialists seeking to understand different execution modes of the n8n Code n8n node (Per Item vs. Once for All).
Anyone utilizing n8n templates to enhance their custom scripting skills.

Overview

The n8n Code n8n node is arguably the most powerful tool in the platform, allowing users to inject custom JavaScript logic directly into their n8n workflow. This specific n8n workflow acts as a hands-on guide, demonstrating four critical use cases for the Code n8n node:


  1. Item Enrichment: Calculating new fields (like age and full name) for individual items.

  2. External Integration: Making asynchronous HTTP requests to external APIs using built-in helpers.

  3. Data Aggregation: Summarizing all incoming data (e.g., calculating the average age).

  4. File Generation: Creating binary data (like a CSV report) from aggregated JSON input.

By following this n8n template, users can quickly transition from basic flow building to scripting powerful, dynamic automations.

How it Works

This comprehensive n8n workflow starts with a manual n8n trigger (Start Tutorial) and proceeds through several logical stages, each highlighting a different facet of the Code n8n node:


  1. Data Initialization: The 1. Sample Data n8n node initializes a list of user objects. The 2. Split Out Users n8n node ensures that each user becomes an individual item, preparing the data for 'Per Item' processing.

  2. Per-Item Transformation (Local): The 3. Process Each User Code n8n node runs for each user item. It calculates the user's fullName and precise age using JavaScript date functions.

  3. Per-Item Transformation (External API): The 4. Fetch External Data (Advanced) Code n8n node continues running per item. It demonstrates how to use this.helpers.httpRequest to call an external API (genderize.io) based on the user's first name, enriching the data with gender probability.

  4. Aggregation: The 5. Calculate Average Age Code n8n node switches to 'Run Once for All Items' mode. It uses the $items() function to access all results from the previous steps and calculates a single summary output containing the total user count and the average age. This is a crucial function of the n8n node when dealing with large datasets.

  5. File Generation (Expert): The final 6. Create a Binary File (Expert) Code n8n node aggregates the data again, generates a CSV string, and uses this.helpers.prepareBinaryData to convert the string into a ready-to-use binary report file within the n8n workflow.

Installation Guide

To install and test this n8n workflow, follow these steps:


  1. Copy the JSON: Copy the provided n8n workflow JSON code.

  2. Import to n8n: In your n8n instance, click 'New' and select 'Import from JSON'. Paste the code and click 'Import'.

  3. Review the Logic: Examine the sticky notes attached to the Code n8n nodes for explanations of the JavaScript code and execution modes.

  4. Execute: Since the starting n8n trigger is a Manual Trigger, click 'Run Workflow' and then click 'Start Tutorial' to execute the n8n workflow step-by-step.

  5. Inspect Output: Check the output of each Code n8n node to observe how the data structure changes, from individual items to a single aggregated report containing both JSON metadata and a binary CSV file. No external credentials are required for this particular n8n template.

Node Details

Start Tutorial (Manual Trigger): The starting n8n trigger point for manually running this n8n workflow.
1. Sample Data (Set n8n node): Initializes the workflow with a sample array of user objects, serving as the input for the tutorial.
2. Split Out Users (Split Out n8n node): Essential n8n node that converts the single item containing the user array into multiple individual items, allowing the subsequent Code n8n nodes to process them one by one.
3. Process Each User (Code n8n node): Function is to calculate derived data (fullName, age) for the current item. Key Configuration: Mode is set to Run Once for Each Item.
4. Fetch External Data (Advanced) (Code n8n node): Function is to demonstrate asynchronous operations using await this.helpers.httpRequest to fetch external data (gender guess) for the current user. Key Configuration: Mode is set to Run Once for Each Item.
5. Calculate Average Age (Code n8n node): Function is to aggregate data from all previous items. It uses $items() and JavaScript reduce to calculate the average age. Key Configuration: Mode is set to Run Once for All Items.


  • 6. Create a Binary File (Expert) (Code n8n node): Function is to generate a CSV report from the aggregated data and convert it into a binary file using this.helpers.prepareBinaryData. Key Configuration: Mode is set to Run Once for All Items.

Related n8n Workflows

Free

Nodes: 5 Nodes
Updated: December 26 2025
View all
Created by

Innovative builder with a passion for crafting automation solutions that solve real-world challenges. From streamlining workflows to driving efficiency, my work empowers teams and individuals to achieve more with less effort. Experienced in developing scalable tools and strategies that deliver results with n8n, supabase and cline.

Featured*