{
  "name": "ContentForge → Instagram Publisher",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "contentforge-publish",
        "options": {
          "responseMode": "onReceived",
          "responseData": "allEntries"
        }
      },
      "id": "webhook-trigger",
      "name": "Webhook - ContentForge",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [250, 300],
      "webhookId": "contentforge-publish"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "condition-has-image",
              "leftValue": "={{ $json.body.image_url }}",
              "rightValue": "",
              "operator": {
                "type": "string",
                "operation": "notEmpty"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "check-image",
      "name": "Has Image?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [480, 300]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://graph.facebook.com/v21.0/{{ $json.body.instagram_account_id || $env.INSTAGRAM_ACCOUNT_ID }}/media",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "image_url",
              "value": "={{ $json.body.image_url }}"
            },
            {
              "name": "caption",
              "value": "={{ $json.body.caption }}{{ $json.body.hashtags ? '\\n\\n' + $json.body.hashtags : '' }}"
            },
            {
              "name": "access_token",
              "value": "={{ $env.INSTAGRAM_ACCESS_TOKEN }}"
            }
          ]
        },
        "options": {}
      },
      "id": "create-container",
      "name": "Create Media Container",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [720, 200]
    },
    {
      "parameters": {
        "amount": 5,
        "unit": "seconds"
      },
      "id": "wait-processing",
      "name": "Wait for Processing",
      "type": "n8n-nodes-base.wait",
      "typeVersion": 1.1,
      "position": [940, 200]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://graph.facebook.com/v21.0/{{ $('Webhook - ContentForge').item.json.body.instagram_account_id || $env.INSTAGRAM_ACCOUNT_ID }}/media_publish",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "creation_id",
              "value": "={{ $('Create Media Container').item.json.id }}"
            },
            {
              "name": "access_token",
              "value": "={{ $env.INSTAGRAM_ACCESS_TOKEN }}"
            }
          ]
        },
        "options": {}
      },
      "id": "publish-post",
      "name": "Publish to Instagram",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [1160, 200]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "=https://graph.facebook.com/v21.0/{{ $json.body.facebook_page_id || $env.FACEBOOK_PAGE_ID }}/feed",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "message",
              "value": "={{ $json.body.caption }}{{ $json.body.hashtags ? '\\n\\n' + $json.body.hashtags : '' }}"
            },
            {
              "name": "link",
              "value": "={{ $json.body.cta_url }}"
            },
            {
              "name": "access_token",
              "value": "={{ $env.FACEBOOK_ACCESS_TOKEN }}"
            }
          ]
        },
        "options": {}
      },
      "id": "post-facebook",
      "name": "Post to Facebook (Optional)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [720, 500],
      "disabled": true
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "status",
              "value": "published"
            },
            {
              "name": "post_id",
              "value": "={{ $json.id }}"
            },
            {
              "name": "brand",
              "value": "={{ $('Webhook - ContentForge').item.json.body.brand_name }}"
            },
            {
              "name": "angle",
              "value": "={{ $('Webhook - ContentForge').item.json.body.angle }}"
            },
            {
              "name": "published_at",
              "value": "={{ new Date().toISOString() }}"
            }
          ]
        },
        "options": {}
      },
      "id": "log-success",
      "name": "Log Success",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [1380, 200]
    },
    {
      "parameters": {
        "values": {
          "string": [
            {
              "name": "status",
              "value": "skipped - no image"
            },
            {
              "name": "caption",
              "value": "={{ $json.body.caption }}"
            },
            {
              "name": "brand",
              "value": "={{ $json.body.brand_name }}"
            }
          ]
        },
        "options": {}
      },
      "id": "no-image-log",
      "name": "No Image - Skip",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [720, 400]
    }
  ],
  "connections": {
    "Webhook - ContentForge": {
      "main": [
        [
          {
            "node": "Has Image?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Has Image?": {
      "main": [
        [
          {
            "node": "Create Media Container",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "No Image - Skip",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Media Container": {
      "main": [
        [
          {
            "node": "Wait for Processing",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Wait for Processing": {
      "main": [
        [
          {
            "node": "Publish to Instagram",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Publish to Instagram": {
      "main": [
        [
          {
            "node": "Log Success",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 1,
  "pinData": {}
}
