Automation, from a shell script to a workflow · Lesson 8 of 8 · about 40 minutes

When it's a workflow: n8n, and where each job belongs

Some jobs aren't a script on one server. They pass work between a web form, an outside service and email, and the people who look after them aren't all engineers. This lesson builds one in n8n, a workflow tool you run yourself: a book request form that checks the request, looks the book up, emails the staff and answers the customer. Then it breaks the mail server, watches what the customer sees, and finishes the track by sorting every job in it into the right tool.

By the end you'll know

  • What a workflow tool is, and when it's a better home for a job than a script
  • How to run n8n in Docker, and load workflows and credentials from files
  • How a webhook starts a workflow, and how the workflow answers
  • How to send each kind of failure down its own path, with retries and an error workflow
  • Why an alert mustn't depend on the thing that broke
  • Which tool each job in this track belongs in, and why

Before you start

  • You need: Docker, as in Lesson 7, and the practice folder: git clone https://github.com/Kwamib/automation-practice.git, or git pull in it.
  • Where commands run: in a terminal on your own computer. n8n and a fake mail server run in two containers; you use n8n in your web browser.
  • The output below is from a real run on November 19, 2026, around 1am in New York.

The problem: a job that isn't on one computer

Every job so far in this track lived on one computer. Old backups, patches, supplier files, user accounts, a full disk, a database password, a folder of spreadsheets: a script on that computer, started by a timer, a file or a person.

The bookshop's next job is different. Customers ask for books the shop doesn't stock, through a form on its website. Today the form emails a shared inbox, and someone copies each request into an order list when they have a minute. Half the requests have a typo in the ISBN (the 13-digit number on every book), so the staff can't find the book, and the customer never hears back.

The job should:

  1. Take the request from the website form.
  2. Check it: a 13-digit ISBN and an email address.
  3. Look the book up, in Open Library, a free online catalog, to get its title and make sure it exists.
  4. Email the staff with the request.
  5. Answer the customer straight away: "thanks", or exactly what was wrong.

None of these steps happens on the bookshop's server. They're messages between apps: a website, an outside service and email. And the staff who'll look after it want to see what it does, and why last Tuesday's request went missing, without reading code.

This is what workflow tools are for. Job listings name them all the time: n8n, Zapier, Make, Microsoft Power Automate. They all work the same way: you draw the job as boxes (nodes), one per step, joined by arrows. Each run is saved, with what went into and came out of each box. This lesson uses n8n, because you can run it yourself, for free, in Docker.

The lab

On your own computer:

BASH
cd ~/Apps/automation-practice/n8n
docker network create n8n-lab
docker run -d --name mailpit --network n8n-lab -p 8025:8025 axllent/mailpit
docker run -d --name n8n --network n8n-lab -p 5678:5678 \
  -e N8N_DIAGNOSTICS_ENABLED=false -e GENERIC_TIMEZONE=UTC \
  -v n8n_lab_data:/home/node/.n8n -v "$PWD":/files:ro \
  docker.n8n.io/n8nio/n8n
sleep 20
docker logs n8n 2>&1 | tail -3
OUTPUT
Editor is now accessible via:
http://localhost:5678
  • docker network create n8n-lab: a private network, so the two containers can reach each other by name.
  • The first docker run: Mailpit, Lesson 7's fake mail server, in the background (-d). Its web page is at http://localhost:8025.
  • The second docker run: n8n, named n8n, on the same network.
    • -p 5678:5678: n8n's web page, at http://localhost:5678.
    • -e N8N_DIAGNOSTICS_ENABLED=false: don't send usage data to n8n's makers. -e GENERIC_TIMEZONE=UTC: schedules and times in UTC.
    • -v n8n_lab_data:/home/node/.n8n: a Docker volume called n8n_lab_data, where n8n keeps its database: workflows, credentials and the history of every run.
    • -v "$PWD":/files:ro: this folder, inside the container at /files, read-only (ro), so n8n can import the lab's files.
    • docker.n8n.io/n8nio/n8n: n8n's official image.
  • sleep 20, then docker logs n8n 2>&1 | tail -3: give it 20 seconds to start, then show the last three lines of its log. 2>&1 includes its error output, where much of the log goes.

