{
  "name": "Generate AI videos with Kling 3 from a form (MegaBrain)",
  "nodes": [
    {
      "parameters": {
        "content": "# Idea → Kling 3 video\nType an idea in the form. A free text model (`mb-auto/free`) turns it into a cinematic prompt, **Kling 3** renders the clip, and the form shows the video link.\n\nKling 3 standard costs $0.097 a second without sound (5 s ≈ $0.48).\n\n## Setup (2 minutes)\n1. Get a key at [getmegabrain.com/keys](https://getmegabrain.com/keys).\n2. In n8n create a **Header Auth** credential named `MegaBrain API key`:\n   - Name: `Authorization`\n   - Value: `Bearer YOUR_KEY`\n3. Pick it in every HTTP Request node with the 🔑 mark.\n\nFailed jobs are refunded automatically. Prices: [getmegabrain.com/docs/media](https://getmegabrain.com/docs/media).\n\n**Post it next:** add a YouTube, TikTok (Blotato) or Google Drive node after *Video link*.",
        "height": 600,
        "width": 440
      },
      "id": "e4583938-ed29-5ef2-8d03-8ebd61e93979",
      "name": "About",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -520,
        -280
      ]
    },
    {
      "parameters": {
        "formTitle": "Make a video with Kling 3",
        "formDescription": "Describe the scene. The video link appears here when it is ready (1–3 minutes).",
        "formFields": {
          "values": [
            {
              "fieldLabel": "Idea",
              "fieldType": "textarea",
              "requiredField": true,
              "placeholder": "a drone flies over a snowy mountain lake at sunrise"
            },
            {
              "fieldLabel": "Seconds",
              "fieldType": "dropdown",
              "fieldOptions": {
                "values": [
                  {
                    "option": "3"
                  },
                  {
                    "option": "5"
                  },
                  {
                    "option": "10"
                  }
                ]
              },
              "requiredField": true
            },
            {
              "fieldLabel": "Sound",
              "fieldType": "dropdown",
              "fieldOptions": {
                "values": [
                  {
                    "option": "no"
                  },
                  {
                    "option": "yes"
                  }
                ]
              },
              "requiredField": true
            }
          ]
        },
        "options": {}
      },
      "id": "efd9fe00-2db1-57b2-81e6-cc8267484683",
      "name": "Video idea form",
      "type": "n8n-nodes-base.formTrigger",
      "typeVersion": 2.2,
      "position": [
        0,
        0
      ],
      "webhookId": "6f08ac4e-e160-5ab1-9847-36519ecf8f72"
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://getmegabrain.com/api/gateway/v1/chat/completions",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ model: 'mb-auto/free', messages: [ { role: 'system', content: 'You write prompts for the Kling video model. Reply with one paragraph under 60 words: subject, action, camera movement, lighting, style. No preamble, no quotes.' }, { role: 'user', content: $json.Idea } ] }) }}",
        "options": {
          "timeout": 120000
        },
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth"
      },
      "id": "39ee345a-1063-50c6-97f0-ed6e8a20fd9a",
      "name": "Write the prompt (free model)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        220,
        0
      ],
      "retryOnFail": true,
      "waitBetweenTries": 3000
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://getmegabrain.com/api/gateway/v1/queue/kling-video/v3/standard/text-to-video",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ prompt: $json.choices[0].message.content.trim(), duration: $('Video idea form').item.json.Seconds, generate_audio: $('Video idea form').item.json.Sound === 'yes', aspect_ratio: '16:9' }) }}",
        "options": {
          "timeout": 120000
        },
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth"
      },
      "id": "46f39fce-96a5-5e7c-b7b8-9d33809007cb",
      "name": "Submit Kling 3 job",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        0
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "69a88d2c-df0c-5e6d-baa0-fe981283d55e",
              "name": "video_url",
              "value": "={{ $json.video.url }}",
              "type": "string"
            },
            {
              "id": "2ef5e8ef-9401-531b-9a62-3d5f8c1ed515",
              "name": "prompt",
              "value": "={{ $('Write the prompt (free model)').item.json.choices[0].message.content.trim() }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "a7c71683-605a-5feb-b904-e092cd764efa",
      "name": "Video link",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1580,
        -120
      ]
    },
    {
      "parameters": {
        "operation": "completion",
        "respondWith": "showText",
        "responseText": "=<div style=\"font-family:system-ui,-apple-system,sans-serif;max-width:640px;margin:48px auto;padding:24px;line-height:1.5\"><h2>Your video is ready</h2><p><a href=\"{{ $json.video_url }}\" target=\"_blank\">Open the video</a></p><p><video src=\"{{ $json.video_url }}\" controls style=\"max-width:100%\"></video></p><p>Prompt: {{ $json.prompt }}</p></div>"
      },
      "id": "1571aacb-8059-50dc-aa07-9f7b56f524cf",
      "name": "Show the video",
      "type": "n8n-nodes-base.form",
      "typeVersion": 2.3,
      "position": [
        1800,
        -120
      ],
      "webhookId": "0ae8ef1f-dcac-5a98-b4f2-f11ce22b1d3d"
    },
    {
      "parameters": {
        "amount": 15
      },
      "id": "009eddd2-d061-51a0-9227-352bae7c1677",
      "name": "Wait 15s",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        660,
        0
      ],
      "webhookId": "fcd58daa-4500-50c0-a35f-976522864586"
    },
    {
      "parameters": {
        "url": "={{ $('Submit Kling 3 job').item.json.status_url }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {
          "timeout": 60000
        }
      },
      "id": "49fe9db4-17ab-5ad4-a630-45b1c70e4ab1",
      "name": "Get video status",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        880,
        0
      ]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.status }}",
                    "rightValue": "COMPLETED",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Completed"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict",
                  "version": 2
                },
                "conditions": [
                  {
                    "leftValue": "={{ $json.status }}",
                    "rightValue": "FAILED",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renameOutput": true,
              "outputKey": "Failed"
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra",
          "renameFallbackOutput": "Running"
        }
      },
      "id": "b4288571-dac6-5953-918c-0f7699c7cde2",
      "name": "Done yet?",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3.2,
      "position": [
        1100,
        0
      ]
    },
    {
      "parameters": {
        "url": "={{ $('Submit Kling 3 job').item.json.response_url }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {
          "timeout": 60000
        }
      },
      "id": "c191d3f9-68f4-502d-a867-d6573e5f0672",
      "name": "Get video",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1360,
        -120
      ]
    },
    {
      "parameters": {
        "errorMessage": "=The video job failed. MegaBrain returned the price to your balance. Request: {{ $('Submit Kling 3 job').item.json.request_id }}"
      },
      "id": "6ef4f3ee-908c-5f61-be25-6c4bf511797f",
      "name": "Job failed (refunded)",
      "type": "n8n-nodes-base.stopAndError",
      "typeVersion": 1,
      "position": [
        1360,
        60
      ]
    },
    {
      "parameters": {
        "content": "## 1. Prompt and job\nThe free model `mb-auto/free` turns the idea into a Kling prompt (no charge). The job is then submitted to the queue; its price is charged at submit and refunded if it fails.",
        "height": 220,
        "width": 420,
        "color": 5
      },
      "id": "a380a449-214d-5c3c-b97e-f9a5dad52616",
      "name": "Note: prompt",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        160,
        -260
      ]
    },
    {
      "parameters": {
        "content": "## 2. Poll the job\nEvery 15 s the loop reads `status_url`. `COMPLETED` → fetch the video from `response_url`; `FAILED` → stop with an error (the price is back on your balance); anything else → wait again.",
        "height": 180,
        "width": 560,
        "color": 5
      },
      "id": "ec424f72-02e5-5314-bea4-5bab5120f69c",
      "name": "Note: poll",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        640,
        -300
      ]
    }
  ],
  "connections": {
    "Video idea form": {
      "main": [
        [
          {
            "node": "Write the prompt (free model)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write the prompt (free model)": {
      "main": [
        [
          {
            "node": "Submit Kling 3 job",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Submit Kling 3 job": {
      "main": [
        [
          {
            "node": "Wait 15s",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait 15s": {
      "main": [
        [
          {
            "node": "Get video status",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get video status": {
      "main": [
        [
          {
            "node": "Done yet?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Done yet?": {
      "main": [
        [
          {
            "node": "Get video",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Job failed (refunded)",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Wait 15s",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get video": {
      "main": [
        [
          {
            "node": "Video link",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Video link": {
      "main": [
        [
          {
            "node": "Show the video",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}
