{
  "name": "SocialCrawl: Add the newest comments under a list of posts to a sheet",
  "nodes": [
    {
      "id": "sc-1",
      "name": "Every morning",
      "type": "n8n-nodes-base.scheduleTrigger",
      "typeVersion": 1.2,
      "position": [
        0,
        0
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "days",
              "triggerAtHour": 9
            }
          ]
        }
      }
    },
    {
      "id": "sc-5",
      "name": "Your post links",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ],
      "parameters": {
        "jsCode": "return [\"https://www.instagram.com/p/Df3Rp0bKNdr/\",\"https://www.instagram.com/p/Df7keQHFxvw/\"].map((value) => ({ json: { value } }));"
      }
    },
    {
      "id": "sc-6",
      "name": "Read the newest comments",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        0
      ],
      "parameters": {
        "method": "GET",
        "url": "=https://www.socialcrawl.dev/v1/instagram/post/comments?url={{ encodeURIComponent($json.value) }}&sort=recent",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-SocialCrawl-Client",
              "value": "template:n8n-post-comments"
            }
          ]
        },
        "options": {
          "timeout": 300000,
          "response": {
            "response": {
              "neverError": true,
              "fullResponse": true
            }
          }
        }
      },
      "credentials": {
        "httpHeaderAuth": {
          "name": "SocialCrawl API"
        }
      }
    },
    {
      "id": "sc-2",
      "name": "Out of credits?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2,
      "position": [
        660,
        0
      ],
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose"
          },
          "conditions": [
            {
              "id": "status-402",
              "leftValue": "={{ $json.statusCode }}",
              "rightValue": 402,
              "operator": {
                "type": "number",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      }
    },
    {
      "id": "sc-3",
      "name": "Stop with the offer",
      "type": "n8n-nodes-base.stopAndError",
      "typeVersion": 1,
      "position": [
        900,
        -140
      ],
      "parameters": {
        "errorMessage": "={{ $json.body.error.message }} Buy credits: {{ $json.body.error.details.checkout_url || $json.body.error.details.top_up_url }}"
      }
    },
    {
      "id": "sc-7",
      "name": "One row per result",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        140
      ],
      "parameters": {
        "jsCode": "return $input.all().flatMap((item, i) => (item.json.body?.data?.items ?? []).map((r) => ({ json: {\n  post: $('Your post links').all()[i]?.json.value, published_at: r.comment.published_at,\n  author: r.comment.author?.username, text: r.comment.text, likes: r.comment.engagement?.likes,\n} })));"
      }
    },
    {
      "id": "sc-4",
      "name": "Append to the sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        1140,
        140
      ],
      "parameters": {
        "operation": "append",
        "documentId": {
          "__rl": true,
          "mode": "url",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "name",
          "value": "Rows"
        },
        "columns": {
          "mappingMode": "autoMapInputData",
          "value": {}
        },
        "options": {}
      }
    },
    {
      "id": "sc-8",
      "name": "How this workflow works",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -520
      ],
      "parameters": {
        "content": "## Add the newest comments under a list of posts to a sheet\n\nEach post costs 5 credits a page of comments on Instagram (1 on most other platforms).\n\n**Setup:** put your post links in Your post links, pick your Google Sheets credential and sheet in Append to the sheet, and turn the workflow on.\n\n**Key:** create a Header Auth credential named SocialCrawl API, with Name `x-api-key` and your API key as Value. New accounts get 100 free credits at https://www.socialcrawl.dev/auth/register\n\n**Out of credits:** a 402 stops the workflow with the price of the pack and the checkout link in the error message. Never retry a 402.",
        "width": 560,
        "height": 460
      }
    }
  ],
  "connections": {
    "Every morning": {
      "main": [
        [
          {
            "node": "Your post links",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Your post links": {
      "main": [
        [
          {
            "node": "Read the newest comments",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read the newest comments": {
      "main": [
        [
          {
            "node": "Out of credits?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Out of credits?": {
      "main": [
        [
          {
            "node": "Stop with the offer",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "One row per result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "One row per result": {
      "main": [
        [
          {
            "node": "Append to the sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
