Noucair ZemraniPlatform engineering & practical automation

ENGINEERING NOTES

3 n8n Workflow Examples for Business Automation

By Noucair Zemrani ·

Use these three n8n workflow examples to capture leads, triage invoice PDFs, and report open tickets in Slack. Each example has its own n8n export, setup instructions, and a check you can run with fictional data.

For each example, expand the workflow JSON, copy it to a file with the shown name, and use Import from File in n8n. Configure the credentials and endpoints in your test environment before running it.

Three independent workflows
  1. Lead: Webhook → validate → valid: PostgreSQL upsert and HTTP 200 · invalid: HTTP 422
  2. Invoice: PDF upload → text extraction → field checks → store ready or review record → HTTP 201
  3. Report: Schedule → fetch tickets → build count or failure message → Slack

1. Capture a website lead without creating a duplicate

Alex fills out your website form twice with the same email. The first submission creates a contact. The second updates it. If Alex leaves the email blank, the workflow asks for a valid email and saves nothing.

The webhook checks the name and email before writing anything. PostgreSQL uses the normalized email as the primary key, so simultaneous requests for the same email still converge on one row.

Setup. Create this table in a disposable PostgreSQL database. Select that database credential in Upsert lead and copy the test webhook URL.

CREATE TABLE demo_leads (email text PRIMARY KEY, name text NOT NULL, source text NOT NULL);
Copy the n8n workflow JSON for lead-capture.json
{
  "name": "Lead capture with PostgreSQL upsert",
  "nodes": [
    {
      "id": "584328d8-d20f-480a-8cd7-8d377ea9e810",
      "name": "Lead webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "parameters": {
        "httpMethod": "POST",
        "path": "portfolio-lead-upsert",
        "responseMode": "responseNode",
        "options": {}
      },
      "position": [
        0,
        0
      ],
      "webhookId": "584328d8-d20f-480a-8cd7-8d377ea9e819"
    },
    {
      "id": "584328d8-d20f-480a-8cd7-8d377ea9e811",
      "name": "Validate lead",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "jsCode": "const b = $input.first().json.body ?? {};\nconst email = typeof b.email === 'string' ? b.email.trim().toLowerCase() : '';\nconst name = typeof b.name === 'string' ? b.name.trim() : '';\nconst source = typeof b.source === 'string' ? b.source.trim() : 'website';\nconst valid = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/.test(email) && email.length <= 254 && name.length > 0 && name.length <= 120 && source.length <= 80;\nreturn [{json:{email,name,source,valid}}];"
      },
      "position": [
        240,
        0
      ]
    },
    {
      "name": "Is lead valid?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "fe710722-2c9f-44c3-8707-b2cf0f1388a5",
              "leftValue": "={{ $json.valid }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "584328d8-d20f-480a-8cd7-8d377ea9e820",
      "position": [
        480,
        0
      ]
    },
    {
      "id": "584328d8-d20f-480a-8cd7-8d377ea9e812",
      "name": "Upsert lead",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO demo_leads (email, name, source)\nVALUES ($1::jsonb->>'email', $1::jsonb->>'name', $1::jsonb->>'source')\nON CONFLICT (email) DO UPDATE SET name=EXCLUDED.name, source=EXCLUDED.source\nRETURNING email, name, source;",
        "options": {
          "queryReplacement": "={{ [JSON.stringify($json)] }}"
        }
      },
      "position": [
        730,
        -120
      ]
    },
    {
      "id": "584328d8-d20f-480a-8cd7-8d377ea9e813",
      "name": "Return result",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ $json }}",
        "options": {
          "responseCode": 200
        }
      },
      "position": [
        970,
        -120
      ]
    },
    {
      "name": "Reject invalid lead",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "parameters": {
        "respondWith": "json",
        "responseBody": "{\"error\":\"Provide a valid email and name\"}",
        "options": {
          "responseCode": 422
        }
      },
      "id": "584328d8-d20f-480a-8cd7-8d377ea9e821",
      "position": [
        730,
        130
      ]
    }
  ],
  "connections": {
    "Lead webhook": {
      "main": [
        [
          {
            "node": "Validate lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate lead": {
      "main": [
        [
          {
            "node": "Is lead valid?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is lead valid?": {
      "main": [
        [
          {
            "node": "Upsert lead",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Reject invalid lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upsert lead": {
      "main": [
        [
          {
            "node": "Return result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  }
}

Test. Start listening for a test event before each POST. Submit the two valid bodies in order, then the invalid body. Expect 200, 200, and 422. Check that the table contains one row for [email protected] with the name Alex Rivera.

{"name":"Alex Morgan","email":" [email protected] "}
{"name":"Alex Rivera","email":"[email protected]"}
{"name":"Missing Email"}

2. Extract fields from a PDF invoice and queue incomplete records

You upload a bill from a supplier. If the workflow finds the invoice number, seller, currency, and total, it marks the bill ready. If a field is missing, it saves the bill for someone to review and says what is missing.

The webhook receives the PDF, Extract From File reads its text, and the classification step stores either a ready record or a review record with missing fields. The review queue is a PostgreSQL table filtered by status.

Setup. Create the table below and select its database credential in Store invoice or review item. Create a text-based PDF with four separate lines beginning with Invoice number:, Vendor:, Currency:, and Total:. Use a three-letter uppercase currency and a plain decimal total. Scanned PDFs require an OCR step.

CREATE TABLE demo_invoice_intake (id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, status text NOT NULL, payload jsonb NOT NULL);
Copy the n8n workflow JSON for invoice-intake.json
{
  "name": "Invoice PDF intake",
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "nodes": [
    {
      "name": "Receive PDF",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "parameters": {
        "httpMethod": "POST",
        "path": "portfolio-invoice-pdf",
        "responseMode": "responseNode",
        "options": {
          "binaryData": true,
          "binaryPropertyName": "data"
        }
      },
      "webhookId": "16570000-6834-4e2e-8eae-000000000099",
      "id": "16570000-6834-4e2e-8eae-000000000001",
      "position": [
        0,
        0
      ]
    },
    {
      "name": "Extract PDF text",
      "type": "n8n-nodes-base.extractFromFile",
      "typeVersion": 1,
      "parameters": {
        "operation": "pdf",
        "binaryPropertyName": "data",
        "options": {}
      },
      "id": "16570000-6834-4e2e-8eae-000000000002",
      "position": [
        245,
        0
      ]
    },
    {
      "name": "Classify invoice",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "jsCode": "const text = String($input.first().json.text ?? '');\nconst value = label => text.split(/\\r?\\n/).find(line => line.startsWith(label))?.slice(label.length).trim() ?? '';\nconst invoice = value('Invoice number:');\nconst vendor = value('Vendor:');\nconst currency = value('Currency:');\nconst total = value('Total:');\nconst missing = [];\nif (!invoice) missing.push('invoice number');\nif (!vendor) missing.push('vendor');\nif (!/^[A-Z]{3}$/.test(currency)) missing.push('currency');\nif (!/^\\d+(\\.\\d{1,2})?$/.test(total)) missing.push('total');\nreturn [{json:{invoice,vendor,currency,total,status:missing.length ? 'review' : 'ready',reason:missing.join(', ')}}];"
      },
      "id": "16570000-6834-4e2e-8eae-000000000003",
      "position": [
        490,
        0
      ]
    },
    {
      "name": "Store invoice or review item",
      "type": "n8n-nodes-base.postgres",
      "typeVersion": 2.6,
      "parameters": {
        "operation": "executeQuery",
        "query": "INSERT INTO demo_invoice_intake (status, payload) VALUES ($1::jsonb->>'status', $1::jsonb) RETURNING id, status, payload->>'reason' AS reason;",
        "options": {
          "queryReplacement": "={{ [JSON.stringify($json)] }}"
        }
      },
      "id": "16570000-6834-4e2e-8eae-000000000004",
      "position": [
        735,
        0
      ]
    },
    {
      "name": "Return intake status",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.4,
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ $json }}",
        "options": {
          "responseCode": 201
        }
      },
      "id": "16570000-6834-4e2e-8eae-000000000005",
      "position": [
        980,
        0
      ]
    }
  ],
  "connections": {
    "Receive PDF": {
      "main": [
        [
          {
            "node": "Extract PDF text",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract PDF text": {
      "main": [
        [
          {
            "node": "Classify invoice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Classify invoice": {
      "main": [
        [
          {
            "node": "Store invoice or review item",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Store invoice or review item": {
      "main": [
        [
          {
            "node": "Return intake status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Test. Start the test webhook and POST the PDF as the raw request body with Content-Type application/pdf. Repeat with a PDF that omits the invoice number. Expect HTTP 201 for each saved record, with status ready on the first and review on the second. Inspect the review queue with the query below.

SELECT id, payload->>'reason' AS reason FROM demo_invoice_intake WHERE status = 'review';

3. Send a scheduled Slack report that distinguishes zero from failure

Every morning, the workflow checks how many support tickets are open and tells the team in Slack. If there are zero, it says “0 open tickets.” If the system is down, it says “Ticket report unavailable.”

The HTTP node keeps the response status and body. A valid empty tickets array produces a zero-count message. An unavailable API or an unexpected response produces a failure message.

Setup. Replace both placeholder URLs with your test tickets endpoint and Slack incoming webhook. The tickets endpoint should return an object with a tickets array containing open tickets. Keep the Slack URL private. Set the workflow time zone before enabling the 9 a.m. schedule.

Copy the n8n workflow JSON for slack-ticket-report.json
{
  "name": "Scheduled Slack ticket report",
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "nodes": [
    {
      "name": "Run test manually",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "parameters": {},
      "id": "23570000-6834-4e2e-8eae-000000000001",
      "position": [
        0,
        0
      ]
    },
    {
      "name": "Run every morning",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "days",
              "daysInterval": 1,
              "triggerAtHour": 9,
              "triggerAtMinute": 0
            }
          ]
        }
      },
      "id": "23570000-6834-4e2e-8eae-000000000002",
      "position": [
        245,
        0
      ]
    },
    {
      "name": "Fetch tickets",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "parameters": {
        "url": "https://example.invalid/tickets",
        "options": {
          "timeout": 5000,
          "response": {
            "response": {
              "fullResponse": true,
              "neverError": true,
              "responseFormat": "json"
            }
          }
        }
      },
      "onError": "continueRegularOutput",
      "id": "23570000-6834-4e2e-8eae-000000000003",
      "position": [
        490,
        0
      ]
    },
    {
      "name": "Prepare report or alert",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "jsCode": "const result = $input.first().json;\nconst ok = result.statusCode >= 200 && result.statusCode < 300 && Array.isArray(result.body?.tickets);\nconst at = new Date().toISOString();\nconst text = ok ? 'Ticket report: ' + result.body.tickets.length + ' open tickets.' : 'Ticket report unavailable (source: tickets API, HTTP ' + (result.statusCode ?? 'unavailable') + ').';\nreturn [{json:{text:text + ' Run time: ' + at}}];"
      },
      "id": "23570000-6834-4e2e-8eae-000000000004",
      "position": [
        735,
        0
      ]
    },
    {
      "name": "Send to Slack",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "parameters": {
        "method": "POST",
        "url": "https://example.invalid/slack-webhook",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ text: $json.text }) }}",
        "options": {
          "timeout": 5000
        }
      },
      "id": "23570000-6834-4e2e-8eae-000000000005",
      "position": [
        980,
        0
      ]
    }
  ],
  "connections": {
    "Run test manually": {
      "main": [
        [
          {
            "node": "Fetch tickets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Run every morning": {
      "main": [
        [
          {
            "node": "Fetch tickets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch tickets": {
      "main": [
        [
          {
            "node": "Prepare report or alert",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare report or alert": {
      "main": [
        [
          {
            "node": "Send to Slack",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

Test. Use the manual trigger with an endpoint returning HTTP 200 and the body below. Expect “0 open tickets” in the test Slack channel. Run again with an endpoint returning HTTP 503. Expect “Ticket report unavailable” with the run time.

{"tickets":[]}

For more on designing failure paths, see API timeouts and safe retries and Practical Automation.

← All engineering articles