{
  "name": "Instagram carousel images and caption with FLUX 2 (MegaBrain)",
  "nodes": [
    {
      "parameters": {
        "content": "# Instagram carousel — FLUX 2\nGive a topic. A free text model plans the slides and writes the caption, **FLUX 2** draws every slide in 4:5, and the last node returns the image URLs plus the caption, ready for Instagram, Blotato or Drive.\n\nFLUX 2 costs $0.014 per megapixel: a 5-slide carousel is about $0.14.\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).",
        "height": 560,
        "width": 440
      },
      "id": "b769ff21-4931-5961-922c-4431ff525380",
      "name": "About",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -520,
        -280
      ]
    },
    {
      "parameters": {},
      "id": "fd996a8c-c853-54d8-a4a4-e690053f16ff",
      "name": "Start",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "72a58ad5-d4c0-5f4d-ba0f-32842161a72b",
              "name": "topic",
              "value": "5 morning habits of calm people",
              "type": "string"
            },
            {
              "id": "8ea1dee4-78c4-557b-a4a3-0ce118770595",
              "name": "slides",
              "value": 5,
              "type": "number"
            },
            {
              "id": "22ac117f-9433-5589-8fbd-cb41d00b1d02",
              "name": "style",
              "value": "soft pastel editorial photography, natural light, lots of empty space for text",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "24b8e88f-b107-56dc-a9da-a0009c59a53e",
      "name": "Topic",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        200,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://getmegabrain.com/api/gateway/v1/chat/completions",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ model: 'mb-auto/free', response_format: { type: 'json_object' }, messages: [ { role: 'system', content: 'You plan Instagram carousels. Reply with JSON only: {\"caption\": string with hashtags, \"slides\": [{\"title\": short slide headline, \"prompt\": image prompt without any text in the picture}]}' }, { role: 'user', content: `Topic: ${$json.topic}. Slides: ${$json.slides}. Visual style: ${$json.style}` } ] }) }}",
        "options": {
          "timeout": 120000
        },
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth"
      },
      "id": "7f651f11-b941-578b-99c7-b986361b49eb",
      "name": "Plan slides (free model)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        420,
        0
      ],
      "retryOnFail": true,
      "waitBetweenTries": 3000
    },
    {
      "parameters": {
        "jsCode": "const raw = $input.first().json.choices[0].message.content;\nconst text = raw.replace(/^```(?:json)?\\s*/i, '').replace(/```\\s*$/, '').trim();\nconst plan = JSON.parse(text);\nconst { style, slides } = $('Topic').first().json;\nreturn plan.slides.slice(0, slides).map((slide, index) => ({\n  json: { index: index + 1, title: slide.title, prompt: `${slide.prompt.replace(/[.\\s]+$/, '')}. ${style}`, caption: plan.caption },\n}));"
      },
      "id": "f022780d-4e4b-538d-b69b-fe93cf66306a",
      "name": "Parse the plan",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        640,
        0
      ]
    },
    {
      "parameters": {
        "options": {}
      },
      "id": "0ac9b11e-c2f0-5006-a21f-cfa8af366c10",
      "name": "One slide at a time",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        860,
        0
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://getmegabrain.com/api/gateway/v1/images/generations",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ model: 'flux-2', prompt: $json.prompt, image_size: { width: 1080, height: 1350 } }) }}",
        "options": {
          "timeout": 120000
        },
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth"
      },
      "id": "e0513468-3f79-5899-9f9d-f54f6ebc9a9c",
      "name": "Draw slide (FLUX 2)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1080,
        100
      ]
    },
    {
      "parameters": {
        "jsCode": "const slides = $input.all().map((item) => item.json);\nreturn [{ json: {\n  caption: slides[0]?.caption,\n  images: slides.map(({ index, title, image_url }) => ({ slide: index, title, url: image_url })),\n} }];"
      },
      "id": "ada737ca-ba8c-5e75-af84-6b1c853d12d9",
      "name": "Carousel",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1080,
        -140
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "a9580568-70c2-5de5-b1b5-aed2e923bf92",
              "leftValue": "={{ $json.data?.[0]?.url ?? $json.images?.[0]?.url ?? '' }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "d7c47f6a-6f24-51a6-81aa-88d12f48afa8",
      "name": "Slide ready?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1300,
        100
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "d32c68bc-f964-5142-b374-4774174984ce",
              "name": "image_url",
              "value": "={{ $json.data?.[0]?.url ?? $json.images?.[0]?.url ?? '' }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "cb034f1f-18a2-5503-9826-1ad28315b1d6",
      "name": "Slide URL",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1520,
        0
      ]
    },
    {
      "parameters": {
        "amount": 10
      },
      "id": "1b55b8c9-c309-53e4-9773-7e42310372f3",
      "name": "Queued: wait 10s (Slide)",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [
        1520,
        220
      ],
      "webhookId": "38f91567-cbaa-5924-b3e1-6071c34b10e4"
    },
    {
      "parameters": {
        "url": "={{ $json.response_url }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "options": {
          "timeout": 60000
        }
      },
      "id": "4db99023-456c-5dee-8e6b-eef58405b066",
      "name": "Fetch queued slide",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1740,
        220
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "e010bad9-3a89-5003-ac32-b6b7d3863d66",
              "name": "index",
              "value": "={{ $('One slide at a time').first().json.index }}",
              "type": "number"
            },
            {
              "id": "c72cc2ed-0775-5c69-97ad-c4230cf086ce",
              "name": "title",
              "value": "={{ $('One slide at a time').first().json.title }}",
              "type": "string"
            },
            {
              "id": "91186baf-61ec-57de-aec0-38360fd3f563",
              "name": "caption",
              "value": "={{ $('One slide at a time').first().json.caption }}",
              "type": "string"
            },
            {
              "id": "b0b03c94-0e06-56c3-8d43-f2241ecccd0a",
              "name": "image_url",
              "value": "={{ $json.image_url }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "960a69fa-5b4d-5fe4-b0a4-79a09140736e",
      "name": "Slide",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        1740,
        0
      ]
    },
    {
      "parameters": {
        "content": "## 1. Plan\nEdit **Topic**: subject, number of slides, visual style. The free model returns JSON with a caption and one image prompt per slide; **Parse the plan** strips code fences and keeps the slide count.",
        "height": 220,
        "width": 440,
        "color": 5
      },
      "id": "84a0c881-f284-54bc-86f7-ef94c8df68af",
      "name": "Note: plan",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        160,
        -260
      ]
    },
    {
      "parameters": {
        "content": "## 2. Draw slides one by one\nEach slide goes through FLUX 2 at 1080×1350. If an image is not ready in ~110 s the loop polls `response_url`. When all slides are done, **Carousel** returns the caption and the URLs.",
        "height": 200,
        "width": 520,
        "color": 5
      },
      "id": "f3e8925c-5fa6-571f-8335-2d37e738ced5",
      "name": "Note: draw",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        1040,
        -300
      ]
    }
  ],
  "connections": {
    "Start": {
      "main": [
        [
          {
            "node": "Topic",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Topic": {
      "main": [
        [
          {
            "node": "Plan slides (free model)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Plan slides (free model)": {
      "main": [
        [
          {
            "node": "Parse the plan",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse the plan": {
      "main": [
        [
          {
            "node": "One slide at a time",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "One slide at a time": {
      "main": [
        [
          {
            "node": "Carousel",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Draw slide (FLUX 2)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Draw slide (FLUX 2)": {
      "main": [
        [
          {
            "node": "Slide ready?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Slide ready?": {
      "main": [
        [
          {
            "node": "Slide URL",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Queued: wait 10s (Slide)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Queued: wait 10s (Slide)": {
      "main": [
        [
          {
            "node": "Fetch queued slide",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Fetch queued slide": {
      "main": [
        [
          {
            "node": "Slide ready?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Slide URL": {
      "main": [
        [
          {
            "node": "Slide",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Slide": {
      "main": [
        [
          {
            "node": "One slide at a time",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "tags": []
}
