{
  "name": "Gookit: three barcode labels to PDF",
  "nodes": [
    {
      "id": "start",
      "name": "Start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "parameters": {}
    },
    {
      "id": "input",
      "name": "Input",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ],
      "parameters": {
        "jsCode": "return [{json:{templateId:\"REPLACE_WITH_YOUR_TEMPLATE_ID\", startedAt:Date.now(), items:[{quantity:2,data:{name:\"Trail Bottle\",sku:\"TB-1042\"}},{quantity:1,data:{name:\"Desk Tray\",sku:\"DT-2006\"}}]}}];"
      }
    },
    {
      "id": "submit",
      "name": "Submit",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        0
      ],
      "parameters": {
        "method": "POST",
        "url": "https://label-api.gookit.co/api/v1/print",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({templateId:$json.templateId,items:$json.items}) }}",
        "options": {
          "timeout": 30000
        }
      }
    },
    {
      "id": "status",
      "name": "Status",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        660,
        0
      ],
      "parameters": {
        "url": "={{ 'https://label-api.gookit.co/api/v1/print/' + encodeURIComponent($('Submit').first().json.jobId) }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {
          "timeout": 30000,
          "response": {
            "response": {
              "fullResponse": true,
              "neverError": true,
              "responseFormat": "json"
            }
          }
        }
      }
    },
    {
      "id": "decide",
      "name": "Decide",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        880,
        0
      ],
      "parameters": {
        "jsCode": "const r=$input.first().json;\nconst deadline=$('Input').first().json.startedAt+120000;\nlet seconds=2;\nif(r.statusCode===429){seconds=Number(r.headers['retry-after'] ?? 2);if(!Number.isFinite(seconds)||seconds<0)throw new Error('Invalid Retry-After');seconds=Math.max(1,seconds);}\nelse {\n if(r.statusCode!==200)throw new Error('Status HTTP '+r.statusCode+'; keep the Submit jobId.');\n if(r.body.status==='completed'){\n  if(typeof r.body.url!=='string'||!r.body.url.startsWith('https://'))throw new Error('Missing HTTPS PDF URL');\n  return [{json:{done:true,url:r.body.url}}];\n }\n if(!['pending','processing'].includes(r.body.status))throw new Error('Job failed or unknown status; keep the Submit jobId.');\n}\nif(Date.now()+seconds*1000>=deadline)throw new Error('Polling deadline. Check the existing job; do not rerun Submit.');\nreturn [{json:{done:false,waitSeconds:seconds}}];"
      }
    },
    {
      "id": "completed",
      "name": "Completed",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [
        1100,
        0
      ],
      "parameters": {
        "conditions": {
          "boolean": [
            {
              "value1": "={{ $json.done }}",
              "value2": true
            }
          ]
        }
      }
    },
    {
      "id": "download",
      "name": "Download",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1320,
        -120
      ],
      "parameters": {
        "url": "={{ $json.url }}",
        "options": {
          "timeout": 60000,
          "response": {
            "response": {
              "responseFormat": "file",
              "outputPropertyName": "data"
            }
          }
        }
      }
    },
    {
      "id": "wait",
      "name": "Wait",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        1320,
        120
      ],
      "parameters": {
        "amount": "={{ $json.waitSeconds }}",
        "unit": "seconds"
      }
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Input",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Input": {
      "main": [
        [
          {
            "node": "Submit",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Submit": {
      "main": [
        [
          {
            "node": "Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Status": {
      "main": [
        [
          {
            "node": "Decide",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Decide": {
      "main": [
        [
          {
            "node": "Completed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Completed": {
      "main": [
        [
          {
            "node": "Download",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait": {
      "main": [
        [
          {
            "node": "Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1",
    "executionTimeout": 180
  },
  "pinData": {}
}
