{
  "id": "doqlo-downstream-file-handling",
  "name": "Doqlo Bulk Fill - Downstream File Handling",
  "nodes": [
    {
      "parameters": {},
      "id": "manual-trigger",
      "name": "Manual Trigger",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        260,
        300
      ]
    },
    {
      "parameters": {
        "mode": "manual",
        "fields": {
          "values": [
            {
              "name": "api_base_url",
              "type": "stringValue",
              "stringValue": "https://api.doqlo.com"
            },
            {
              "name": "bf_export_api_key",
              "type": "stringValue",
              "stringValue": "replace-with-your-bf-export-api-key"
            },
            {
              "name": "source_pdf_url",
              "type": "stringValue",
              "stringValue": "https://example.com/source.pdf"
            },
            {
              "name": "doqlo_url",
              "type": "stringValue",
              "stringValue": "https://example.com/template.doqlo"
            },
            {
              "name": "rows_json",
              "type": "stringValue",
              "stringValue": "[{\"column_0\":\"Alice Example\"}]"
            },
            {
              "name": "max_failed_row_percent",
              "type": "stringValue",
              "stringValue": "0"
            },
            {
              "name": "flatten_forms",
              "type": "stringValue",
              "stringValue": "false"
            },
            {
              "name": "include_stickers",
              "type": "stringValue",
              "stringValue": "false"
            },
            {
              "name": "downstream_url",
              "type": "stringValue",
              "stringValue": "https://example.com/downstream-endpoint"
            },
            {
              "name": "poll_max_attempts",
              "type": "stringValue",
              "stringValue": "12"
            }
          ]
        },
        "include": "all",
        "options": {}
      },
      "id": "set-example-downstream-data",
      "name": "Set Example Downstream Data",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.2,
      "position": [
        520,
        300
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "={{$node[\"Set Example Downstream Data\"].json[\"source_pdf_url\"]}}",
        "authentication": "none",
        "options": {
          "response": {
            "responseFormat": "file",
            "outputPropertyName": "pdf"
          }
        }
      },
      "id": "download-source-pdf",
      "name": "Download Source PDF",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        760,
        180
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "={{$node[\"Set Example Downstream Data\"].json[\"doqlo_url\"]}}",
        "authentication": "none",
        "options": {
          "response": {
            "responseFormat": "file",
            "outputPropertyName": "doqlo_file"
          }
        }
      },
      "id": "download-doqlo-package",
      "name": "Download Doqlo Package",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        760,
        420
      ]
    },
    {
      "parameters": {
        "mode": "combine",
        "combineBy": "combineByPosition",
        "numberInputs": 2,
        "options": {}
      },
      "id": "merge-job-inputs",
      "name": "Merge Job Inputs",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3.2,
      "position": [
        1000,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$node[\"Set Example Downstream Data\"].json[\"api_base_url\"] + \"/v1/bulkfill/export-jobs\"}}",
        "authentication": "none",
        "sendHeaders": true,
        "specifyHeaders": "keypair",
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "={{\"Bearer \" + $node[\"Set Example Downstream Data\"].json[\"bf_export_api_key\"]}}"
            },
            {
              "name": "Idempotency-Key",
              "value": "={{\"doqlo-downstream-\" + $now.toMillis()}}"
            }
          ]
        },
        "sendBody": true,
        "contentType": "multipart-form-data",
        "bodyParameters": {
          "parameters": [
            {
              "parameterType": "formBinaryData",
              "name": "pdf",
              "inputDataFieldName": "pdf"
            },
            {
              "parameterType": "formBinaryData",
              "name": "doqlo_file",
              "inputDataFieldName": "doqlo_file"
            },
            {
              "parameterType": "formData",
              "name": "rows_json",
              "value": "={{$node[\"Set Example Downstream Data\"].json[\"rows_json\"]}}"
            },
            {
              "parameterType": "formData",
              "name": "max_failed_row_percent",
              "value": "={{$node[\"Set Example Downstream Data\"].json[\"max_failed_row_percent\"]}}"
            },
            {
              "parameterType": "formData",
              "name": "flatten_forms",
              "value": "={{$node[\"Set Example Downstream Data\"].json[\"flatten_forms\"]}}"
            },
            {
              "parameterType": "formData",
              "name": "include_stickers",
              "value": "={{$node[\"Set Example Downstream Data\"].json[\"include_stickers\"]}}"
            }
          ]
        },
        "options": {
          "response": {
            "responseFormat": "json"
          }
        }
      },
      "id": "create-export-job",
      "name": "Create Export Job",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        1240,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"status\"] || \"\"}}",
              "operation": "equal",
              "value2": "completed"
            }
          ]
        }
      },
      "id": "create-returned-completed",
      "name": "Create Returned Completed?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1480,
        180
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"status\"] || \"\"}}",
              "operation": "equal",
              "value2": "failed"
            }
          ]
        }
      },
      "id": "create-returned-failed",
      "name": "Create Returned Failed?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1480,
        420
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const base = $input.first().json;\nconst maxAttempts = Number($node[\"Set Example Downstream Data\"].json[\"poll_max_attempts\"] || 12);\nreturn Array.from({ length: maxAttempts }, (_, index) => ({\n  json: {\n    job_id: base.job_id,\n    attempt_number: index + 1,\n    max_attempts: maxAttempts\n  }\n}));"
      },
      "id": "prepare-poll-attempts",
      "name": "Prepare Poll Attempts",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1720,
        300
      ]
    },
    {
      "parameters": {
        "batchSize": 1,
        "options": {}
      },
      "id": "loop-over-poll-attempts",
      "name": "Loop Over Poll Attempts",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        1960,
        300
      ]
    },
    {
      "parameters": {
        "resume": "timeInterval",
        "amount": 10,
        "unit": "seconds"
      },
      "id": "wait-before-poll",
      "name": "Wait Before Poll",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        2200,
        300
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "={{$node[\"Set Example Downstream Data\"].json[\"api_base_url\"] + \"/v1/bulkfill/export-jobs/\" + $json[\"job_id\"]}}",
        "authentication": "none",
        "sendHeaders": true,
        "specifyHeaders": "keypair",
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "={{\"Bearer \" + $node[\"Set Example Downstream Data\"].json[\"bf_export_api_key\"]}}"
            }
          ]
        },
        "options": {
          "response": {
            "responseFormat": "json"
          }
        }
      },
      "id": "get-export-job",
      "name": "Get Export Job",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        2440,
        300
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"status\"] || \"\"}}",
              "operation": "equal",
              "value2": "completed"
            }
          ]
        }
      },
      "id": "is-job-completed",
      "name": "Is Job Completed?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        2680,
        260
      ]
    },
    {
      "parameters": {
        "conditions": {
          "string": [
            {
              "value1": "={{$json[\"status\"] || \"\"}}",
              "operation": "equal",
              "value2": "failed"
            }
          ]
        }
      },
      "id": "is-job-failed",
      "name": "Is Job Failed?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        2680,
        420
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "={{$node[\"Set Example Downstream Data\"].json[\"api_base_url\"] + \"/v1/bulkfill/export-jobs/\" + $json[\"job_id\"] + \"/download\"}}",
        "authentication": "none",
        "sendHeaders": true,
        "specifyHeaders": "keypair",
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "={{\"Bearer \" + $node[\"Set Example Downstream Data\"].json[\"bf_export_api_key\"]}}"
            }
          ]
        },
        "options": {
          "response": {
            "responseFormat": "file",
            "outputPropertyName": "export_file"
          }
        }
      },
      "id": "download-completed-export",
      "name": "Download Completed Export",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        2920,
        220
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{$node[\"Set Example Downstream Data\"].json[\"downstream_url\"]}}",
        "authentication": "none",
        "sendHeaders": true,
        "specifyHeaders": "keypair",
        "headerParameters": {
          "parameters": [
            {
              "name": "X-Doqlo-Job-Id",
              "value": "={{$node[\"Get Export Job\"].json[\"job_id\"] || $node[\"Create Export Job\"].json[\"job_id\"] || \"\"}}"
            }
          ]
        },
        "sendBody": true,
        "contentType": "binaryData",
        "inputDataFieldName": "export_file",
        "options": {
          "response": {
            "responseFormat": "json"
          }
        }
      },
      "id": "send-output-to-downstream-endpoint",
      "name": "Send Output To Downstream Endpoint",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.4,
      "position": [
        3160,
        220
      ]
    },
    {
      "parameters": {
        "mode": "manual",
        "fields": {
          "values": [
            {
              "name": "status",
              "type": "stringValue",
              "stringValue": "completed"
            },
            {
              "name": "downstream_status",
              "type": "stringValue",
              "stringValue": "sent"
            },
            {
              "name": "job_id",
              "type": "stringValue",
              "stringValue": "={{$node[\"Get Export Job\"].json[\"job_id\"] || $node[\"Create Export Job\"].json[\"job_id\"] || \"\"}}"
            }
          ]
        },
        "include": "all",
        "options": {}
      },
      "id": "output-ready",
      "name": "Output Ready",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.2,
      "position": [
        3400,
        220
      ]
    },
    {
      "parameters": {
        "mode": "manual",
        "fields": {
          "values": [
            {
              "name": "status",
              "type": "stringValue",
              "stringValue": "failed"
            },
            {
              "name": "job_id",
              "type": "stringValue",
              "stringValue": "={{$json[\"job_id\"] || \"\"}}"
            },
            {
              "name": "error_code",
              "type": "stringValue",
              "stringValue": "={{($json[\"error\"] && $json[\"error\"][\"code\"]) || \"\"}}"
            },
            {
              "name": "error_message",
              "type": "stringValue",
              "stringValue": "={{($json[\"error\"] && $json[\"error\"][\"message\"]) || \"\"}}"
            }
          ]
        },
        "include": "all",
        "options": {}
      },
      "id": "job-failed",
      "name": "Job Failed",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.2,
      "position": [
        2920,
        520
      ]
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "language": "javaScript",
        "jsCode": "const items = $input.all();\nconst last = items.length ? items[items.length - 1].json : {};\nconst maxAttempts = Number($node[\"Set Example Downstream Data\"].json[\"poll_max_attempts\"] || 12);\nreturn [{\n  json: {\n    status: last.status || 'processing',\n    job_id: last.job_id || $node[\"Create Export Job\"].json.job_id || '',\n    message: `The starter workflow waited ${maxAttempts} times at 10-second intervals. Check the job later or switch to webhook-based handling for longer runs.`\n  }\n}];"
      },
      "id": "check-later",
      "name": "Check Later",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2200,
        520
      ]
    }
  ],
  "connections": {
    "Manual Trigger": {
      "main": [
        [
          {
            "node": "Set Example Downstream Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set Example Downstream Data": {
      "main": [
        [
          {
            "node": "Download Source PDF",
            "type": "main",
            "index": 0
          },
          {
            "node": "Download Doqlo Package",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Source PDF": {
      "main": [
        [
          {
            "node": "Merge Job Inputs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Doqlo Package": {
      "main": [
        [
          {
            "node": "Merge Job Inputs",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge Job Inputs": {
      "main": [
        [
          {
            "node": "Create Export Job",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Export Job": {
      "main": [
        [
          {
            "node": "Create Returned Completed?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Returned Completed?": {
      "main": [
        [
          {
            "node": "Download Completed Export",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create Returned Failed?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Returned Failed?": {
      "main": [
        [
          {
            "node": "Job Failed",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Prepare Poll Attempts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Poll Attempts": {
      "main": [
        [
          {
            "node": "Loop Over Poll Attempts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Poll Attempts": {
      "main": [
        [
          {
            "node": "Check Later",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Wait Before Poll",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait Before Poll": {
      "main": [
        [
          {
            "node": "Get Export Job",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Export Job": {
      "main": [
        [
          {
            "node": "Is Job Completed?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Job Completed?": {
      "main": [
        [
          {
            "node": "Download Completed Export",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Is Job Failed?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Is Job Failed?": {
      "main": [
        [
          {
            "node": "Job Failed",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Loop Over Poll Attempts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download Completed Export": {
      "main": [
        [
          {
            "node": "Send Output To Downstream Endpoint",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Send Output To Downstream Endpoint": {
      "main": [
        [
          {
            "node": "Output Ready",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "8e4b7f87-95c0-4e77-b6bc-f84c7ce39c72",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "tags": []
}
