Go to app

Code for the n8n guides

The n8n guides keep to clicks and short expressions. The longer pieces they use are here: the Docker command for a self-hosted n8n, and the Code nodes of the example workflows. Each example workflow already carries its Code nodes, so you need these only to build the workflow by hand or to change it.


Run n8n in Docker with a public address

For Tell n8n its public address. Replace https://n8n.example.com/ with your own address:

bash
docker run -d --name n8n -p 5678:5678 \
  -e WEBHOOK_URL=https://n8n.example.com/ \
  -e N8N_EDITOR_BASE_URL=https://n8n.example.com/ \
  -e N8N_PROXY_HOPS=1 \
  -v n8n_data:/home/node/.n8n \
  docker.n8n.io/n8nio/n8n

Build the checklist

The Build the checklist Code node of Collect the documents each case needs. It looks up the documents for the claim type and writes the form as blocks. The throw stops the run on an unknown claim type instead of sending a form with no questions. Every block type and option is in Create a form and a request in one call.

javascript
const checklists = {
  water_damage: [
    { key: 'damage_photos', title: 'Photos of the damage', maxFiles: 10 },
    { key: 'repair_estimate', title: 'Repair estimate from a contractor', maxFiles: 1 },
  ],
  theft: [
    { key: 'police_report', title: 'Police report', maxFiles: 1 },
    { key: 'receipts', title: 'Receipts for the stolen items', maxFiles: 10 },
  ],
}
const documents = checklists[$json.claimType]
if (!documents) throw new Error(`No checklist for claim type ${$json.claimType}`)
const blocks = [
  { type: 'header', content: 'Hi {{first_name}}, a few documents for claim {{claim_id}}' },
  { type: 'paragraph', content: 'Upload each document below. Photos from your phone are fine.' },
  { type: 'hiddenField', key: 'first_name' },
  { type: 'hiddenField', key: 'claim_id' },
  ...documents.map((document) => ({ type: 'file', ...document })),
  { type: 'text', key: 'notes', title: 'Anything we should know?', required: false },
]
return { json: { ...$json, blocks } }

One item per file

The Code node after the If node in Collect the documents each case needs. It walks the answers instead of naming the questions, so it works for any claim type:

javascript
// One item per uploaded file, named after the question it answers.
const event = $json.body.data
const items = []
for (const [question, answer] of Object.entries(event.answers)) {
  if (!Array.isArray(answer)) continue
  for (const file of answer) {
    if (file.url) items.push({ json: { question, name: `${question} - ${file.name}`, url: file.url } })
  }
}
return items

Compare the AI reading with the confirmed answers

The Code node of Read a PDF with AI, then get it confirmed. It lists the keys whose confirmed answer differs from what the AI read:

javascript
// Compare what the AI read with what the supplier confirmed.
const read = $('Read the invoice with AI').item.json.output
const confirmed = $json.body.data.answers
const corrected = Object.keys(read).filter((key) => String(read[key]) !== String(confirmed[key]))
return { json: { invoice: confirmed, corrected, notes: confirmed.notes ?? null } }