{
  "name": "DocKiln order PDF to email",
  "active": false,
  "nodes": [
    {
      "name": "Order form",
      "type": "n8n-nodes-base.formTrigger",
      "typeVersion": 2.2,
      "parameters": {
        "formTitle": "Generate order PDF",
        "formFields": {
          "values": [
            {
              "fieldLabel": "Customer",
              "requiredField": true
            },
            {
              "fieldLabel": "Order reference",
              "requiredField": true
            }
          ]
        },
        "options": {}
      },
      "position": [
        0,
        0
      ],
      "id": "91063ce0-3e2c-4458-a263-559bce332e20",
      "webhookId": "68e41752-f5e3-4bd2-966b-893822195113"
    },
    {
      "name": "Configuration",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "d2f30a1e-d92b-4efa-b9b5-183ff7f40e05",
              "name": "origin",
              "value": "https://your-dockiln.example",
              "type": "string"
            },
            {
              "id": "9f34f12e-8bc4-4748-81c9-17545ccfe37d",
              "name": "template",
              "value": "YOUR_PUBLISHED_TEMPLATE_ID",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "position": [
        220,
        0
      ],
      "id": "a0507ee0-9835-499e-a4e9-e53d979749fc"
    },
    {
      "name": "Queue document",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "parameters": {
        "url": "={{ $json.origin + \"/v1/documents\" }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {},
        "method": "POST",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ templateId: $json.template, data: { recipient: { name: $json.Customer }, items: [{ field1: $json[\"Order reference\"], field2: \"1\", field3: \"Confirmed\" }] } }) }}",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Idempotency-Key",
              "value": "={{ \"n8n-\" + $execution.id }}"
            }
          ]
        }
      },
      "position": [
        440,
        0
      ],
      "id": "63f4c339-0fe7-48c1-b4e2-2f01b968b93d",
      "credentials": {
        "httpHeaderAuth": {
          "name": "Configure httpHeaderAuth locally"
        }
      }
    },
    {
      "name": "Pause",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "parameters": {
        "amount": 3,
        "unit": "seconds"
      },
      "position": [
        660,
        0
      ],
      "id": "131cf33a-0153-42de-a694-4a0f1fe98d77",
      "webhookId": "36fa6779-942d-4aaa-a070-2727b23c5a3c"
    },
    {
      "name": "Read status",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "parameters": {
        "url": "={{ $(\"Configuration\").first().json.origin + \"/v1/documents/\" + $(\"Queue document\").first().json.id }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {}
      },
      "position": [
        880,
        0
      ],
      "id": "e4fba550-6aec-4a6c-b1c7-bb5047e8a1e0",
      "credentials": {
        "httpHeaderAuth": {
          "name": "Configure httpHeaderAuth locally"
        }
      }
    },
    {
      "name": "Check outcome",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "parameters": {
        "jsCode": "const job=$input.first().json;\nif(job.status===\"failed\") throw Error(job.error);\nif(Date.now()-job.created>120000) throw Error(\"Poll window expired: \"+job.id);\nreturn [{json:job}];"
      },
      "position": [
        1100,
        0
      ],
      "id": "9c39adb9-44d0-4ff7-83ef-70b1f5db35d9"
    },
    {
      "name": "Completed",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "typeValidation": "strict",
            "version": 2
          },
          "conditions": [
            {
              "id": "725acdab-32d9-47d3-87eb-286774a41ad3",
              "leftValue": "={{ $json.status }}",
              "rightValue": "completed",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "position": [
        1320,
        0
      ],
      "id": "917bae62-acb8-4519-876e-b7a7bc660af3"
    },
    {
      "name": "Download PDF",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "parameters": {
        "url": "={{ $(\"Configuration\").first().json.origin + $json.download }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {
          "response": {
            "response": {
              "responseFormat": "file",
              "outputPropertyName": "data"
            }
          }
        }
      },
      "position": [
        1540,
        0
      ],
      "id": "a87be091-40ff-4066-a503-2ea5fa3ddd4a",
      "credentials": {
        "httpHeaderAuth": {
          "name": "Configure httpHeaderAuth locally"
        }
      }
    },
    {
      "name": "Email document",
      "type": "n8n-nodes-base.emailSend",
      "typeVersion": 2.1,
      "parameters": {
        "fromEmail": "sender@example.invalid",
        "toEmail": "reviewer@example.invalid",
        "subject": "Order document",
        "emailFormat": "text",
        "text": "The generated order document is attached.",
        "options": {
          "attachments": "data"
        }
      },
      "position": [
        1760,
        0
      ],
      "id": "281130b2-1a6d-4ceb-9794-25b3d176fcca",
      "credentials": {
        "smtp": {
          "name": "Configure smtp locally"
        }
      }
    }
  ],
  "connections": {
    "Order form": {
      "main": [
        [
          {
            "node": "Configuration",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Configuration": {
      "main": [
        [
          {
            "node": "Queue document",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Queue document": {
      "main": [
        [
          {
            "node": "Pause",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pause": {
      "main": [
        [
          {
            "node": "Read status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read status": {
      "main": [
        [
          {
            "node": "Check outcome",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check outcome": {
      "main": [
        [
          {
            "node": "Completed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Completed": {
      "main": [
        [
          {
            "node": "Download PDF",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Pause",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download PDF": {
      "main": [
        [
          {
            "node": "Email document",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "6827712f-8343-4112-a6ae-86df7141567f"
}