Check a volume's name before you use it

If a volume with that name already exists, Docker uses it, without a word. While building this lab, the first run used n8n's usual name, n8n_data, and that computer already had a real n8n using it. The lab's workflows were imported into the real one, next to its own. Nothing was lost, but it could have been: the clean-up step deletes the volume. Before you use a volume name, docker volume ls shows what's there. This lab uses its own name, n8n_lab_data.

Open http://localhost:5678 in your browser and make the owner account it asks for. It's only on your computer, so any email address and password will do.

Load the workflows from files

The lab's two workflows and its mail server login are JSON files in the n8n folder. n8n's command line imports them:

BASH
docker exec n8n n8n import:credentials --input=/files/credentials.json
docker exec n8n n8n import:workflow --input=/files/failure-alerts.json
docker exec n8n n8n import:workflow --input=/files/book-requests.json
docker exec n8n n8n publish:workflow --id=bookRequest001
docker exec n8n n8n list:workflow
OUTPUT
Successfully imported 1 credential.
Importing 1 workflows...
Successfully imported 1 workflow.
Importing 1 workflows...
Successfully imported 1 workflow.
Publishing workflow with ID: bookRequest001 (current version)
A running n8n applies this change shortly. If n8n is stopped, it applies the change when it starts.
bookAlerts0001|Alert when a workflow fails
bookRequest001|Book requests
  • docker exec n8n n8n ...: run the n8n command inside the container called n8n.
  • import:credentials: load a saved login, here for the mail server. n8n keeps logins (credentials) apart from workflows and encrypts them, so a workflow file can be shared without its passwords.
  • import:workflow: load a workflow. The alert workflow goes first, because Book requests names it.
  • publish:workflow --id=bookRequest001: switch it on, so its web address answers real requests. A workflow that isn't published only runs when you click to test it.
  • list:workflow: list every workflow, as id|name.

The real output also has a few lines starting [license SDK], about n8n's license, between these. They're normal, and left out here.

Workflows as files is the important part. They can live in Git, be reviewed like code, and be loaded into a new n8n in seconds. n8n export:workflow --all writes them back out.

The workflow

Refresh the page, and open Book requests:

The whole job on one screen. Each way it can end has its own box.

Seven nodes, read left to right:

NodeTypeWhat it does
Book request formWebhookGives the workflow a web address. The website's form sends each request there, as JSON: name, email and isbn.
Check the requestIfIs the ISBN exactly 13 digits, and does the email look like an email? true goes on; false goes to Reject the request.
Look up the bookHTTP RequestAsks Open Library for https://openlibrary.org/isbn/ISBN.json. Success: the book's details, including its title. Error: no such book.
Email the staffSend EmailEmails staff@bookshop.example: who asked, for which book. If the mail server doesn't answer, it tries 3 times, 2 seconds apart.
Thank the customerRespond to WebhookAnswers the form: 200, and the book's title.
Book not foundRespond to WebhookAnswers 404, with the ISBN they typed, so they can check it.
Reject the requestRespond to WebhookAnswers 400: what a good request looks like.

Double-click any node to see its settings. A few are worth a closer look.

The webhook answers later. By default, a webhook answers the moment a request arrives, before the workflow has done anything. This one is set to answer with a Respond to Webhook node instead, so the customer hears how it really went: thanks, not found, or what to fix.

The checks are patterns. Check the request has two conditions, both of which must be true:

TEXT
{{ String($json.body.isbn ?? '').trim() }}     matches regex   ^\d{13}$
{{ String($json.body.email ?? '').trim() }}    matches regex   ^[^@\s]+@[^@\s]+\.[^@\s]+$
  • {{ ... }}: an expression, a little JavaScript n8n works out for each request. $json.body.isbn is the isbn the form sent. ?? '' means "or empty, if it's missing", and .trim() drops spaces at either end.
  • ^\d{13}$: a regular expression (a pattern for text): exactly 13 digits, nothing before or after.
  • The email pattern: something, an @, something, a dot, something, with no spaces. It won't catch every bad address, but it catches the typos that matter: a missing @ or a missing domain.

