Generating PDF…

Preparing…
← Back

Workflow Automation with n8n

Computer Programming 2 — Week 14

Quick Recap: Docker

In Week 12 we learned that Docker packages an app and everything it needs into a container.

  • A container runs the same way on any machine
  • No messy installs — just docker run
  • It keeps your own computer clean

This week we'll use Docker to run a tool called n8n in a single command.

# The Docker idea in one line:
docker run hello-world

# Today we'll do the same thing,
# but run a full automation app:
docker run ... n8n

Part 1 — What is n8n?

Connecting apps without writing a full program

What is n8n?

n8n (pronounced "n-eight-n") is a workflow automation tool.

  • You build automations by dragging boxes (called nodes) and connecting them
  • Each node does one job: fetch data, make a decision, send a message…
  • Data flows from one node to the next, left to right
  • It's open source and runs on your own machine

Think of it as "visual programming" — same logic as Python, but you click instead of type.

⏰ Trigger 🌐 Get Data 📨 Send Alert

A workflow is just nodes joined together.

Why Use n8n?

  • Fast (RAD): build a working tool in minutes using ready-made blocks
  • No boilerplate: no servers, no libraries to install for each task
  • 400+ integrations: Telegram, Gmail, Google Sheets, HTTP APIs, databases…
  • You can still code: a Code node lets you drop in JavaScript when needed

Real examples

  • Watch a sensor API and alert you when a value is too high
  • Save every new form submission to a spreadsheet
  • Send a daily weather message to Telegram
  • Back up data on a schedule

This is exactly what you'll build in Lab Task 4 — a temperature monitor that alerts Telegram.

Four Words You Need to Know

🧩 Node

A single building block. It does one task (e.g. "make an HTTP request").

⏰ Trigger

The node that starts a workflow — a button click, a schedule, or an event.

🔗 Connection

The line joining nodes. It carries the data from one node to the next.

⚙️ Workflow

The whole picture — all your connected nodes working together.

Part 2 — Installing n8n with Docker

One command to get started

Before You Start

You only need Docker Desktop installed and running (from Week 12).

  • Open Docker Desktop and wait until it says "running"
  • Open your terminal (macOS) or PowerShell (Windows)
  • Check Docker works with the command on the right

If docker --version prints a version number, you're ready.

# Check Docker is installed
docker --version
# e.g. Docker version 27.0.3

# Check the Docker engine is running
docker ps
# Shows an (empty) list of containers

Run n8n in One Command

Just like any other image, we pull n8n then run it.

  • -p 5678:5678 opens the web editor on your machine
  • -v ~/.n8n:/home/node/.n8n saves your work so it survives a restart
  • --name n8n gives the container a friendly name

The first run downloads the image — give it a minute.

# 1. Download the official n8n image
docker pull docker.n8n.io/n8nio/n8n

# 2. Run it (Windows: use ~/.n8n too in PowerShell)
docker run -it --rm \
  --name n8n \
  -p 5678:5678 \
  -v ~/.n8n:/home/node/.n8n \
  docker.n8n.io/n8nio/n8n

What Does Each Part Mean?

Flag What it does
docker runStart a new container
-p 5678:5678Connect the container's port to your browser at localhost:5678
-v ~/.n8n:/home/node/.n8nSave workflows on your computer (a "volume") so they are not lost
--name n8nName the container so you can stop/start it easily
--rmClean up the container when you stop it (your saved volume stays)

Open the n8n Editor

Once the terminal shows Editor is now accessible, open your browser:

  • Go to http://localhost:5678
  • Create a local owner account (just an email + password — it stays on your machine)
  • You'll land on the blank canvas where you build workflows

To stop n8n later: press Ctrl + C in the terminal. Your workflows are saved.

Version: 1.x.x
Editor is now accessible via:
http://localhost:5678

Open that link in Chrome or Edge.

Part 3 — Building Workflows

Simple, practical examples

The Editor at a Glance

  • Canvas: the big empty area where you place nodes
  • + button (top-right): opens the node search to add a node
  • Click a node to open its settings panel
  • Execute Workflow button runs everything and shows the data

Golden rule: every workflow must start with a Trigger node.

A node panel looks like this:

🌐 HTTP Request
Method: GET
URL: https://...
▶ Execute step

Example 1: Your First Output

Goal: click a button and produce a small piece of data. The simplest possible workflow.

Two nodes:

  • Manual Trigger — starts the workflow when you click Execute
  • Edit Fields (Set) — creates some output data
▶ Manual Trigger ✏️ Edit Fields

