{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "075c5626-b67e-4d21-9c03-2864d6d279d9",
      "name": "Wait",
      "type": "n8n-nodes-base.wait",
      "position": [
        -320,
        176
      ],
      "parameters": {
        "amount": 60
      },
      "typeVersion": 1.1
    },
    {
      "id": "81367e44-cb1e-4e50-bb57-22d8583ef118",
      "name": "If",
      "type": "n8n-nodes-base.if",
      "position": [
        -992,
        176
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "8a860859-613f-4295-96c6-c2ec920ec520",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              },
              "leftValue": "={{ $json.body.data.send_for_generation }}",
              "rightValue": ""
            },
            {
              "id": "8069790d-2dd5-4bdc-83b5-0f8e19d15631",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json.body.data.prompt }}",
              "rightValue": ""
            },
            {
              "id": "1cf4e44c-3bfd-491f-bc72-6e309c9a5f1a",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json.body.data.resolution }}",
              "rightValue": ""
            },
            {
              "id": "0f90c20f-37e5-49de-a5af-e6515e0b337d",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json.body.data.aspectRatio }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "265be212-8981-4929-8a3f-6fd5a6269ad3",
      "name": "Data collection",
      "type": "n8n-nodes-base.set",
      "position": [
        -768,
        176
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "17954335-b96b-4813-9c4a-20817d675448",
              "name": "api_endpoint",
              "type": "string",
              "value": "us-central1-aiplatform.googleapis.com"
            },
            {
              "id": "5cedc3de-6221-4d7e-a6c8-82f4cb9cf0e9",
              "name": "project_id",
              "type": "string",
              "value": "your_cloud_console_project_ID"
            },
            {
              "id": "90019751-e2d5-4764-9bf9-e13916dcc528",
              "name": "veo_3_model",
              "type": "string",
              "value": "veo-3.0-generate-preview"
            },
            {
              "id": "67ab205c-82b3-4263-99c4-c906a0ca6ae9",
              "name": "region",
              "type": "string",
              "value": "us-central1"
            },
            {
              "id": "413415fb-60c5-4d0d-ac45-1e6178a55227",
              "name": "prompt",
              "type": "string",
              "value": "={{ $json.body.data.prompt }}"
            },
            {
              "id": "bf0910e1-b757-4852-9341-a7792161f89b",
              "name": "vertex_api",
              "type": "string",
              "value": "Vertex-AI-API"
            },
            {
              "id": "e307a6ab-c416-4e1d-9659-6c5c29a66c5c",
              "name": "resolution",
              "type": "string",
              "value": "={{ $json.body.data.resolution }}"
            },
            {
              "id": "bd5af97c-1caf-477a-b6e8-de5b4fbe3867",
              "name": "aspectRatio",
              "type": "string",
              "value": "={{ $json.body.data.aspectRatio }}"
            },
            {
              "id": "01dab59a-5a22-4a98-9b16-e5794b70b4a3",
              "name": "durationSeconds",
              "type": "number",
              "value": "={{ $json.body.data.durationSeconds }}"
            },
            {
              "id": "d9cbd980-fdd8-4129-a9dd-4a8112afcb17",
              "name": "row",
              "type": "number",
              "value": "={{ $json.body.row }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "68465f8b-bcbf-490a-a4af-85f60b0f5272",
      "name": "Vertex AI Send for Generation",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -544,
        176
      ],
      "parameters": {
        "url": "=https://{{ $json.api_endpoint }}/v1/projects/{{ $json.project_id }}/locations/{{ $json.region }}/publishers/google/models/{{ $json.veo_3_model }}:predictLongRunning",
        "method": "POST",
        "options": {},
        "jsonBody": "={\n  \"instances\": [\n    {\n      \"prompt\": {{ JSON.stringify($json.prompt) }}\n    }\n  ],\n  \"parameters\": {\n    \"aspectRatio\": \"{{ $json.aspectRatio }}\",\n    \"compressionQuality\": \"optimized\",\n    \"durationSeconds\": {{ $json.durationSeconds }},\n    \"generateAudio\": true,\n    \"resolution\": \"{{ $json.resolution }}\",\n    \"sampleCount\": 1,\n    \"personGeneration\": \"allow_adult\"\n  }\n}",
        "sendBody": true,
        "sendQuery": true,
        "specifyBody": "json",
        "queryParameters": {
          "parameters": [
            {
              "name": "key",
              "value": "={{ $json.vertex_api }}"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "ecf60740-7105-4253-aca8-23dfa28ea688",
      "name": "Fetch/Check Video",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -96,
        176
      ],
      "parameters": {
        "url": "=https://{{ $('Data collection').item.json.api_endpoint }}/v1/projects/{{ $('Data collection').item.json.project_id }}/locations/{{ $('Data collection').item.json.region }}/publishers/google/models/{{ $('Data collection').item.json.veo_3_model }}:fetchPredictOperation\n",
        "method": "POST",
        "options": {
          "response": {
            "response": {
              "responseFormat": "json"
            }
          }
        },
        "jsonBody": "={\n  \"operationName\": \"{{ $json.name }}\"\n}\n",
        "sendBody": true,
        "sendQuery": true,
        "specifyBody": "json",
        "queryParameters": {
          "parameters": [
            {
              "name": "key",
              "value": "={{ $('Data collection').item.json.vertex_api }}"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "5d435d6e-41ac-4a55-9b3b-7edb7922476e",
      "name": "Wait before next video check",
      "type": "n8n-nodes-base.wait",
      "position": [
        544,
        336
      ],
      "parameters": {
        "amount": 60
      },
      "typeVersion": 1.1
    },
    {
      "id": "ddc76d39-64d5-4506-afb4-1eda4c3849cb",
      "name": "If Video Is Created",
      "type": "n8n-nodes-base.if",
      "position": [
        128,
        96
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "94587ad8-ed9b-4d04-98c9-54389e20f5d8",
              "operator": {
                "type": "number",
                "operation": "equals"
              },
              "leftValue": "={{ $json.response.raiMediaFilteredCount }}",
              "rightValue": 0
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "f7254f1a-4493-4491-9621-bc97fbd1e471",
      "name": "Convert to Binary File",
      "type": "n8n-nodes-base.convertToFile",
      "position": [
        464,
        -256
      ],
      "parameters": {
        "options": {},
        "operation": "toBinary",
        "sourceProperty": "response.videos[0].bytesBase64Encoded"
      },
      "typeVersion": 1.1
    },
    {
      "id": "f45ffa9a-3b9c-4f19-a57f-06f644113c40",
      "name": "Upload Video to Drive",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        656,
        -256
      ],
      "parameters": {
        "name": "={{$now.format('yyyy-MM-dd hh:mm:ss')}}",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "options": {},
        "folderId": {
          "__rl": true,
          "mode": "list",
          "value": "1zcpqSwOmiheqWowes-kQ6mSPPmUiuSoZ",
          "cachedResultUrl": "https://drive.google.com/drive/folders/1zcpqSwOmiheqWowes-kQ6mSPPmUiuSoZ",
          "cachedResultName": "veo"
        }
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "593c1893-9467-4fbd-818e-f9fdfa68e1fa",
      "name": "Google Sheet Trigger",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -1216,
        176
      ],
      "parameters": {
        "path": "499c5cd9-7fd6-4ddf-829f-3a0eba4b4460",
        "options": {},
        "httpMethod": "POST"
      },
      "typeVersion": 2.1
    },
    {
      "id": "8e3976bb-9432-48ec-8d52-041a42566cfa",
      "name": "Update video Link in sheet",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1088,
        -16
      ],
      "parameters": {
        "columns": {
          "value": {
            "row_number": "={{ $('Data collection').item.json.row }}",
            "video_drive_link": "={{ $json.webViewLink }}"
          },
          "schema": [
            {
              "id": "prompt",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "prompt",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "resolution",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "resolution",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "aspectRatio",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "aspectRatio",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "durationSeconds",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "durationSeconds",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "send_for_generation",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "send_for_generation",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "video_drive_link",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "video_drive_link",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "row_number",
              "type": "number",
              "display": true,
              "removed": false,
              "readOnly": true,
              "required": false,
              "displayName": "row_number",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "row_number"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "update",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1E3ujHxOFXvokfDnEMK7wcxy21F3YZgKLL_rAIvcZrkI/edit#gid=0",
          "cachedResultName": "VEO_3"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1E3ujHxOFXvokfDnEMK7wcxy21F3YZgKLL_rAIvcZrkI",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1E3ujHxOFXvokfDnEMK7wcxy21F3YZgKLL_rAIvcZrkI/edit?usp=drivesdk",
          "cachedResultName": "google_veo_3_bulk_video_generation_template"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "d2bf3899-b4eb-4b81-a5dc-9c8d3340d225",
      "name": "If Video Generation Eror",
      "type": "n8n-nodes-base.if",
      "position": [
        352,
        192
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "or",
          "conditions": [
            {
              "id": "94587ad8-ed9b-4d04-98c9-54389e20f5d8",
              "operator": {
                "type": "number",
                "operation": "gt"
              },
              "leftValue": "={{ $json.response.raiMediaFilteredCount }}",
              "rightValue": 0
            },
            {
              "id": "367d37fe-842a-4252-8fac-0f21614fdbd7",
              "operator": {
                "type": "object",
                "operation": "exists",
                "singleValue": true
              },
              "leftValue": "={{ $json.error }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "fc15ddb8-662d-4df6-981b-0f7ab33e0dff",
      "name": "Update Eror in Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        992,
        176
      ],
      "parameters": {
        "columns": {
          "value": {
            "row_number": "={{ $('Data collection').item.json.row }}",
            "video_drive_link": "={{ $json.response?.raiMediaFilteredReasons ?? $json.error }}"
          },
          "schema": [
            {
              "id": "prompt",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "prompt",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "resolution",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "resolution",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "aspectRatio",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "aspectRatio",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "durationSeconds",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "durationSeconds",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "send_for_generation",
              "type": "string",
              "display": true,
              "removed": true,
              "required": false,
              "displayName": "send_for_generation",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "video_drive_link",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "video_drive_link",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "row_number",
              "type": "number",
              "display": true,
              "removed": false,
              "readOnly": true,
              "required": false,
              "displayName": "row_number",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "row_number"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "update",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1E3ujHxOFXvokfDnEMK7wcxy21F3YZgKLL_rAIvcZrkI/edit#gid=0",
          "cachedResultName": "VEO_3"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1E3ujHxOFXvokfDnEMK7wcxy21F3YZgKLL_rAIvcZrkI",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1E3ujHxOFXvokfDnEMK7wcxy21F3YZgKLL_rAIvcZrkI/edit?usp=drivesdk",
          "cachedResultName": "google_veo_3_bulk_video_generation_template"
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "fcb5b9b3-9ebb-4fc0-acf6-5ec6e3bc6af7",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1776,
        -16
      ],
      "parameters": {
        "width": 480,
        "height": 512,
        "content": "## Bulk Veo 3 Video Generation from Google Sheets via Vertex AI\n### How it works\n\n1. Triggers a workflow based on a Google Sheet update.\n2. Collects data and sends a request to Vertex AI for video generation.\n3. Waits and checks if the video is created successfully.\n4. Converts the video to a binary file, uploads it to Google Drive, and updates the link in the sheet.\n5. Handles errors by updating the error status in the Google Sheet and retrying the video check.\n\n### Setup steps\n\n- [ ] Configure Google Sheet trigger with appropriate webhook.\n- [ ] Set up the Vertex AI API endpoint and credentials.\n- [ ] Configure Google Drive authentication for video uploads.\n- [ ] Set up Google Sheets API for updating video links and error statuses.\n"
      },
      "typeVersion": 1
    },
    {
      "id": "4daa43a5-9465-4309-8634-609b67377ef6",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1264,
        64
      ],
      "parameters": {
        "color": 7,
        "width": 416,
        "height": 304,
        "content": "## Trigger workflow from sheet\n\nStarts the workflow on a specified Google Sheet update."
      },
      "typeVersion": 1
    },
    {
      "id": "d8480c5a-2bba-472a-8c9b-d74f116ee7c0",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -832,
        48
      ],
      "parameters": {
        "color": 7,
        "width": 416,
        "height": 304,
        "content": "## Prepare video request\n\nCollects necessary data and sends a POST request to Vertex AI."
      },
      "typeVersion": 1
    },
    {
      "id": "30859fbc-eb14-4ce1-bf85-2e42acdf46e2",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -384,
        0
      ],
      "parameters": {
        "color": 7,
        "width": 1088,
        "height": 544,
        "content": "## Video generation loop\n\nWaits for video generation and retries as needed."
      },
      "typeVersion": 1
    },
    {
      "id": "4115d9fd-b106-4b05-b20a-fbc60bf936e8",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        400,
        -352
      ],
      "parameters": {
        "color": 7,
        "width": 416,
        "height": 304,
        "content": "## Post-process video file\n\nConverts video to a file and uploads it to Google Drive."
      },
      "typeVersion": 1
    },
    {
      "id": "292e9980-9030-4303-9d0e-3c6e16d00c6a",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        928,
        -160
      ],
      "parameters": {
        "color": 7,
        "width": 416,
        "height": 496,
        "content": "## Update sheet with results\n\nUpdates Google Sheet with video link or error status."
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "If": {
      "main": [
        [
          {
            "node": "Data collection",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait": {
      "main": [
        [
          {
            "node": "Fetch/Check Video",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Data collection": {
      "main": [
        [
          {
            "node": "Vertex AI Send for Generation",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch/Check Video": {
      "main": [
        [
          {
            "node": "If Video Is Created",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Video Is Created": {
      "main": [
        [
          {
            "node": "Convert to Binary File",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "If Video Generation Eror",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheet Trigger": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload Video to Drive": {
      "main": [
        [
          {
            "node": "Update video Link in sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Convert to Binary File": {
      "main": [
        [
          {
            "node": "Upload Video to Drive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If Video Generation Eror": {
      "main": [
        [
          {
            "node": "Update Eror in Sheet",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Wait before next video check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait before next video check": {
      "main": [
        [
          {
            "node": "Fetch/Check Video",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Vertex AI Send for Generation": {
      "main": [
        [
          {
            "node": "Wait",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}