A failed lookup goes somewhere. Normally, a node that fails stops the whole workflow. Look up the book is set to "On Error: Continue (using error output)", so a failed lookup goes out of its Error side, to Book not found. A book that doesn't exist is something to expect, not a crash.

Failures go to another workflow. In Book requests' settings (the ... menu at the top, then Settings), Error Workflow is set to Alert when a workflow fails. That one has two nodes: an Error Trigger, which starts whenever a workflow that names it fails, and an email to oncall@bookshop.example with the workflow's name, the step that failed and the error.

Try it

curl plays the website's form:

BASH
U=http://localhost:5678/webhook/book-request
curl -s -w '  [%{http_code}]\n' -X POST $U -H 'Content-Type: application/json' -d '{"name":"Ada","email":"ada@example.com","isbn":"9780141439518"}'
curl -s -w '  [%{http_code}]\n' -X POST $U -H 'Content-Type: application/json' -d '{"name":"Ada","email":"ada@example.com","isbn":"978-0141439518"}'
curl -s -w '  [%{http_code}]\n' -X POST $U -H 'Content-Type: application/json' -d '{"name":"Ada","email":"ada@example.com","isbn":"9780141439510"}'
curl -s -w '  [%{http_code}]\n' -X POST $U -H 'Content-Type: application/json' -d '{"name":"Ada","email":"ada","isbn":"9780141439518"}'
OUTPUT
{"ok":true,"message":"Thanks! We've asked our staff to find Pride and Prejudice."}  [200]
{"ok":false,"message":"Please give a 13-digit ISBN and an email address."}  [400]
{"ok":false,"message":"We couldn't find a book with ISBN 9780141439510. Please check the number."}  [404]
{"ok":false,"message":"Please give a 13-digit ISBN and an email address."}  [400]
  • U=...: the workflow's web address: n8n's address, /webhook/, then the path set in Book request form.
  • -s: silent, with no progress bar. -w ' [%{http_code}]\n': after the answer, print the HTTP status code in brackets.
  • -X POST: send the request as a POST, as a form does. -H 'Content-Type: application/json': say the request is JSON. -d '...': the request itself.

Each of the four ended in a different place:

  1. A good request: 200, and the book's real title, from Open Library.
  2. The ISBN with a hyphen, as it's printed on the back of the book: 400. The check works, but this is a real customer's way of typing it. The workflow should remove hyphens and spaces before checking, with .replace(/[-\s]/g, '') in both the check and the lookup. Rejecting what people naturally type is a bug, even when the code is doing what it was told.
  3. The last digit wrong: 404, and the number they typed, so they can check it. Open Library has no such book.
  4. No proper email: 400.

In Mailpit, at http://localhost:8025:

Only the good requests reach the staff. The middle one was meant to be a made-up number.

Only good requests reach the staff. The three bad ones were answered at once and never became email. The newest email is from the run above. The two older ones are from a first run, and one of them is a surprise.

9780000000000 was meant to be a made-up ISBN, to show the not-found path. It's a real book: Bertrand Barère, and Open Library has it. A number that looks fake can pass every check. When you test with made-up data, check it's really made up. Here, a wrong final digit is a safer "not found": an ISBN's last digit is a check digit, worked out from the other twelve, so changing it makes a number that can't belong to any book.

Every run is saved

Switch from Editor to Executions, at the top. Each request is a row, green or red, with how long it took. Click one to see the workflow as it ran: which path it took, and exactly what went into and came out of each node. This is a workflow tool's best feature. When a customer says "I asked for a book last week and heard nothing", you can find their request and see what happened to it, without anyone having added logging.

When the mail server is down

Stop the mail server, and send one more request:

BASH
docker stop mailpit
time curl -s -w '  [%{http_code}]\n' -X POST $U -H 'Content-Type: application/json' -d '{"name":"Grace","email":"grace@example.com","isbn":"9780141439518"}'
OUTPUT
{"message":"Error in workflow"}  [500]
  • docker stop mailpit: stops the Mailpit container, as if the mail server had gone down.
  • time: runs the command after it, then says how long it took.

The customer waited 25.4 seconds, then got an error that tells them nothing. In Executions:

