{
  "name": "IQCore: events loop and create post",
  "nodes": [
    {
      "parameters": {
        "width": 1900,
        "height": 150,
        "color": 5,
        "content": "## IQCore events loop\nEvery minute: read the saved cursor, call GET /events, hand each event to your own code, then save next_cursor and go on while has_more is true. A 410 resets the cursor to oldest_cursor. Create the credential \"IQCore API key\" (Header Auth, name Authorization, value Bearer iqk_live_XXXX) and pick it in \"Get events\" and in the other HTTP Request nodes. Replace the \"Your code\" nodes with your own steps."
      },
      "id": "0a17a2e8-bba1-63fa-db66-207677e40c77",
      "name": "Note: events loop",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -260
      ]
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "minutes",
              "minutesInterval": 1
            }
          ]
        }
      },
      "id": "742b9194-13c5-c95d-7a2a-2f4a74a8916f",
      "name": "Every minute",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// The cursor lives in the workflow's static data (one cursor per organization).\n// Empty on the first run: then GET /events starts at the oldest event IQCore keeps.\nconst data = $getWorkflowStaticData('global');\nconst query = { limit: 100 };\nif (data.cursor) query.since = data.cursor;\nreturn [{ json: { query } }];"
      },
      "id": "49d56bb3-e213-c6a0-58f9-555777eb715f",
      "name": "Read cursor",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://iqcore.ai/api/public/v1/events",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "specifyQuery": "json",
        "jsonQuery": "={{ JSON.stringify($json.query) }}",
        "options": {
          "response": {
            "response": {
              "fullResponse": true,
              "neverError": true,
              "responseFormat": "json"
            }
          }
        }
      },
      "id": "5a7e90e6-6252-f26c-5cfd-7b726f68e4d5",
      "name": "Get events",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        0
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "REPLACE_ME",
          "name": "IQCore API key"
        }
      },
      "onError": "continueRegularOutput"
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "6b8984be-1435-110e-bcd2-216b1fe9d5ff",
              "leftValue": "={{ $json.statusCode }}",
              "rightValue": 410,
              "operator": {
                "type": "number",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "d98f7ec3-e68e-a87d-2bd9-83066484757f",
      "name": "Cursor expired (410)?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        660,
        0
      ]
    },
    {
      "parameters": {
        "jsCode": "// 410 cursor_expired: you were away for more than 30 days.\n// Start again from error.oldest_cursor and reconcile what you missed.\nconst data = $getWorkflowStaticData('global');\nconst err = ($json.body && $json.body.error) || {};\nif (err.oldest_cursor) data.cursor = err.oldest_cursor;\nreturn [{ json: { reset_to: data.cursor || null, reconcile_since: data.lastOk || null } }];"
      },
      "id": "72603a19-9b43-6d51-1fab-ab6c6634f35a",
      "name": "Reset cursor to oldest_cursor",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        -140
      ]
    },
    {
      "parameters": {},
      "id": "9e603054-7487-bb26-2847-a6acdab4a537",
      "name": "Your code: reconcile after 410",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1140,
        -140
      ],
      "notes": "Call GET /posts?updated_since={{ $json.reconcile_since }} and compare with your records.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "f1172df4-29af-57ab-10c6-8e5c1c33b53f",
              "leftValue": "={{ $json.statusCode }}",
              "rightValue": 200,
              "operator": {
                "type": "number",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "61c21fec-e825-98cd-360f-b6da7e1704a9",
      "name": "Status 200?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        900,
        100
      ]
    },
    {
      "parameters": {},
      "id": "6eca16b5-7ae1-79d2-ab23-3132ed4d8677",
      "name": "Your code: handle the error",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1140,
        560
      ],
      "notes": "401 invalid key, 429 rate limited (wait Retry-After), 5xx (retry next minute).",
      "notesInFlow": true
    },
    {
      "parameters": {
        "fieldToSplitOut": "body.data",
        "options": {}
      },
      "id": "658bf404-25a2-9c46-c23a-0b662f0199a3",
      "name": "Split events",
      "type": "n8n-nodes-base.splitOut",
      "typeVersion": 1,
      "position": [
        1140,
        40
      ]
    },
    {
      "parameters": {
        "rules": {
          "values": [
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "3fc5e7a9-7625-3587-0594-e745ded0c610",
                    "leftValue": "={{ $json.type }}",
                    "rightValue": "post.published",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renamedOutput": true,
              "outputKey": "post.published"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "2c4d5c54-e53b-8cb8-8b86-906304fa8913",
                    "leftValue": "={{ $json.type }}",
                    "rightValue": "post.failed",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renamedOutput": true,
              "outputKey": "post.failed"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "3e2e69e8-7f6d-d1e5-b58e-531f3baa09cc",
                    "leftValue": "={{ $json.type }}",
                    "rightValue": "channel.needs_reconnect",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  },
                  {
                    "id": "30cdfd6a-c23b-458b-2061-82d67faa65f2",
                    "leftValue": "={{ $json.type }}",
                    "rightValue": "channel.expiring",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "or"
              },
              "renamedOutput": true,
              "outputKey": "channel needs attention"
            },
            {
              "conditions": {
                "options": {
                  "caseSensitive": true,
                  "leftValue": "",
                  "typeValidation": "strict"
                },
                "conditions": [
                  {
                    "id": "074d6549-6241-c878-4b4a-c6093c3568ee",
                    "leftValue": "={{ $json.type }}",
                    "rightValue": "credits.low",
                    "operator": {
                      "type": "string",
                      "operation": "equals"
                    }
                  }
                ],
                "combinator": "and"
              },
              "renamedOutput": true,
              "outputKey": "credits.low"
            }
          ]
        },
        "options": {
          "fallbackOutput": "extra",
          "renameFallbackOutput": "other event types"
        }
      },
      "id": "fde2829f-cf20-1b76-51e5-836e69a8b3a2",
      "name": "Which event?",
      "type": "n8n-nodes-base.switch",
      "typeVersion": 3,
      "position": [
        1360,
        40
      ]
    },
    {
      "parameters": {},
      "id": "817f38bc-7608-66cf-d78b-520f2ae1ed0b",
      "name": "Your code: post published",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1620,
        -80
      ],
      "notes": "data.post_id, data.platform, data.permalink, data.platform_post_id. Match data.post_id with the id posts_create gave you (or GET /posts?external_ref=).",
      "notesInFlow": true
    },
    {
      "parameters": {},
      "id": "2774d848-e35f-6d82-32db-a7a950cbb97d",
      "name": "Your code: post failed",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1620,
        40
      ],
      "notes": "data.error_code. Call GET /posts/{data.post_id} for the reason, then tell a person.",
      "notesInFlow": true
    },
    {
      "parameters": {},
      "id": "8e463728-f98f-635f-8502-4b0be28f1066",
      "name": "Your code: channel needs attention",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1620,
        160
      ],
      "notes": "Tell a person to open the channel reconnect_url (GET /channels/{data.channel_id}).",
      "notesInFlow": true
    },
    {
      "parameters": {},
      "id": "1fe6aa09-6ce6-b84c-0f42-40932169108c",
      "name": "Your code: credits low",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1620,
        280
      ],
      "notes": "data.balance, data.threshold. Buy credits or pause spending.",
      "notesInFlow": true
    },
    {
      "parameters": {},
      "id": "b42e8f8e-4b9f-3db0-0700-622bb6a10d56",
      "name": "Ignore other event types",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        1620,
        400
      ],
      "notes": "Unknown and unused types: ignore them. New types can appear at any time.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "jsCode": "// Runs after the events of this page were handled (the branch above is on top: executionOrder v1).\n// Saving the cursor only now means a crash re-reads the same page instead of skipping it.\nconst body = $('Get events').first().json.body || {};\nconst data = $getWorkflowStaticData('global');\nif (body.next_cursor) data.cursor = body.next_cursor;\ndata.lastOk = new Date().toISOString();\nreturn [{ json: { has_more: !!body.has_more, cursor: data.cursor || null } }];"
      },
      "id": "3bc8ef57-24aa-befc-5927-75336743c265",
      "name": "Save cursor",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1140,
        440
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "bfdca11d-02c0-9147-bd78-018a64a25986",
              "leftValue": "={{ $json.has_more }}",
              "rightValue": "",
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "fd446132-718d-2957-55e7-468fd274a914",
      "name": "More pages?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        1360,
        440
      ]
    },
    {
      "parameters": {
        "width": 1100,
        "height": 140,
        "color": 4,
        "content": "## Create a draft post\nRun by hand. Replace the channel id, media id and time in the body (scheduled_at must be 1 minute to 365 days ahead). The Idempotency-Key is the execution id; for a post you may create again later build the key from your own data, and keep external_ref stable. A new post is a draft: it needs an approval (see the Post states guide)."
      },
      "id": "7cb9e7e6-c987-191a-3e86-6d29f5789043",
      "name": "Note: create post",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        700
      ]
    },
    {
      "parameters": {},
      "id": "44029a14-ec7f-a792-48b5-c0f89117ea8e",
      "name": "Run by hand",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        880
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://iqcore.ai/api/public/v1/posts",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Idempotency-Key",
              "value": "={{ $execution.id }}"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "{\n  \"channel_ids\": [\"PASTE_CHANNEL_ID\"],\n  \"caption\": \"New in the shop today.\",\n  \"media_ids\": [\"PASTE_MEDIA_ID\"],\n  \"scheduled_at\": \"2026-12-20T09:00:00Z\",\n  \"external_ref\": \"n8n:example:1\"\n}",
        "options": {
          "response": {
            "response": {
              "fullResponse": true,
              "neverError": true,
              "responseFormat": "json"
            }
          }
        }
      },
      "id": "2c5561ef-f540-d905-d386-15b2a99d319e",
      "name": "Create post",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        260,
        880
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "REPLACE_ME",
          "name": "IQCore API key"
        }
      }
    },
    {
      "parameters": {},
      "id": "08e61b1f-bb0c-4005-2725-e13772a24caf",
      "name": "Your code: post answer",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        520,
        880
      ],
      "notes": "201 = draft created (body.id). 409 = body.error.existing_id already exists. Next: POST /posts/{id}/request-approval.",
      "notesInFlow": true
    },
    {
      "parameters": {
        "width": 1100,
        "height": 120,
        "color": 3,
        "content": "## Daily reconcile\nThe events feed is exactly-once while you follow the cursor, but an event is lost if IQCore stops between a change and its event. Once a day list the posts changed since the last run and compare them with your records."
      },
      "id": "804c33e9-5e54-47a2-5dca-54e0c715e158",
      "name": "Note: daily reconcile",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        1080
      ]
    },
    {
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "days",
              "daysInterval": 1,
              "triggerAtHour": 3
            }
          ]
        }
      },
      "id": "9a3e9468-37a5-e25c-dca4-45361ee295af",
      "name": "Every day 03:00",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        1240
      ]
    },
    {
      "parameters": {
        "jsCode": "// Take the time from BEFORE the request, so nothing slips between two runs.\nconst data = $getWorkflowStaticData('global');\nconst since = data.reconciledAt || new Date(Date.now() - 26 * 3600 * 1000).toISOString();\ndata.reconciledAt = new Date().toISOString();\nreturn [{ json: { query: { updated_since: since, limit: 50 } } }];"
      },
      "id": "72e793d2-24c7-4f1a-95e9-f3462d04415c",
      "name": "Since when?",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        1240
      ]
    },
    {
      "parameters": {
        "method": "GET",
        "url": "https://iqcore.ai/api/public/v1/posts",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendQuery": true,
        "specifyQuery": "json",
        "jsonQuery": "={{ JSON.stringify($json.query) }}",
        "options": {
          "response": {
            "response": {
              "fullResponse": true,
              "neverError": true,
              "responseFormat": "json"
            }
          }
        }
      },
      "id": "ff4c4e7c-ef7e-1ddb-fdb9-630a7f3c7533",
      "name": "List changed posts",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        1240
      ],
      "credentials": {
        "httpHeaderAuth": {
          "id": "REPLACE_ME",
          "name": "IQCore API key"
        }
      }
    },
    {
      "parameters": {},
      "id": "16b37997-5d83-0620-461e-cb364339c3ac",
      "name": "Your code: compare with your records",
      "type": "n8n-nodes-base.noOp",
      "typeVersion": 1,
      "position": [
        680,
        1240
      ],
      "notes": "body.posts is one page (newest first). While body.has_more is true, call again with cursor=body.next_cursor.",
      "notesInFlow": true
    }
  ],
  "connections": {
    "Every minute": {
      "main": [
        [
          {
            "node": "Read cursor",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read cursor": {
      "main": [
        [
          {
            "node": "Get events",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get events": {
      "main": [
        [
          {
            "node": "Cursor expired (410)?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Cursor expired (410)?": {
      "main": [
        [
          {
            "node": "Reset cursor to oldest_cursor",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Status 200?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Reset cursor to oldest_cursor": {
      "main": [
        [
          {
            "node": "Your code: reconcile after 410",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Status 200?": {
      "main": [
        [
          {
            "node": "Split events",
            "type": "main",
            "index": 0
          },
          {
            "node": "Save cursor",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Your code: handle the error",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split events": {
      "main": [
        [
          {
            "node": "Which event?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Which event?": {
      "main": [
        [
          {
            "node": "Your code: post published",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Your code: post failed",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Your code: channel needs attention",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Your code: credits low",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Ignore other event types",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Save cursor": {
      "main": [
        [
          {
            "node": "More pages?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "More pages?": {
      "main": [
        [
          {
            "node": "Read cursor",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Run by hand": {
      "main": [
        [
          {
            "node": "Create post",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create post": {
      "main": [
        [
          {
            "node": "Your code: post answer",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Every day 03:00": {
      "main": [
        [
          {
            "node": "Since when?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Since when?": {
      "main": [
        [
          {
            "node": "List changed posts",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "List changed posts": {
      "main": [
        [
          {
            "node": "Your code: compare with your records",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "active": false,
  "tags": []
}
