{
  "name": "CreativeAPI Batch Video Generation",
  "nodes": [
    {
      "parameters": {
        "operation": "readAll",
        "base": {
          "__rl": true,
          "value": "appXXXXXXXXXXXXXX",
          "mode": "id"
        },
        "table": {
          "__rl": true,
          "value": "Video Queue",
          "mode": "name"
        },
        "filters": [
          {
            "key": "Status",
            "value": "pending"
          }
        ],
        "returnAll": false,
        "limit": 20
      },
      "id": "fetch-queue",
      "name": "Fetch Video Queue",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 1,
      "position": [250, 300],
      "credentials": {
        "airtableApi": {
          "id": "airtable-creds",
          "name": "Airtable API"
        }
      },
      "notes": "Fetch up to 20 pending videos from Airtable queue"
    },
    {
      "parameters": {
        "mode": "runOnceForAllItems",
        "jsCode": "// Prepare batch of video generation requests\nconst items = $input.all();\nconst batch = [];\n\nfor (const item of items) {\n  batch.push({\n    prompt: item.json.Prompt,\n    model: item.json.Model || 'kling-v3',\n    duration: item.json.Duration || 5,\n    aspect_ratio: item.json.Aspect_Ratio || '16:9',\n    record_id: item.json.Record_ID,\n    webhook_url: $webhookUrl + '/batch-complete'\n  });\n}\n\nreturn batch.map(item => ({ json: item }));"
      },
      "id": "prepare-batch",
      "name": "Prepare Batch",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [450, 300],
      "notes": "Format items for CreativeAPI batch request"
    },
    {
      "parameters": {
        "url": "https://api.creativeai.run/v1/video/batch",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "videos",
              "value": "={{ $json }}"
            },
            {
              "name": "webhook_url",
              "value": "={{ $webhookUrl }}/batch-complete"
            },
            {
              "name": "failover",
              "value": "true"
            }
          ]
        }
      },
      "id": "submit-batch",
      "name": "Submit Batch Request",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4,
      "position": [650, 300],
      "credentials": {
        "httpHeaderAuth": {
          "id": "creativeapi-auth",
          "name": "CreativeAPI Key"
        }
      },
      "notes": "Submit batch of up to 20 videos to CreativeAPI"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "type": "string"
          },
          "conditions": [
            {
              "id": "success-check",
              "leftValue": "={{ $json.success }}",
              "rightValue": "true",
              "operator": {
                "type": "boolean",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "check-batch-status",
      "name": "Check Batch Status",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [850, 300],
      "notes": "Verify batch was accepted"
    },
    {
      "parameters": {
        "operation": "update",
        "base": {
          "__rl": true,
          "value": "appXXXXXXXXXXXXXX",
          "mode": "id"
        },
        "table": {
          "__rl": true,
          "value": "Video Queue",
          "mode": "name"
        },
        "id": "={{ $json.record_id }}",
        "fields": {
          "values": [
            {
              "fieldName": "Status",
              "fieldValue": "queued"
            },
            {
              "fieldName": "Batch ID",
              "fieldValue": "={{ $json.batch_id }}"
            },
            {
              "fieldName": "Submitted At",
              "fieldValue": "={{ $now.toISO() }}"
            }
          ]
        }
      },
      "id": "mark-queued",
      "name": "Mark as Queued",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 1,
      "position": [1050, 200],
      "credentials": {
        "airtableApi": {
          "id": "airtable-creds",
          "name": "Airtable API"
        }
      },
      "notes": "Update Airtable records with batch job IDs"
    },
    {
      "parameters": {
        "operation": "update",
        "base": {
          "__rl": true,
          "value": "appXXXXXXXXXXXXXX",
          "mode": "id"
        },
        "table": {
          "__rl": true,
          "value": "Video Queue",
          "mode": "name"
        },
        "id": "={{ $json.record_id }}",
        "fields": {
          "values": [
            {
              "fieldName": "Status",
              "fieldValue": "error"
            },
            {
              "fieldName": "Error Message",
              "fieldValue": "={{ $json.error }}"
            }
          ]
        }
      },
      "id": "mark-error",
      "name": "Mark as Error",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 1,
      "position": [1050, 400],
      "credentials": {
        "airtableApi": {
          "id": "airtable-creds",
          "name": "Airtable API"
        }
      },
      "notes": "Mark failed submissions with error"
    },
    {
      "parameters": {},
      "id": "webhook-complete",
      "name": "Batch Complete Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1,
      "position": [250, 500],
      "webhookId": "batch-complete",
      "httpMethod": "POST",
      "path": "batch-complete",
      "notes": "Receives webhook when batch completes"
    },
    {
      "parameters": {
        "mode": "runOnceForEachItem",
        "jsCode": "// Process batch completion results\nconst result = $json;\n\nreturn {\n  record_id: result.record_id,\n  status: result.status,\n  video_url: result.output?.video_url,\n  thumbnail_url: result.output?.thumbnail_url,\n  duration: result.output?.duration,\n  model: result.model,\n  error: result.error\n};"
      },
      "id": "process-results",
      "name": "Process Results",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [450, 500],
      "notes": "Parse batch completion webhook data"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "type": "string"
          },
          "conditions": [
            {
              "id": "completion-check",
              "leftValue": "={{ $json.status }}",
              "rightValue": "completed",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        }
      },
      "id": "check-result-status",
      "name": "Check Result Status",
      "type": "n8n-nodes-base.if",
      "typeVersion": 1,
      "position": [650, 500],
      "notes": "Check individual video result"
    },
    {
      "parameters": {
        "operation": "update",
        "base": {
          "__rl": true,
          "value": "appXXXXXXXXXXXXXX",
          "mode": "id"
        },
        "table": {
          "__rl": true,
          "value": "Video Queue",
          "mode": "name"
        },
        "id": "={{ $json.record_id }}",
        "fields": {
          "values": [
            {
              "fieldName": "Status",
              "fieldValue": "completed"
            },
            {
              "fieldName": "Video URL",
              "fieldValue": "={{ $json.video_url }}"
            },
            {
              "fieldName": "Thumbnail URL",
              "fieldValue": "={{ $json.thumbnail_url }}"
            },
            {
              "fieldName": "Completed At",
              "fieldValue": "={{ $now.toISO() }}"
            }
          ]
        }
      },
      "id": "mark-complete",
      "name": "Mark Complete",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 1,
      "position": [850, 400],
      "credentials": {
        "airtableApi": {
          "id": "airtable-creds",
          "name": "Airtable API"
        }
      },
      "notes": "Update record with completed video"
    },
    {
      "parameters": {
        "operation": "update",
        "base": {
          "__rl": true,
          "value": "appXXXXXXXXXXXXXX",
          "mode": "id"
        },
        "table": {
          "__rl": true,
          "value": "Video Queue",
          "mode": "name"
        },
        "id": "={{ $json.record_id }}",
        "fields": {
          "values": [
            {
              "fieldName": "Status",
              "fieldValue": "failed"
            },
            {
              "fieldName": "Error Message",
              "fieldValue": "={{ $json.error }}"
            }
          ]
        }
      },
      "id": "mark-failed",
      "name": "Mark Failed",
      "type": "n8n-nodes-base.airtable",
      "typeVersion": 1,
      "position": [850, 600],
      "credentials": {
        "airtableApi": {
          "id": "airtable-creds",
          "name": "Airtable API"
        }
      },
      "notes": "Mark failed records"
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 5
            }
          ]
        }
      },
      "id": "schedule-trigger",
      "name": "Every 5 Minutes",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1,
      "position": [50, 300],
      "notes": "Process queue every 5 minutes"
    }
  ],
  "connections": {
    "Fetch Video Queue": {
      "main": [
        [
          {
            "node": "Prepare Batch",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Prepare Batch": {
      "main": [
        [
          {
            "node": "Submit Batch Request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Submit Batch Request": {
      "main": [
        [
          {
            "node": "Check Batch Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Batch Status": {
      "main": [
        [
          {
            "node": "Mark as Queued",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Mark as Error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Batch Complete Webhook": {
      "main": [
        [
          {
            "node": "Process Results",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Process Results": {
      "main": [
        [
          {
            "node": "Check Result Status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Check Result Status": {
      "main": [
        [
          {
            "node": "Mark Complete",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Mark Failed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Every 5 Minutes": {
      "main": [
        [
          {
            "node": "Fetch Video Queue",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 2,
  "updatedAt": "2026-04-04T00:00:00.000Z",
  "versionId": "1.0.0"
}