The failed run, step by step. Everything worked until the email.

The request was good, and the book was found. Then Email the staff failed: "The host is unreachable, perhaps the server is offline." So the customer never reached Thank the customer, and n8n answered for the workflow, with a 500.

Why 25 seconds? Email the staff tried 3 times, 2 seconds apart, as it was told. That's 4 seconds of waiting. The rest is each try waiting to find the mail server. Its name, mailpit, belongs to a container that's stopped, so each try waits for the name lookup to give up. Retries are right for a mail server that blips for a moment, but they all happen while the customer waits.

The alert that couldn't be sent

The failure started Alert when a workflow fails, as it should. Open it, and look at its Executions: its run failed too. It tried to send the alert through the same mail server that was down.

Nobody was told. This is the most common mistake with alerts: the alert must not depend on the thing that broke. An alert about email has to go some other way: a chat message (n8n has Slack and Microsoft Teams nodes), a text, or a heartbeat check like Lesson 2's, which tells you when something doesn't check in.

Getting the request back

Grace's request isn't lost. n8n saved the run, with her request in it. Start the mail server again:

BASH
docker start mailpit

Then, in Book requests' Executions, the red run has a Retry button (a circular arrow). "Retry with original workflow" runs it again from the step that failed, with the same data, and the workflow as it was when it failed. "Retry with currently saved workflow" uses the workflow as it is now, for when you've fixed the workflow itself.

That emails the staff. But Grace was already told "Error in workflow". She doesn't know it worked in the end, so she may send it again, or give up.

One more thing Mailpit shows: after the restart, its inbox is empty. Mailpit keeps mail in memory, so stopping it wiped the earlier emails. A test inbox doesn't keep anything.

A better order

The workflow answers the customer last, after everything has worked. That's why a broken mail server became the customer's problem. Two better ways:

  • Answer first, then do the work. Put Thank the customer straight after Look up the book. The customer hears "thanks" in about a second. If the email fails after that, the error workflow and Retry are for the staff to deal with, not the customer.
  • Keep the request, then work through it. Write each request to a table first (n8n has its own Data tables, or a Google Sheet, or a database), answer the customer, and send the emails from the table, marking each one sent. Nothing lives only in a failed run.

The rule is the same as Lesson 3's supplier files: keep the input safe first, then process it.

Where each job belongs

This is the last lesson in the track, so here's every job in it, and why it lives where it does:

JobWhere it livesWhy there
1. Clean up old backupsBash script, timer, on the serverIt's about files on that server.
2. Patch a serverBash script, systemd timer, heartbeatIt changes that server, and must check it afterwards. For many servers, Ansible.
3. When a file arrivessystemd path unit, scriptStarts the moment the file lands, on the server it lands on.
4. Add and remove usersScript run by a person, roster syncA person decides who leaves; the script makes sure nothing's missed.
5. A runbookScript run by a person, with a confirmationSomeone's judgment starts it; the script does the steps the same way every time.
6. Rotate a passwordScript, by hand or on a scheduleOrder matters, and every step is checked before the next.
7. A weekly reportPython, a timer, testsLots of checking logic. Tests keep it right as it grows.
8. Book requestsn8nPasses work between a website, an outside service and email. People who don't code need to see each run.

A rule of thumb:

  • It touches one server's files, services or users? A script on that server, started by systemd: a timer, a path unit, or a person.
  • It passes messages between web apps, each step is simple, and people besides engineers look after it? A workflow tool.
  • It has a lot of logic, like the weekly report's checks? Code, with tests. A workflow can still start it: n8n has nodes to run a command or call a web address, so code can do the hard part inside a workflow.
  • Either way, it needs a safe default, a clear record of what it did, and a way to tell a person when it fails that doesn't depend on the thing that failed.

Clean up

