Your First Automation Workflow
Ch03 Your First Automation Workflow
Enough theory โ time to build. This chapter walks through building a real, working workflow from scratch: automatically fetch Hacker News top stories every morning, generate summaries using OpenAI, and send them to your inbox. The process covers the full workflow-building cycle and builds your muscle memory for n8n development.
Workflow Design
Five nodes connected in sequence:
- Schedule Trigger โ fires automatically at 8:00 AM every day
- HTTP Request โ calls the Hacker News API to get Top 10 stories
- Code โ filters out stories with score below 100
- OpenAI โ generates a summary for each story
- Gmail โ sends the compiled summary to your inbox
Step 1: Add the Schedule Trigger Node
Open n8n, click New Workflow, click the + button on the blank canvas, search "Schedule", and select Schedule Trigger.
Configuration:
- Trigger Interval:
Days - Days Between Triggers:
1 - Trigger at Hour:
8 - Trigger at Minute:
0
Development tip: Don't wait for the schedule during development โ click the Execute Node button on the node to manually trigger it immediately and inspect its output.
Step 2: HTTP Request for Hacker News Data
Pull a connection from the Schedule Trigger and add an HTTP Request node.
Configuration:
- Method:
GET - URL:
https://hacker-news.firebaseio.com/v0/topstories.json
This returns an array of 500 story IDs. A second HTTP Request node fetches each story's details:
- Method:
GET - URL:
https://hacker-news.firebaseio.com/v0/item/{{ $json }}.json
Since the upstream node outputs 500 IDs, this node would run 500 times. To limit it to 10, set Batch Size to 10 in the node Settings tab.
Step 3: Code Node to Filter Stories
Add a Code node to filter low-quality stories:
// Keep only stories with score >= 100 and a URL
const filtered = items.filter(item => {
const score = item.json.score || 0;
return score >= 100 && item.json.url;
});
// Sort by score descending
filtered.sort((a, b) => b.json.score - a.json.score);
// Take top 10
return filtered.slice(0, 10);
Step 4: OpenAI Node to Generate Summaries
Add an OpenAI node (requires an OpenAI API Key in Credentials first).
Configuration:
- Resource:
Chat - Operation:
Message a Model - Model:
gpt-4o-mini(low cost, sufficient for summarization) - Messages:
System: You are a tech content assistant. Summarize Hacker News articles concisely.
User: Summarize this article in 2-3 sentences:
Title: {{ $json.title }}
URL: {{ $json.url }}
Score: {{ $json.score }}
Output only the summary, no prefix.
Step 5: Aggregate and Send with Gmail
After all summaries are generated, add an Aggregate node to merge all items into one, then format the email with a Code node:
// Generate HTML email body
const articles = items.map((item, index) => {
return `
<div style="margin-bottom: 20px; padding: 15px; border-left: 3px solid #ff6600;">
<h3><a href="${item.json.url}">${index + 1}. ${item.json.title}</a></h3>
<p>Score: ${item.json.score} |
<a href="https://news.ycombinator.com/item?id=${item.json.id}">HN Comments</a>
</p>
<p>${item.json.summary}</p>
</div>
`;
}).join('');
const emailBody = `
<h2>Hacker News Top 10 โ ${new Date().toLocaleDateString('en-US')}</h2>
${articles}
<p style="color: #999; font-size: 12px;">Generated automatically by n8n</p>
`;
return [{ json: { body: emailBody } }];
Add a Gmail node (requires Gmail OAuth2 credentials):
- To: your email address
- Subject:
HN Daily Digest - {{ $now.format("YYYY-MM-DD") }} - Email Type:
HTML - HTML:
{{ $json.body }}
Testing the Complete Workflow
Click Execute Workflow to manually trigger a full run and observe each node's execution. If a node fails, click it to view the detailed error message.
Once tests pass, flip the Active toggle to activate the workflow โ n8n will automatically run it every morning at 8 AM.
Common issues: If the OpenAI node times out, reduce Batch Size from 10 to 5. If Gmail reports authorization errors, reconfigure OAuth2 credentials in Google Cloud Console.
Optimization Tips
- Cost: gpt-4o-mini costs ~$0.00015/1k tokens โ summarizing 10 stories costs under $0.01
- Error handling: Enable Continue on Fail on the OpenAI node to prevent one failed story from stopping the entire run
- Content quality: Add filters in the Code node to skip "Show HN" and "Ask HN" posts if you only want news articles