Example 1: Step by Step

  1. On a new canvas, click + and add "Manual Trigger"
  2. Click + again, search "Edit Fields" (the Set node), and connect it after the trigger
  3. Open the Edit Fields node → Add Field:
    • Name: message   Value: Hello from n8n!
    • Name: course   Value: CP2
  4. Click Execute Workflow
// What you'll see in the output panel:
[
  {
    "message": "Hello from n8n!",
    "course": "CP2"
  }
]

Example 2: Fetch Data from an API

Goal: pull live data from the internet — the heart of the lab.

Two nodes:

  • Manual Trigger — start it yourself
  • HTTP Request — call a public API and get JSON back

We'll use a free joke API so you can see real data instantly.

▶ Manual Trigger 🌐 HTTP Request

Example 2: Step by Step

  1. Add a Manual Trigger
  2. Add an HTTP Request node after it
  3. In the HTTP Request node set:
    • Method: GET
    • URL: https://official-joke-api.appspot.com/random_joke
  4. Click Execute Workflow and read the JSON result

The API gives back fields like setup and punchline.

// Example response (your joke will differ):
{
  "type": "general",
  "setup": "Why did the developer go broke?",
  "punchline": "Because he used up all his cache.",
  "id": 42
}

Using Data: Expressions

To use data from an earlier node, n8n uses expressions written in {{ }}.

  • {{ $json.setup }} → reads the setup field of the incoming item
  • $json always means "the data coming into this node"
  • Switch any field to "Expression" mode to type these in

In the lab you'll write {{ $json.alert_message }} to put text into the Telegram message.

# Reading fields from the previous node
{{ $json.setup }}        → the question
{{ $json.punchline }}    → the answer
{{ $json.id }}           → the id number

# You can even combine them:
{{ $json.setup }} ... {{ $json.punchline }}

Example 3: Making a Decision

Goal: do something only when a condition is true — exactly like an if in Python.

The IF node splits the flow:

  • true output → runs when the condition matches
  • false output → runs otherwise

This is how the lab decides whether a temperature is "out of range".

🌐 Get Number ❓ IF > 50
true → 🔔 alert false → do nothing

Example 3: Step by Step

  1. Add a Manual Trigger
  2. Add an Edit Fields node and create a number field:
    • Name: temperature   Value: 120 (type: Number)
  3. Add an IF node and set the condition:
    • Value 1: {{ $json.temperature }}
    • Operation: is greater than
    • Value 2: 100
  4. Execute → the item leaves through the true branch
# The IF node is just this in plain English:

if temperature > 100:
    go out the TRUE branch   # raise an alert
else:
    go out the FALSE branch  # all good

Example 4: The Code Node

When a ready-made node isn't enough, the Code node lets you write a little JavaScript.

  • $input.all() gives you every incoming item
  • Return an array of { json: {...} } objects
  • Great for calculations and building alert messages

The lab uses a Code node to check both sensors and build the alert text.

// Code node: flag temperatures over 100°C
const items = $input.all();

return items.map(item => {
  const temp = item.json.temperature;
  return {
    json: {
      temperature: temp,
      status: temp > 100 ? 'ALERT' : 'NORMAL'
    }
  };
});

Putting It Together — Lab Task 4

Your lab combines everything from today into one monitoring workflow:

⏰ Schedule
every 5s
🌐 HTTP
sensor API
⚙️ Code
check range
❓ IF
out of range?
📨 Telegram
send alert

You now know every block in this chain. The lab just connects them and adds a Telegram bot. Full brief: Task 4 — Industrial Temperature Sensor Monitoring.

Handy Commands

Manage your n8n container from the terminal:

  • Stop n8n: press Ctrl + C in its terminal
  • List running containers: docker ps
  • Your saved workflows live in the ~/.n8n folder

Because of the volume, your workflows are still there next time you run the container.

# Start n8n again any time
docker run -it --rm --name n8n \
  -p 5678:5678 \
  -v ~/.n8n:/home/node/.n8n \
  docker.n8n.io/n8nio/n8n

# See what's running
docker ps

Recap

  • n8n is visual workflow automation — build by connecting nodes
  • We ran it with Docker in a single command and saved data with a volume
  • Every workflow starts with a Trigger (Manual or Schedule)
  • HTTP Request fetches data; expressions {{ $json.x }} reuse it
  • IF makes decisions; the Code node adds custom logic
  • You're ready for Lab Task 4 🎉

Quick Check

Which node would you use to start a workflow automatically every 5 seconds?