# Code for the n8n guides

The Docker command for a self-hosted n8n, and the Code nodes the n8n example workflows run: build a form from a checklist, split the uploads into one item per file, and compare an AI reading with the confirmed answers.

## Run n8n in Docker with a public address

For [Tell n8n its public address](https://formstep.io/docs/guides/n8n/install-the-node/#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](https://formstep.io/docs/guides/n8n/collect-documents/#build-the-checklist). 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](https://formstep.io/docs/developers/inline-forms/#blocks).

```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](https://formstep.io/docs/guides/n8n/collect-documents/#one-item-per-file). 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](https://formstep.io/docs/guides/n8n/read-a-pdf-with-ai/#compare). 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 } }
```
