{
  "name": "SocialCrawl: Add the new posts of a list of accounts to a sheet every day",
  "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": "Read the newest run",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        330,
        0
      ],
      "parameters": {
        "method": "GET",
        "url": "https://www.socialcrawl.dev/v1/monitors/YOUR_MONITOR_ID/runs?include=result&limit=1",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-SocialCrawl-Client",
              "value": "template:n8n-track-accounts"
            }
          ]
        },
        "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-6",
      "name": "One row per new post",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        900,
        140
      ],
      "parameters": {
        "jsCode": "const run = $input.first().json.body.runs?.[0];\nreturn (run?.result?.rows ?? []).map((r) => ({ json: {\n  run_at: run.finished_at, account: r.subject, url: r.row.post?.url,\n  published_at: r.row.post?.published_at, likes: r.row.post?.engagement?.likes,\n  comments: r.row.post?.engagement?.comments, views: r.row.post?.engagement?.views,\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-7",
      "name": "Run once: price and create the list job",
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        0,
        360
      ],
      "parameters": {}
    },
    {
      "id": "sc-8",
      "name": "Price the list job (dry run)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        330,
        360
      ],
      "parameters": {
        "method": "POST",
        "url": "https://www.socialcrawl.dev/v1/monitors?dry_run=1",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-SocialCrawl-Client",
              "value": "template:n8n-track-accounts"
            }
          ]
        },
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "{\n  \"recipe\": \"instagram/profile/posts\",\n  \"subjects\": [\n    \"nasa\",\n    \"natgeo\",\n    \"smithsonian\"\n  ],\n  \"cadence\": \"daily\"\n}",
        "options": {
          "timeout": 300000,
          "response": {
            "response": {
              "neverError": true,
              "fullResponse": true
            }
          }
        }
      },
      "credentials": {
        "httpHeaderAuth": {
          "name": "SocialCrawl API"
        }
      }
    },
    {
      "id": "sc-9",
      "name": "How this workflow works",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -40,
        -520
      ],
      "parameters": {
        "content": "## Add the new posts of a list of accounts to a sheet every day\n\nEach run of the list job costs 1 credit per 100 accounts plus one page per account at the feed's price (1 credit on Instagram), so 3 accounts with nothing new cost 4 credits. Reading the run here is free.\n\n**Setup:** run the lower branch once. Read `estimated_cost_per_run` in its output, then delete `?dry_run=1` from the URL and run it again to create the list job. Copy the `monitor.id` it returns into the URL of Read the newest run, and schedule this workflow an hour after the job's `next_run_at`.\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": "Read the newest run",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read the newest run": {
      "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 new post",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "One row per new post": {
      "main": [
        [
          {
            "node": "Append to the sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Run once: price and create the list job": {
      "main": [
        [
          {
            "node": "Price the list job (dry run)",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "pinData": {}
}
