{
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "10581005-fea5-4431-b274-0a3e21f3b9e4",
      "name": "On form submission",
      "type": "n8n-nodes-base.formTrigger",
      "position": [
        -336,
        960
      ],
      "parameters": {
        "options": {},
        "formTitle": "topic",
        "formFields": {
          "values": [
            {
              "fieldLabel": "topic",
              "placeholder": "one word is enough"
            }
          ]
        },
        "formDescription": "Auto-Research Assistant for Market Trends"
      },
      "typeVersion": 2.2
    },
    {
      "id": "f6ebbf4f-d246-46cb-86e2-2003827bd764",
      "name": "HTTP Request",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        848,
        1104
      ],
      "parameters": {
        "url": "=https://www.reddit.com/search.json?q={{ $json.keyword }}",
        "options": {},
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "limit",
              "value": "5"
            }
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "e65b0f0b-8711-41c0-9e8c-5b9ab0ab6003",
      "name": "Search Tweets",
      "type": "n8n-nodes-base.twitter",
      "onError": "continueErrorOutput",
      "position": [
        848,
        1296
      ],
      "parameters": {
        "limit": 10,
        "operation": "search",
        "searchText": "={{ $json.topic }}",
        "additionalFields": {
          "sortOrder": "recency"
        }
      },
      "credentials": {
        "twitterOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "2fc2d116-2482-4a91-88d3-c8c98ea1ea1b",
      "name": "Code",
      "type": "n8n-nodes-base.code",
      "position": [
        1152,
        1200
      ],
      "parameters": {
        "jsCode": "// Assume the input is the whole Reddit response JSON as you showed\nconst children = items[0].json.data.children;\n\nreturn children.map(child => {\n  const d = child.data;\n\n  return {\n    json: {\n      title: d.title,\n      url: d.url, // canonical URL\n      subreddit: d.subreddit,\n      score: d.score,\n      permalink: `https://www.reddit.com${d.permalink}`,\n      num_comments: d.num_comments,\n      created_utc: d.created_utc,\n      thumbnail: d.thumbnail\n    }\n  };\n});\n"
      },
      "typeVersion": 2
    },
    {
      "id": "e32ed007-b250-45f4-b98e-7ebdb9cb89c4",
      "name": "Aggregate",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        1696,
        1408
      ],
      "parameters": {
        "options": {},
        "aggregate": "aggregateAllItemData"
      },
      "typeVersion": 1
    },
    {
      "id": "5f6bcc66-8b49-42bc-8fd7-b2e8f3b98a7a",
      "name": "Edit Fields",
      "type": "n8n-nodes-base.set",
      "position": [
        1472,
        1296
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "75eb3f16-86ad-4f26-99e2-9d74b9fb3285",
              "name": "source",
              "type": "string",
              "value": "reddit"
            },
            {
              "id": "6583726f-b8cc-4f78-a453-05e51023fc12",
              "name": "keyword",
              "type": "string",
              "value": "={{ $('Code1').item.json.keyword }}"
            }
          ]
        },
        "includeOtherFields": true
      },
      "typeVersion": 3.4
    },
    {
      "id": "dbe34f3a-c144-4cb4-8d04-e9a3cabd6b9d",
      "name": "Edit Fields1",
      "type": "n8n-nodes-base.set",
      "position": [
        1136,
        1408
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "90a1c2f6-963d-41a7-ac58-c22ea509f8c1",
              "name": "source",
              "type": "string",
              "value": "x (formally twitter)"
            },
            {
              "id": "cb0e321f-0598-4011-831e-3c223432659c",
              "name": "text",
              "type": "string",
              "value": "={{ $json.text }}"
            },
            {
              "id": "80872f05-f26a-435c-82ff-617a3b43ca79",
              "name": "keyword",
              "type": "string",
              "value": "={{ $('Code1').item.json.keyword }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "3353b3e1-5923-4da4-acdc-1562ecb4056e",
      "name": "Aggregate1",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        1456,
        1504
      ],
      "parameters": {
        "options": {},
        "aggregate": "aggregateAllItemData"
      },
      "typeVersion": 1
    },
    {
      "id": "05771ef7-456b-4b40-99bc-25870f7fba0d",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "position": [
        1984,
        1808
      ],
      "parameters": {},
      "typeVersion": 3.2
    },
    {
      "id": "1beebde2-ecf1-4daa-b83b-e55c766a97de",
      "name": "AI Agent1",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -80,
        960
      ],
      "parameters": {
        "text": "=You are a keyword research assistant. \n\nGiven the following topic: \"{{ $json.topic }}\", do the following:\n\n1. Expand the topic into 5 subtopics that people are actively discussing or searching for.\n2. For each subtopic, generate 2 SEO-friendly, trend-relevant keywords.\n3. Ensure the keywords are concise, clear, and commonly searched terms.\n4. Present the output in the following JSON format:\n\n{\n  \"topic\": \"{{ $json.topic}}\",\n  \"subtopics\": [\n    {\n      \"name\": \"Subtopic 1\",\n      \"keywords\": [\"keyword1\", \"keyword2\"]\n    },\n    ...\n  ]\n}\n",
        "options": {},
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 2
    },
    {
      "id": "917b3b79-81a6-4ea3-a7d5-04eeda633622",
      "name": "Structured Output Parser",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        16,
        1184
      ],
      "parameters": {
        "autoFix": true,
        "jsonSchemaExample": "{\n  \"topic\": \"{{topic}}\",\n  \"subtopics\": [\n    {\n      \"name\": \"Subtopic 1\",\n      \"keywords\": [\"keyword1\", \"keyword2\"]\n    }\n  ]\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "dde05994-e310-41f4-8c35-206f370cb8c2",
      "name": "Google Gemini Chat Model1",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        96,
        1392
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "5f1f5331-4c72-4508-a202-beffb066d57c",
      "name": "Google Gemini Chat Model2",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        -112,
        1184
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "391afab1-444d-4ef2-9cf3-6cf4bdd1ac86",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        608,
        960
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "a5e9ad5d-6b40-4a14-ba8e-566e6c28f5dd",
      "name": "Code1",
      "type": "n8n-nodes-base.code",
      "position": [
        384,
        960
      ],
      "parameters": {
        "jsCode": "const subtopics = $json.output.subtopics || [];\n\nconst keywords = subtopics.flatMap(sub => sub.keywords);\n\nreturn keywords.map(kw => {\n  return {\n    json: {\n      keyword: kw\n    }\n  };\n});\n"
      },
      "typeVersion": 2
    },
    {
      "id": "c301ca14-1ffc-41f5-a8c0-cf9660564f4e",
      "name": "Split Out",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        1664,
        960
      ],
      "parameters": {
        "include": "allOtherFields",
        "options": {},
        "fieldToSplitOut": "data[0].keyword"
      },
      "typeVersion": 1
    },
    {
      "id": "073ab345-b33a-4f44-acb3-966bc1a243a4",
      "name": "AI Agent",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        2496,
        1152
      ],
      "parameters": {
        "text": "=You are a research analyst and trend expert.\n\nEvaluate the following content item and provide insights that help decide if this is useful for creating trend-driven content, educational summaries, or platform-specific posts.  \nIf a field like \"upvotes\" or \"comments\" is 0, ignore it when analyzing value.\n\n---\nContent Details:\nTitle: {{ $json.data[0].title }}\nText: {{ $json[\"text\"] || \"No text content available.\" }}\nURL: {{ $json.data[0].url }}\nSubreddit: {{ $json.data[0].subreddit }}\nUpvotes: {{ $json.data[0].score }}\nComments: {{ $json.data[0].num_comments }}\nSource: {{ $json.data[0].source }}\nPermalink: {{ $json.data[0].permalink }}\nKeyword: {{ $('Code1').item.json.keyword }}\n---\n\nBased on this:\n\n1. What is this post about in 1 sentence?\n2. Is this content useful or interesting for a broader audience? Why?\n3. Who would benefit most from this (e.g., marketer, founder, consumer)?\n4. Does this have trend potential? Give a score out of 100.\n5. Suggest a content format this could be turned into (e.g., Instagram reel, LinkedIn post, carousel, newsletter blurb).\n6. Tag this with 1\u20132 topic categories (like: automation, AI agents, marketing trends, consumer behavior).\n7. Suggest 1 clear keyword this content ranks for or is relevant to.\n\nReturn the result in this exact JSON format:\n{\n  \"summary\": \"...\",\n  \"audience\": \"...\",\n  \"trend_score\": 85,\n  \"format_suggestion\": \"...\",\n  \"categories\": [\"...\", \"...\"],\n  \"keyword\": \"...\"\n}\n",
        "options": {},
        "promptType": "define",
        "hasOutputParser": true
      },
      "typeVersion": 2
    },
    {
      "id": "66539b94-883b-412e-bf2f-62ce2ac4e82e",
      "name": "Loop Over Items1",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        2288,
        944
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "17f77c57-5c38-4fe8-803c-3b328a1cbcf9",
      "name": "Google Gemini Chat Model",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        2496,
        1376
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "24f5b926-d950-4eca-9129-dcedec256762",
      "name": "Structured Output Parser1",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        2592,
        1376
      ],
      "parameters": {
        "autoFix": true,
        "jsonSchemaExample": "{\n  \"summary\": \"...\",\n  \"audience\": \"...\",\n  \"trend_score\": 85,\n  \"format_suggestion\": \"...\",\n  \"categories\": [\"...\", \"...\"],\n  \"keyword\": \"...\"\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "bf870ecd-eeb5-4a0e-b766-0634d1b14450",
      "name": "Google Gemini Chat Model4",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        2592,
        1536
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "19009c00-e0bf-43cd-9b2f-647ca4e44b9b",
      "name": "AI Agent3",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        3040,
        928
      ],
      "parameters": {
        "text": "=You are a strategic trend analyst.\n\nYou will be given a list of analyzed content items. Each item contains insights like summary, audience, trend score, categories, and format suggestions.\n\nYour goal is to:\n1. Review all the items.\n2. Select the **top 3\u20135 items** based on **trend_score, audience relevance, and uniqueness**.\n3. Group them into **final themes or topics** if multiple posts relate to the same trend.\n4. Suggest 1\u20132 **content strategy ideas** based on these findings (e.g., \"Create a content series on online dating apps\" or \"Make a video breakdown of AI productivity tools\").\n5. Return a final summary of your recommendation.\n\nHere is the content batch:\n\n{{ JSON.stringify($json) }}\n\nRespond in this JSON format:\n\n{\n  \"final_trends\": [\n    {\n      \"title\": \"Best Dating Apps in 2024\",\n      \"reason\": \"High engagement and wide appeal to both users and dating app makers\",\n      \"recommended_format\": \"LinkedIn carousel or Blog article\",\n      \"category\": \"Online Dating\"\n    },\n    {\n      \"title\": \"AI Replacing Jobs\",\n      \"reason\": \"Strong emotional impact, relevant for general and professional audience\",\n      \"recommended_format\": \"Instagram Reel or YouTube Short\",\n      \"category\": \"Future of Work\"\n    }\n  ],\n  \"strategy_recommendations\": [\n    \"Create a trend spotlight series on social platforms using content with high trend scores and emotional relevance.\",\n    \"Develop content clusters based on categories like 'AI' and 'Consumer Tech' to improve reach and niche authority.\"\n  ],\n  \"summary\": \"These trends reflect current user interests across Reddit and other platforms, particularly in AI disruption and online lifestyle tools. The content formats suggested match the platforms where these topics are actively discussed.\"\n}\n",
        "options": {},
        "promptType": "define"
      },
      "typeVersion": 2
    },
    {
      "id": "d30344f4-71ea-47b7-bcdb-a9a92066bb69",
      "name": "Google Gemini Chat Model5",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        3040,
        1104
      ],
      "parameters": {
        "options": {}
      },
      "credentials": {
        "googlePalmApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "1c07bcc4-7dce-4813-b7a7-3738699938dd",
      "name": "Code2",
      "type": "n8n-nodes-base.code",
      "position": [
        3328,
        928
      ],
      "parameters": {
        "jsCode": "const items = [];\n\nfor (const item of $input.all()) {\n  const rawOutput = item.json.output;\n\n  // Remove code block markers (e.g., ```json\\n ... \\n```)\n  const cleanedJsonString = rawOutput\n    .replace(/^```json\\s*/i, '') // remove opening ```json\n    .replace(/```$/i, '')        // remove closing ```\n    .trim();\n\n  // Parse the cleaned JSON string\n  const parsed = JSON.parse(cleanedJsonString);\n\n  // Push structured output to be used downstream\n  items.push({ json: parsed });\n}\n\nreturn items;\n"
      },
      "typeVersion": 2
    },
    {
      "id": "e225cc5d-cabc-48be-a3df-09c0fb9d005a",
      "name": "add more source",
      "type": "n8n-nodes-base.noOp",
      "position": [
        864,
        1776
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "b9404d81-d02e-4c1d-a578-8dd0f17c9b22",
      "name": "add source",
      "type": "n8n-nodes-base.noOp",
      "position": [
        864,
        1568
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "2c292953-9e76-434e-a13a-ff82576a947a",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        816,
        1504
      ],
      "parameters": {
        "color": 7,
        "width": 288,
        "height": 432,
        "content": "## add more platforms"
      },
      "typeVersion": 1
    },
    {
      "id": "7243bf39-f240-4992-8fa3-e0c5e7c1b2e6",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1072,
        928
      ],
      "parameters": {
        "width": 624,
        "height": 496,
        "content": "## Trend Analyzer & Content Strategist\n\n### How it works\n1.  Submit a topic through the workflow's form.\n2.  An AI model expands the topic into relevant subtopics and generates associated keywords.\n3.  The workflow searches Reddit and X (formerly Twitter) for posts related to these keywords.\n4.  Each relevant post is analyzed by an AI for its trend potential, target audience, and suitable content formats.\n5.  A final AI agent synthesizes these analyses, identifies top trends, groups them into themes, and suggests strategic content ideas.\n\n### Setup\n- [ ] Connect your Google Gemini API account.\n- [ ] Connect your X (formerly Twitter) account.\n- [ ] Ensure the \\\"On form submission\\\" trigger is active and accessible.\n- [ ] (Optional) Customize the form fields for specific inputs."
      },
      "typeVersion": 1
    },
    {
      "id": "8925f592-c2b1-4598-b481-2ef6799e6fda",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -384,
        896
      ],
      "parameters": {
        "color": 7,
        "width": 880,
        "height": 624,
        "content": "## 1. get topic from user to split into more sub topics"
      },
      "typeVersion": 1
    },
    {
      "id": "583bf19a-b576-46ab-8c65-6ef9d5a340f4",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        528,
        864
      ],
      "parameters": {
        "color": 7,
        "width": 1616,
        "height": 1232,
        "content": "## 2. do market research from different platform"
      },
      "typeVersion": 1
    },
    {
      "id": "8cd82279-f569-4413-8189-379213334d70",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2192,
        848
      ],
      "parameters": {
        "color": 7,
        "width": 720,
        "height": 832,
        "content": "## 3. summarize posts"
      },
      "typeVersion": 1
    },
    {
      "id": "afb51b37-20a4-4972-83b3-8f85b7a41c73",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2944,
        848
      ],
      "parameters": {
        "color": 7,
        "width": 576,
        "height": 400,
        "content": "## 4. analyze the trend"
      },
      "typeVersion": 1
    }
  ],
  "connections": {
    "Code": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Code1": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Merge": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent1": {
      "main": [
        [
          {
            "node": "Code1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent3": {
      "main": [
        [
          {
            "node": "Code2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Out": {
      "main": [
        [
          {
            "node": "Loop Over Items1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Aggregate1": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Aggregate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields1": {
      "main": [
        [
          {
            "node": "Aggregate1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "HTTP Request": {
      "main": [
        [
          {
            "node": "Code",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Tweets": {
      "main": [
        [
          {
            "node": "Edit Fields1",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Loop Over Items": {
      "main": [
        [
          {
            "node": "Split Out",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "HTTP Request",
            "type": "main",
            "index": 0
          },
          {
            "node": "Search Tweets",
            "type": "main",
            "index": 0
          },
          {
            "node": "add more source",
            "type": "main",
            "index": 0
          },
          {
            "node": "add source",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items1": {
      "main": [
        [
          {
            "node": "AI Agent3",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "On form submission": {
      "main": [
        [
          {
            "node": "AI Agent1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser": {
      "ai_outputParser": [
        [
          {
            "node": "AI Agent1",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "Structured Output Parser",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model2": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent1",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model4": {
      "ai_languageModel": [
        [
          {
            "node": "Structured Output Parser1",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model5": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent3",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser1": {
      "ai_outputParser": [
        [
          {
            "node": "AI Agent",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    }
  }
}