Chapter 3

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:

  1. Schedule Trigger โ€” fires automatically at 8:00 AM every day
  2. HTTP Request โ€” calls the Hacker News API to get Top 10 stories
  3. Code โ€” filters out stories with score below 100
  4. OpenAI โ€” generates a summary for each story
  5. 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:

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:

This returns an array of 500 story IDs. A second HTTP Request node fetches each story's details:

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:

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):

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

Rate this chapter
4.8  / 5  (77 ratings)

๐Ÿ’ฌ Comments