BASH
docker rm -f n8n mailpit
docker volume rm n8n_lab_data
docker network rm n8n-lab
  • docker rm -f n8n mailpit: stops and deletes both containers (-f: even though they're running).
  • docker volume rm n8n_lab_data: deletes the lab's n8n database. Only the lab's: check the name.
  • docker network rm n8n-lab: deletes the network.

In real life

  • Protect the webhook. Anyone who finds its address can send requests. Give the Webhook node a login (it has Header Auth and Basic Auth), or put it behind the website, which adds the secret. And limit how often one person can send.
  • Back up the encryption key. n8n encrypts credentials with a key it keeps in its data folder, or in N8N_ENCRYPTION_KEY. Lose it, and every saved login has to be entered again.
  • Keep workflows in Git. n8n export:workflow --all --separate --output=DIR writes one file per workflow. Review changes as you would code.
  • Pin the version. Use a version number in the image name, not latest, and read n8n's release notes before you upgrade, as with any server software.
  • Don't keep every run forever. Saved runs hold customers' names and emails. n8n can delete old ones for you: look for EXECUTIONS_DATA_PRUNE and EXECUTIONS_DATA_MAX_AGE in its settings.
  • n8n's license: the self-hosted Community Edition is free to run for your own business. Check the license before selling n8n itself as a service to others. n8n Cloud, run by n8n's makers, is paid.

Spot the problem

Work out what's wrong before you read the explanation.

"It worked when I tested it." Someone builds a workflow like this one in n8n's editor. They click Execute at the bottom of the editor, send a request to the address shown, and it works. They give the website that address. The next day, every request from the website fails with a 404, and Executions shows nothing at all. What's wrong? They gave the website the test address. A Webhook node has two: a test URL, with /webhook-test/ in it, which only listens while you've clicked to test, and once; and a production URL, with /webhook/, which answers only once the workflow is published. The fix: publish the workflow, and give the website the production URL. And test the address the website will really use, as this lesson's curl commands did.

Keep this

ToUse
Run n8n in Docker, with its own datadocker run -d --name n8n -p 5678:5678 -v NAME:/home/node/.n8n docker.n8n.io/n8nio/n8n
See which volumes existdocker volume ls
Import a login, or a workflown8n import:credentials --input=FILE, n8n import:workflow --input=FILE
Switch a workflow onn8n publish:workflow --id=ID
Save workflows as filesn8n export:workflow --all --separate --output=DIR
Start a workflow from a web requestWebhook node; answer with Respond to Webhook
Send a failure down its own pathNode settings: On Error, Continue (using error output)
Try again before failingNode settings: Retry On Fail, Max Tries, Wait Between Tries
Be told when a workflow failsA workflow starting with Error Trigger, named in Settings, Error Workflow
See what happened to a requestExecutions, then click the run
Run a failed request againExecutions, Retry, "Retry with original workflow"

Where you are now

You've built a workflow that passes work between a web form, an outside service and email, and answers every kind of request properly. You've broken its mail server, and seen the real cost: a customer waiting 25 seconds for an error, and an alert that couldn't be sent because it used the same broken mail server. You know how to get a failed request back, and how to order a workflow so the customer isn't the one who pays when something breaks.

That's the end of the Automation track. Across eight jobs, the same ideas kept coming back: do nothing risky by default, check before and after, keep the input safe, say clearly what went wrong, and tell a person in a way that doesn't depend on the broken thing. Those matter more than which tool you use.

Check yourself

  1. Why does this workflow use a Respond to Webhook node instead of the webhook's default answer?

  2. A customer types the ISBN 978-0141439518, as printed on the book. What happens, and what should change?

  3. With the mail server down, why did the customer wait 25 seconds?

  4. Why did nobody get the alert about the failure?

  5. What does "Retry with original workflow" do?

  6. Which job from this track is the best fit for a workflow tool like n8n?

Key terms

  • Workflow tool: an app where you draw a job as connected steps, and it runs them and saves every run. n8n, Zapier, Make and Power Automate are workflow tools.
  • n8n: a workflow tool you can run yourself, in Docker or on a server.
  • Node: one step in a workflow.
  • Webhook: a web address that starts a workflow when a request is sent to it.
  • Credential: a saved login n8n keeps apart from workflows, encrypted.
  • Execution: one run of a workflow, saved with what went into and came out of each node.
  • Error workflow: a workflow that starts when another one fails, to tell someone.
  • Publish: switch a workflow on, so its production address answers.
  • Docker volume: storage Docker keeps for a container, by name, after the container is gone.

Go deeper