{
  "name": "Answer questions with cited Wikipedia summaries using WikiVectors",
  "nodes": [
    {
      "parameters": {
        "content": "## Answer questions with Wikipedia citations\n\n**Who it's for:** anyone who wants short, sourced answers from Wikipedia in a form, chat or bot, without the model making things up.\n\n**What it does:** you ask a question in a form. [WikiVectors](https://wikivectors.com) searches current English Wikipedia (updated within the hour) and writes a 1-3 sentence answer only from the paragraphs it found, citing each one with its link and exact revision.\n\n- **Answered:** you see the answer and its Wikipedia sources.\n- **Not answered:** nothing is made up. The workflow searches again and shows the closest Wikipedia paragraphs, so you can read them yourself.\n- **Call failed:** you see what to fix (key, credits, too many calls).\n\n### Set up (2 minutes)\n1. Get a free key at https://wikivectors.com (no card needed).\n2. Open **Ask WikiVectors**. Under Credential, create a new **Bearer Auth** credential and paste your key into it.\n3. Open **Find closest paragraphs** and pick the same credential.\n4. Click **Test workflow**, open the form, and ask something like *Who won the 2026 Tour de France?*\n\n### Cost and limits\n- 1,000 free searches a month. One answer uses 5 searches and the fallback search uses 1, so the free month covers about 160-200 questions. After that $2 per 1,000 searches, prepaid.\n- English Wikipedia only. An answer takes about 2-3 seconds.\n\n### Change it\n- Swap the form for a Chat, Slack or Telegram trigger and send its text as `query`.\n- Change how many paragraphs the fallback shows in **Settings**.\n- Docs: https://wikivectors.com/docs",
        "height": 900,
        "width": 500,
        "color": 4
      },
      "id": "6f1d2a3e-0001-4a51-9b1e-5c1f0e6a0001",
      "name": "Overview",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -620,
        -260
      ]
    },
    {
      "parameters": {
        "content": "### 1. Ask for a cited answer\n**Settings** holds the one value you may want to change. **Ask WikiVectors** calls `POST /v1/answer` with your question. It is set to continue on error, so a bad key or used-up credits reach the next step instead of stopping the run.",
        "height": 520,
        "width": 680,
        "color": 7
      },
      "id": "6f1d2a3e-0020-4a51-9b1e-5c1f0e6a0020",
      "name": "Step 1 note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -60,
        -260
      ]
    },
    {
      "parameters": {
        "content": "### 2. Answered, failed, or no plain answer\n**Answered?** checks the reply. If WikiVectors answered, we show it with its sources. If the call failed, **Explain the error** says what to fix. Otherwise we never guess: we search for the closest paragraphs (`POST /v1/search`) and show those with their links.",
        "height": 720,
        "width": 660,
        "color": 7
      },
      "id": "6f1d2a3e-0021-4a51-9b1e-5c1f0e6a0021",
      "name": "Step 2 note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        660,
        -260
      ]
    },
    {
      "parameters": {
        "content": "### 3. Show the result\nEach branch ends on a form result page. Every source links to the exact Wikipedia revision it came from (CC BY-SA 4.0).",
        "height": 720,
        "width": 520,
        "color": 7
      },
      "id": "6f1d2a3e-0022-4a51-9b1e-5c1f0e6a0022",
      "name": "Step 3 note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1360,
        -260
      ]
    },
    {
      "parameters": {
        "formTitle": "Ask Wikipedia",
        "formDescription": "A short answer from current English Wikipedia, with its sources.",
        "formFields": {
          "values": [
            {
              "fieldLabel": "Question",
              "placeholder": "Who won the 2026 Tour de France?",
              "requiredField": true
            }
          ]
        },
        "options": {
          "buttonLabel": "Ask"
        }
      },
      "id": "6f1d2a3e-0002-4a51-9b1e-5c1f0e6a0002",
      "name": "Ask a question",
      "type": "n8n-nodes-base.formTrigger",
      "typeVersion": 2.2,
      "position": [
        0,
        0
      ],
      "webhookId": "6f1d2a3e-0003-4a51-9b1e-5c1f0e6a0003"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "6f1d2a3e-0030-4a51-9b1e-5c1f0e6a0030",
              "name": "closest_paragraphs",
              "value": 3,
              "type": "number"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "6f1d2a3e-0008-4a51-9b1e-5c1f0e6a0008",
      "name": "Settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://wikivectors.com/v1/answer",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ query: $json.Question }) }}",
        "options": {
          "timeout": 30000
        }
      },
      "id": "6f1d2a3e-0004-4a51-9b1e-5c1f0e6a0004",
      "name": "Ask WikiVectors",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        0
      ],
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "6f1d2a3e-0031-4a51-9b1e-5c1f0e6a0031",
              "leftValue": "={{ $json.answered }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      },
      "id": "6f1d2a3e-0009-4a51-9b1e-5c1f0e6a0009",
      "name": "Answered?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        720,
        0
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "6f1d2a3e-0032-4a51-9b1e-5c1f0e6a0032",
              "leftValue": "={{ $json.error }}",
              "rightValue": "",
              "operator": {
                "type": "object",
                "operation": "exists",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "looseTypeValidation": true,
        "options": {}
      },
      "id": "6f1d2a3e-0010-4a51-9b1e-5c1f0e6a0010",
      "name": "Call failed?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        940,
        220
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://wikivectors.com/v1/search",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpBearerAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ query: $('Ask a question').first().json.Question, limit: $('Settings').first().json.closest_paragraphs }) }}",
        "options": {
          "timeout": 30000
        }
      },
      "id": "6f1d2a3e-0011-4a51-9b1e-5c1f0e6a0011",
      "name": "Find closest paragraphs",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1160,
        380
      ],
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "jsCode": "const esc = (s) => String(s ?? '').replace(/[&<>\"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '\"': '&quot;' }[c]));\n// The answer is 1-3 sentences with [1] style citations. List each cited\n// Wikipedia paragraph with its link, the exact revision and the license.\nconst q = $('Ask a question').first().json.Question;\nconst r = $input.first().json;\n\nconst sources = (r.sources || []).map((s) => ({\n  ref: s.ref,\n  title: s.title,\n  section: s.section || '',\n  url: s.permalink || s.url,\n  revision_id: s.revision_id,\n  license: s.license,\n}));\nconst list = sources.map((s) =>\n  `[${s.ref}] <a href=\"${esc(s.url)}\" target=\"_blank\">${esc(s.title)}${s.section ? ' &ndash; ' + esc(s.section) : ''}</a> (revision ${esc(s.revision_id)}, ${esc(s.license)})`\n).join('<br>');\n\nreturn [{ json: {\n  question: q,\n  answered: true,\n  answer: r.answer,\n  detail: r.detail,\n  sources,\n  title: r.answer,\n  message: `${esc(r.detail)}<br><br>Sources from Wikipedia:<br>${list}`,\n} }];"
      },
      "id": "6f1d2a3e-0005-4a51-9b1e-5c1f0e6a0005",
      "name": "Format answer and sources",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1160,
        -120
      ]
    },
    {
      "parameters": {
        "jsCode": "const esc = (s) => String(s ?? '').replace(/[&<>\"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '\"': '&quot;' }[c]));\n// \"Ask WikiVectors\" continues on error, so a failed call arrives here.\n// Turn the error into the one thing the person should fix.\nconst q = $('Ask a question').first().json.Question;\nconst text = JSON.stringify($input.first().json.error);\nlet fix = 'WikiVectors could not be reached. Try again in a minute.';\nif (/401|api_key/.test(text)) fix = 'The key is missing or wrong. Open the \"Ask WikiVectors\" node and check its credential.';\nelse if (/quota_exceeded/.test(text)) fix = \"This month's free searches and credits are used up. Add credits at https://wikivectors.com/account.\";\nelse if (/429|rate_limited/.test(text)) fix = 'Too many questions at once. Wait a few seconds and ask again.';\n\nreturn [{ json: { question: q, answered: false, error: text, title: 'Something went wrong', message: esc(fix) } }];"
      },
      "id": "6f1d2a3e-0012-4a51-9b1e-5c1f0e6a0012",
      "name": "Explain the error",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1160,
        160
      ]
    },
    {
      "parameters": {
        "jsCode": "const esc = (s) => String(s ?? '').replace(/[&<>\"]/g, (c) => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '\"': '&quot;' }[c]));\n// No answer was written (Wikipedia doesn't say it plainly), so nothing is\n// made up. Show the closest Wikipedia paragraphs instead, each with its link.\nconst q = $('Ask a question').first().json.Question;\nconst r = $input.first().json;\nconst max = $('Settings').first().json.closest_paragraphs;\nconst hits = (r.results || []).slice(0, max);\n\nif (r.error || hits.length === 0) {\n  return [{ json: { question: q, answered: false, title: \"Wikipedia doesn't say\",\n    message: 'No answer was made up, and no close paragraph was found. Try asking it another way.' } }];\n}\n\nconst short = (t) => (t.length > 400 ? t.slice(0, 400).replace(/\\s+\\S*$/, '') + ' ...' : t);\nconst list = hits.map((h, i) =>\n  `<b>${i + 1}. <a href=\"${esc(h.permalink || h.url)}\" target=\"_blank\">${esc(h.title)}${h.section ? ' &ndash; ' + esc(h.section) : ''}</a></b><br>${esc(short(h.text))}`\n).join('<br><br>');\n\nreturn [{ json: {\n  question: q,\n  answered: false,\n  paragraphs: hits.map((h) => ({ title: h.title, section: h.section, text: h.text, url: h.permalink || h.url, revision_id: h.revision_id })),\n  title: \"Wikipedia doesn't say it plainly\",\n  message: `No answer was made up. These are the closest Wikipedia paragraphs:<br><br>${list}`,\n} }];"
      },
      "id": "6f1d2a3e-0013-4a51-9b1e-5c1f0e6a0013",
      "name": "Format closest paragraphs",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1400,
        380
      ]
    },
    {
      "parameters": {
        "operation": "completion",
        "respondWith": "text",
        "completionTitle": "={{ $json.title }}",
        "completionMessage": "={{ $json.message }}"
      },
      "id": "6f1d2a3e-0006-4a51-9b1e-5c1f0e6a0006",
      "name": "Show the answer",
      "type": "n8n-nodes-base.form",
      "typeVersion": 1,
      "position": [
        1440,
        -120
      ],
      "webhookId": "6f1d2a3e-0007-4a51-9b1e-5c1f0e6a0007"
    },
    {
      "parameters": {
        "operation": "completion",
        "respondWith": "text",
        "completionTitle": "={{ $json.title }}",
        "completionMessage": "={{ $json.message }}"
      },
      "id": "6f1d2a3e-0014-4a51-9b1e-5c1f0e6a0014",
      "name": "Show what to fix",
      "type": "n8n-nodes-base.form",
      "typeVersion": 1,
      "position": [
        1440,
        160
      ],
      "webhookId": "6f1d2a3e-0015-4a51-9b1e-5c1f0e6a0015"
    },
    {
      "parameters": {
        "operation": "completion",
        "respondWith": "text",
        "completionTitle": "={{ $json.title }}",
        "completionMessage": "={{ $json.message }}"
      },
      "id": "6f1d2a3e-0016-4a51-9b1e-5c1f0e6a0016",
      "name": "Show closest paragraphs",
      "type": "n8n-nodes-base.form",
      "typeVersion": 1,
      "position": [
        1640,
        380
      ],
      "webhookId": "6f1d2a3e-0017-4a51-9b1e-5c1f0e6a0017"
    }
  ],
  "connections": {
    "Ask a question": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Settings": {
      "main": [
        [
          {
            "node": "Ask WikiVectors",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Ask WikiVectors": {
      "main": [
        [
          {
            "node": "Answered?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Answered?": {
      "main": [
        [
          {
            "node": "Format answer and sources",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Call failed?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Call failed?": {
      "main": [
        [
          {
            "node": "Explain the error",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Find closest paragraphs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find closest paragraphs": {
      "main": [
        [
          {
            "node": "Format closest paragraphs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format answer and sources": {
      "main": [
        [
          {
            "node": "Show the answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Explain the error": {
      "main": [
        [
          {
            "node": "Show what to fix",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format closest paragraphs": {
      "main": [
        [
          {
            "node": "Show closest paragraphs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {},
  "meta": {
    "templateCredsSetupCompleted": false
  }
}
