{
  "name": "SocialCrawl: Add new Reddit posts for a list of terms to a sheet, free when nothing is new",
  "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 search terms",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        220,
        0
      ],
      "parameters": {
        "jsCode": "return [\"solar eclipse\",\"aurora forecast\"].map((value) => ({ json: { value } }));"
      }
    },
    {
      "id": "sc-6",
      "name": "Search Reddit",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        440,
        0
      ],
      "parameters": {
        "method": "GET",
        "url": "=https://www.socialcrawl.dev/v1/reddit/search?query={{ encodeURIComponent($json.value) }}&sort=new&seen=n8n-keyword-watch",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "X-SocialCrawl-Client",
              "value": "template:n8n-keyword-search"
            }
          ]
        },
        "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  term: $('Your search terms').all()[i]?.json.value, published_at: r.post.published_at,\n  url: r.post.url, text: r.post.content?.text, likes: r.post.engagement?.likes, comments: r.post.engagement?.comments,\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 new Reddit posts for a list of terms to a sheet, free when nothing is new\n\nEach term costs 1 credit a page. With seen, a page of posts you already received is free, and a page with some new posts is billed in proportion.\n\n**Setup:** put your terms in Your search terms, pick your Google Sheets credential and sheet in Append to the sheet, and turn the workflow on. Keep one `seen` name per workflow: posts already received under it are left out for 24 hours.\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 search terms",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Your search terms": {
      "main": [
        [
          {
            "node": "Search Reddit",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Reddit": {
      "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": {}
}
