{
  "name": "Kit Rx | Bank statement PDF and balance checks",
  "active": false,
  "nodes": [
    {
      "id": "example-trigger",
      "name": "Run fictional example",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "parameters": {}
    },
    {
      "id": "sample",
      "name": "Fictional response",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        240,
        0
      ],
      "parameters": {
        "jsCode": "return [{ json: { example: true, data: \"{\\\"type\\\":\\\"started\\\"}\\n{\\\"type\\\":\\\"result\\\",\\\"ok\\\":true,\\\"pages\\\":1,\\\"statement\\\":{\\\"institution\\\":\\\"Example Credit Union\\\",\\\"account_type\\\":\\\"checking\\\",\\\"account_last4\\\":\\\"0000\\\",\\\"currency\\\":\\\"USD\\\",\\\"period_start\\\":\\\"2026-01-01\\\",\\\"period_end\\\":\\\"2026-01-31\\\",\\\"opening_balance\\\":100,\\\"closing_balance\\\":125,\\\"printed_total_credits\\\":25,\\\"printed_total_debits\\\":0},\\\"transactions\\\":[{\\\"date\\\":\\\"2026-01-15\\\",\\\"description\\\":\\\"Fictional deposit\\\",\\\"amount\\\":25,\\\"balance\\\":125,\\\"page\\\":1}],\\\"unparsed_rows\\\":0,\\\"reconciliation\\\":{\\\"status\\\":\\\"balanced\\\",\\\"liability\\\":false,\\\"count\\\":1,\\\"opening\\\":100,\\\"opening_inferred\\\":false,\\\"closing\\\":125,\\\"total_credits\\\":25,\\\"total_debits\\\":0,\\\"net\\\":25,\\\"expected_closing\\\":125,\\\"difference\\\":0,\\\"printed_total_credits\\\":25,\\\"printed_total_debits\\\":0,\\\"totals\\\":{\\\"credits_match\\\":true,\\\"debits_match\\\":true},\\\"running_balance_checked\\\":1,\\\"row_errors\\\":[]}}\" } }];"
      }
    },
    {
      "id": "receive",
      "name": "Receive one PDF",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.1,
      "position": [
        0,
        260
      ],
      "parameters": {
        "inputSource": "passthrough"
      }
    },
    {
      "id": "check-file",
      "name": "Check one PDF",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        240,
        260
      ],
      "parameters": {
        "jsCode": "const items = $input.all();\nif (items.length !== 1) throw new Error(\"Pass exactly one statement per call.\");\nconst file = items[0].binary?.data;\nif (!file || (file.mimeType !== \"application/pdf\" && !/\\.pdf$/i.test(file.fileName || \"\"))) {\n  throw new Error(\"Supply the PDF in the binary field named data.\");\n}\nreturn [{ json: {}, binary: { data: file }, pairedItem: { item: 0 } }];"
      }
    },
    {
      "id": "convert",
      "name": "Convert paid PDF",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        480,
        260
      ],
      "retryOnFail": false,
      "parameters": {
        "method": "POST",
        "url": "https://statements.kit-rx.com/api/convert",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-Workflow",
              "value": "n8n"
            }
          ]
        },
        "sendBody": true,
        "contentType": "binaryData",
        "inputDataFieldName": "data",
        "options": {
          "timeout": 600000,
          "redirect": {
            "redirect": {
              "followRedirects": false
            }
          },
          "response": {
            "response": {
              "responseFormat": "text",
              "outputPropertyName": "data"
            }
          }
        }
      }
    },
    {
      "id": "read-result",
      "name": "Read statement and checks",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        740,
        100
      ],
      "parameters": {
        "jsCode": "function readConversion(text, example) {\n  if (typeof text !== \"string\") throw new Error(\"The response is missing. Check your Kit Rx balance before retrying.\");\n  const lines = text.trim().split(\"\\n\").filter((line) => line.trim());\n  let result;\n  try { result = JSON.parse(lines.at(-1)); }\n  catch { throw new Error(\"The response ended before the result arrived. Check your Kit Rx balance before retrying.\"); }\n  if (result?.type === \"error\") throw new Error(result.message || \"Kit Rx could not convert this file.\");\n  const check = result?.reconciliation;\n  if (result?.type !== \"result\" || result.ok !== true || !result.statement || !Array.isArray(result.transactions) || !check) {\n    throw new Error(\"The response has no final statement. Check your Kit Rx balance before retrying.\");\n  }\n\n  const reasons = [];\n  if (check.status !== \"balanced\") reasons.push(\"The printed figures did not verify the total.\");\n  if (check.opening_inferred) reasons.push(\"The opening balance was inferred from a transaction.\");\n  if (check.totals?.credits_match === false) reasons.push(\"Printed credits differ from the extracted credits.\");\n  if (check.totals?.debits_match === false) reasons.push(\"Printed debits differ from the extracted debits.\");\n  if (!Array.isArray(check.row_errors) || check.row_errors.length) reasons.push(\"Review the running balances.\");\n  if (result.unparsed_rows !== 0) reasons.push(\"Review the statement for unread rows.\");\n  if (!result.transactions.length) reasons.push(\"No transaction rows were extracted.\");\n\n  return {\n    example,\n    review_required: reasons.length > 0,\n    review_reasons: reasons,\n    statement: result.statement,\n    transactions: result.transactions,\n    reconciliation: check,\n    unparsed_rows: result.unparsed_rows,\n    pages: result.pages,\n    pages_balance: result.remaining?.pages_balance ?? null,\n  };\n}\n\nreturn $input.all().map((item, index) => ({ json: readConversion(item.json.data, item.json.example === true), pairedItem: { item: index } }));"
      }
    }
  ],
  "connections": {
    "Run fictional example": {
      "main": [
        [
          {
            "node": "Fictional response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fictional response": {
      "main": [
        [
          {
            "node": "Read statement and checks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Receive one PDF": {
      "main": [
        [
          {
            "node": "Check one PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check one PDF": {
      "main": [
        [
          {
            "node": "Convert paid PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert paid PDF": {
      "main": [
        [
          {
            "node": "Read statement and checks",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1",
    "callerPolicy": "workflowsFromSameOwner",
    "saveDataErrorExecution": "none",
    "saveDataSuccessExecution": "none",
    "saveManualExecutions": false,
    "saveExecutionProgress": false
  }
}
