D: Are you ready to unlock the full potential of n8n’s Code Node? π€© This powerful feature lets you inject custom JavaScript/Python logic into your workflows, opening doors to endless automation possibilities. Letβs dive into 10 practical examples that will transform how you automate tasks!
π₯ 1. Dynamic Data Transformation
Use Case: Convert raw API data into a clean format.
// Input: [{ "name": "John", "age": 30 }]
return items.map(item => ({
userName: item.json.name.toUpperCase(),
userAge: item.json.age + 5 // Add buffer for "data maturity"
}));
Output: [{ "userName": "JOHN", "userAge": 35 }]
π 2. Date Magic
Use Case: Auto-calculate deadlines from today.
const dueDate = new Date();
dueDate.setDate(dueDate.getDate() + 7); // +7 days
return [{ json: { deadline: dueDate.toISOString().split('T')[0] } }];
Output: { "deadline": "2023-12-25" }
π 3. Conditional Routing
Use Case: Route tasks based on priority (e.g., “High” β Slack, “Low” β Email).
const priority = item.json.priority;
return [{ json: { ...item.json, routeTo: priority === "High" ? "slack" : "email" } }];
π€ 4. AI-Powered Text Processing
Use Case: Use OpenAI to summarize text.
const { Configuration, OpenAIApi } = require('openai');
const config = new Configuration({ apiKey: $secrets.OPENAI_KEY });
const openai = new OpenAIApi(config);
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: `Summarize: "${item.json.text}"`,
});
return [{ json: { summary: response.data.choices[0].text } }];
π 5. Aggregating Data from Multiple APIs
Use Case: Merge weather + calendar data.
const weather = await $node["GetWeather"].json();
const events = await $node["GetCalendar"].json();
return [{
json: {
today: `π§οΈ ${weather.temp}°C | π
${events[0].title}`
}
}];
π 6. Encryption/Decryption
Use Case: Secure sensitive data before DB storage.
const crypto = require('crypto');
const cipher = crypto.createCipheriv('aes-256-cbc', $secrets.CRYPTO_KEY, $secrets.IV);
let encrypted = cipher.update(item.json.password, 'utf8', 'hex');
encrypted += cipher.final('hex');
return [{ json: { encryptedPassword: encrypted } }];
π 7. E-Commerce Price Alerts
Use Case: Notify if product price drops below $X.
if (item.json.price headlines.push($(el).text()));
return [{ json: { headlines } }];
π€― 10. Custom Error Handling
Use Case: Retry failed API calls with exponential backoff.
if (item.json.error) {
const retries = item.json.retries || 0;
if (retries < 3) {
const delay = Math.pow(2, retries) * 1000;
return [{ json: { ...item.json, retries: retries + 1 }, pairedItem: { ...item.pairedItem, wait: delay } }];
}
}
return items; // Proceed if no error
π‘ Pro Tips
- Debugging: Use
console.log()
β View output in “Execution” tab. - Async/Await: Perfect for API calls (e.g.,
await $node["PreviousNode"].json()
). - Libraries: Pre-installed (
axios
,lodash
) or add vianpm
.
π― Why This Matters
The Code Node turns n8n from a “no-code” tool into a “low-code powerhouse” β‘. Whether youβre cleaning data, calling APIs, or building complex logic, these examples showcase its flexibility.
Try one today! Which example excites you the most? Drop a comment below. π
#n8n #Automation #WorkflowMagic #CodeNode