금. 8μ›” 15th, 2025

D: n8n is a powerful workflow automation tool that lets you connect various apps and services without writing code. But when you need to implement complex logic, the Code Node becomes your secret weapon! πŸ’»βœ¨

In this guide, we’ll explore 10 real-world examples of how to use the Code Node in n8n to handle advanced data processing, custom transformations, and dynamic decision-making.


πŸ”Ή What is the Code Node?

The Code Node in n8n allows you to write JavaScript (Node.js) or Python code directly inside your workflow. This means you can:
βœ… Process & transform data dynamically
βœ… Implement custom calculations
βœ… Call external APIs with complex logic
βœ… Manipulate JSON, arrays, and objects
βœ… Add conditional branching beyond what regular nodes offer


πŸ”₯ 10 Powerful Code Node Examples

1️⃣ Format & Clean Raw Data

Need to standardize messy data? Use the Code Node to trim whitespace, convert cases, or remove duplicates.

// Clean & format names
const inputData = $input.all();
const output = inputData.map(item => ({
  cleanName: item.json.name.trim().toUpperCase(),
  email: item.json.email.toLowerCase()
}));
return output;

2️⃣ Merge Data from Multiple Sources

Combine data from different APIs or databases into a single output.

const dataA = $input.all()[0].json; // From HTTP Request Node
const dataB = $input.all()[1].json; // From Google Sheets
return [{ ...dataA, ...dataB }];

3️⃣ Dynamic IF-ELSE Conditions

Go beyond basic IF conditionsβ€”implement multi-layered logic.

const score = $input.first().json.score;
let grade;
if (score >= 90) grade = "A";
else if (score >= 80) grade = "B";
else grade = "C";
return [{ grade }];

4️⃣ Generate Custom Reports in JSON/CSV

Process raw data into structured reports.

const salesData = $input.all();
const report = salesData.map(sale => ({
  month: sale.json.month,
  revenue: sale.json.amount * 0.9 // Apply 10% tax
}));
return report;

5️⃣ Call External APIs with Authentication

Handle OAuth, API keys, or custom headers.

const axios = require('axios');
const response = await axios.get('https://api.example.com/data', {
  headers: { 'Authorization': 'Bearer YOUR_TOKEN' }
});
return response.data;

6️⃣ Date & Time Calculations

Calculate deadlines, time differences, or format dates.

const now = new Date();
const dueDate = new Date(now.setDate(now.getDate() + 7)); // +7 days
return [{ due: dueDate.toISOString().split('T')[0] }];

7️⃣ Validate & Filter Data

Remove invalid entries before further processing.

const users = $input.all();
const validUsers = users.filter(user => 
  user.json.email.includes('@') && user.json.age > 18
);
return validUsers;

8️⃣ Generate Random Test Data

Create mock data for testing workflows.

const mockUsers = Array(5).fill().map((_, i) => ({
  id: i + 1,
  name: `User_${i}`,
  value: Math.floor(Math.random() * 100)
}));
return mockUsers;

9️⃣ Advanced Error Handling

Catch and manage errors gracefully.

try {
  const data = $input.first().json;
  if (!data.email) throw new Error("Email missing!");
  return [{ success: true }];
} catch (error) {
  return [{ error: error.message }];
}

πŸ”Ÿ Loop & Batch Processing

Process large datasets in chunks.

const bigData = $input.first().json.data;
const chunkSize = 50;
const batches = [];
for (let i = 0; i < bigData.length; i += chunkSize) {
  batches.push(bigData.slice(i, i + chunkSize));
}
return batches;

πŸš€ Pro Tips for Using Code Node

βœ” Use $input to access workflow data.
βœ” return must be an array of objects.
βœ” Install npm packages via n8n's “External Libraries” feature.
βœ” Debug with `console.log()β€”output appears in the “Execution” tab.


🎯 Conclusion

The Code Node unlocks endless possibilities in n8n workflows. Whether you're cleaning data, calling APIs, or implementing complex logic, it’s the ultimate tool for power users.

Try these examples and customize them for your needs! πŸš€ Which one will you implement first? Let us know in the comments! πŸ’¬πŸ‘‡

#n8n #Automation #Workflow #NoCode #LowCode #JavaScript #Python

λ‹΅κΈ€ 남기기

이메일 μ£Όμ†ŒλŠ” κ³΅κ°œλ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. ν•„μˆ˜ ν•„λ“œλŠ” *둜 ν‘œμ‹œλ©λ‹ˆλ‹€