{
  "id": "dq7nH5WlVgIT55Hb",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "AI Content Generator with Auto Pexels Image Matching",
  "tags": [],
  "nodes": [
    {
      "id": "08ee8372-2a74-43a7-bf7c-36e2bc1e9b99",
      "name": "Pexels Image Search",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        928,
        -144
      ],
      "parameters": {
        "url": "https://api.pexels.com/v1/search",
        "options": {},
        "sendQuery": true,
        "sendHeaders": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "query",
              "value": "={{ $json.output[0].content[0].text }}"
            },
            {
              "name": "per_page",
              "value": "5"
            },
            {
              "name": "orientation",
              "value": "landscape"
            }
          ]
        },
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "={{ INSERT YOUR PEXELS API KEY HERE }}"
            }
          ]
        }
      },
      "typeVersion": 4.3
    },
    {
      "id": "a3c48b24-cc90-40c8-a8c6-5441c6fa5c1b",
      "name": "Create Content with AI",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        208,
        -144
      ],
      "parameters": {
        "text": "=Create a {{ $json['Content Type'] }} about: {{ $json['Topic or Keyword'] }} \nTone: {{ $json.Tone }}\nLength: {{ $json['Content Length'] }} words\nRemember to respond with valid JSON only.",
        "options": {
          "systemMessage": "=You are a professional content writer. Generate high-quality content based on user requirements. Your response MUST be valid JSON with this exact structure:\n\nTitle: Engaging title here\nContent: Main content with multiple paragraphs \n\nContent should be engaging, well-structured, and match the requested tone. Use proper grammar and formatting."
        },
        "promptType": "define"
      },
      "typeVersion": 3
    },
    {
      "id": "0cd1c513-9fa3-41dd-85ec-a682bb7fd0f6",
      "name": "Find suitable content keyword",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        576,
        -144
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini",
          "cachedResultName": "GPT-4.1-MINI"
        },
        "options": {},
        "responses": {
          "values": [
            {
              "role": "system",
              "content": "=You are an expert for image keyword finder. Give one keyword to the Pexels, that suitable with this:\n\n{{ $json.output }}\n\nGive only the keyword, no explanation."
            }
          ]
        },
        "builtInTools": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2
    },
    {
      "id": "987a945c-52f8-4a31-a775-d4164e9c2cd6",
      "name": "Create Suitable Content Including Image",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        1248,
        -144
      ],
      "parameters": {
        "text": "test",
        "options": {
          "systemMessage": "=You are an expert in {{ $('On form submission').item.json['Content Type'] }} and develop beautiful HTML posting.\n\nFrom below info create the {{ $('On form submission').item.json['Topic or Keyword'] }} in HTML format.\n\n\n{{ $('Create Content with AI').item.json.output }}\n\nThis is the URL Image: {{ $json.photos[0].url }}\n\n** Just give the ouput no explanation.\n** Don't start the HTML with ```html\n"
        },
        "promptType": "define"
      },
      "typeVersion": 3
    },
    {
      "id": "9fdc71f7-28f1-4514-af43-6e1d4afcc96b",
      "name": "Extract Content and Image Keyword",
      "type": "n8n-nodes-base.code",
      "position": [
        304,
        368
      ],
      "parameters": {
        "jsCode": "const items = $input.all();\nconst out = [];\n\nfor (const item of items) {\n  let raw = item.json.output;\n\n  raw = raw.trim();\n  if (raw.startsWith('```json')) {\n    raw = raw.substring(7);\n  } else if (raw.startsWith('```')) {\n    raw = raw.substring(3);\n  }\n  \n  if (raw.endsWith('```')) {\n    raw = raw.substring(0, raw.length - 3);\n  }\n  raw = raw.trim();\n\n  const parsed = JSON.parse(raw);\n\n  out.push({\n    json: {\n      Title: parsed.Title,\n      Content: parsed.Content,\n      Image_keyword: parsed.Image_keyword,\n    }\n  });\n}\n\nreturn out;\n"
      },
      "typeVersion": 2
    },
    {
      "id": "9217d3c5-b98b-47c9-8bca-0fe7feb4cdf9",
      "name": "View the Result",
      "type": "n8n-nodes-base.html",
      "position": [
        1808,
        -144
      ],
      "parameters": {
        "html": "{{ $json.output }}"
      },
      "typeVersion": 1.2
    },
    {
      "id": "9594253b-72fb-4143-a8ee-b14d1de3770b",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -656,
        -464
      ],
      "parameters": {
        "width": 512,
        "height": 752,
        "content": "## AI Content Generator with Auto Pexels Image Matching\n\n### How it works\n1. Submit your content topic or keyword through the form.\n2. AI will generate initial content based on your input.\n3. The AI extracts suitable keywords from the generated content.\n4. The keywords are sent to Pexels API to search for relevant royalty-free images.\n5. AI creates the final polished content that matches the selected image.\n6. The result is displayed as a formatted HTML template with content and image combined.\n\n### How to Set Up\n1. Get a OpenAI API key from [https://platform.openai.com](https://platform.openai.com).\n2. Get a free Pexels API key from [https://www.pexels.com/api](https://www.pexels.com/api). You can make up to 200 requests per hour.\n3. Insert your OpenAI API key in the \"Create Content with AI\" and \"Create Suitable Content\" nodes.\n4. Insert your Pexels API key in the \"Pexels Image Search\" node in the Authorization header.\n\n### Customize\n1. **Optimize token usage**: If you are an advanced user, use the floating node in the \"Alternative way to optimize token usage\" section to process everything in one prompt and minimize API costs. If you use this option, update the expressions in the \"Pexels Image Search\" node and \"Create Suitable Content Including Image\" node to reference the extracted contents and keywords from the JS node.\n2. If you want deeper content analysis, change the model to a more advanced one, for example GPT-4.1, GPT-5.1, or GPT-5.2."
      },
      "typeVersion": 1
    },
    {
      "id": "705d85d0-a6d9-47d4-9c8f-a41d1ee48aca",
      "name": "On form submission",
      "type": "n8n-nodes-base.formTrigger",
      "position": [
        -32,
        -144
      ],
      "parameters": {
        "options": {},
        "formTitle": "AI Content Generator",
        "formFields": {
          "values": [
            {
              "fieldLabel": "Topic or Keyword",
              "placeholder": "Example: Fitness, Business Tips"
            },
            {
              "fieldType": "dropdown",
              "fieldLabel": "Content Type",
              "fieldOptions": {
                "values": [
                  {
                    "option": "Blog Post"
                  },
                  {
                    "option": "Social Media Post"
                  },
                  {
                    "option": "Landing Page"
                  },
                  {
                    "option": "Email Newsletter"
                  }
                ]
              }
            },
            {
              "fieldType": "dropdown",
              "fieldLabel": "Tone",
              "fieldOptions": {
                "values": [
                  {
                    "option": "Professional"
                  },
                  {
                    "option": "Casual"
                  },
                  {
                    "option": "Friendly"
                  },
                  {
                    "option": "Educational"
                  }
                ]
              }
            },
            {
              "fieldType": "dropdown",
              "fieldLabel": "Content Length",
              "fieldOptions": {
                "values": [
                  {
                    "option": "Short"
                  },
                  {
                    "option": "Medium"
                  },
                  {
                    "option": "Long"
                  }
                ]
              }
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "ecf2e07b-a940-4b39-818a-184d03922b59",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -96,
        -272
      ],
      "parameters": {
        "color": 7,
        "width": 608,
        "height": 448,
        "content": "## 1.Create RAW content from the the user's input"
      },
      "typeVersion": 1
    },
    {
      "id": "aa56198c-6e04-4b2e-b2b0-5df87c0655bc",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        560,
        -272
      ],
      "parameters": {
        "color": 7,
        "width": 528,
        "height": 320,
        "content": "## 2. Generate suitable image for the content"
      },
      "typeVersion": 1
    },
    {
      "id": "5313814c-13a4-448d-9acf-ceda614c4725",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1136,
        -272
      ],
      "parameters": {
        "color": 7,
        "width": 448,
        "height": 464,
        "content": "## 3. Regenerate the content with picture and HTML format"
      },
      "typeVersion": 1
    },
    {
      "id": "e8113386-da96-425c-bb59-446ef8af0df2",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1632,
        -272
      ],
      "parameters": {
        "color": 7,
        "width": 448,
        "height": 320,
        "content": "## 4. Get the results"
      },
      "typeVersion": 1
    },
    {
      "id": "18834b61-2e26-4f30-9186-c5f65a9fa1f0",
      "name": "Sticky Note5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -80,
        304
      ],
      "parameters": {
        "color": 7,
        "width": 608,
        "height": 448,
        "content": "## Alternative way to optimize token usage"
      },
      "typeVersion": 1
    },
    {
      "id": "667a4a51-b737-4e23-a3ba-fbaa46c9fa29",
      "name": "Create Content and Find Suitable Image",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -16,
        368
      ],
      "parameters": {
        "text": "=Create a {{ $json['Content Type'] }} about: {{ $json['Topic or Keyword'] }} \nTone: {{ $json.Tone }}\nLength: {{ $json['Content Length'] }} words\nRemember to respond with valid JSON only.",
        "options": {
          "systemMessage": "=You are a professional content writer. Generate high-quality content based on user requirements. Your response MUST be valid JSON with this exact structure:\n\nTitle: Engaging title here\nContent: Main content with multiple paragraphs \nImage_keyword: Give one word suitable keyword to send to Pexels API\n\nContent should be engaging, well-structured, and match the requested tone. Use proper grammar and formatting."
        },
        "promptType": "define"
      },
      "typeVersion": 3
    },
    {
      "id": "6d5962ea-1fe2-4912-9fb9-c13554bb753d",
      "name": "OpenAI 4.1 mini",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        208,
        48
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini"
        },
        "options": {},
        "builtInTools": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "7dde863b-2bfc-4662-a243-6b08116af161",
      "name": "OpenAi 4.1 Mini",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        1248,
        64
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini"
        },
        "options": {},
        "builtInTools": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.3
    },
    {
      "id": "98788352-0fc3-4a06-ba84-24dd0343b6da",
      "name": "OpenAI 4.1 mini for Content and Image",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        -16,
        560
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini"
        },
        "options": {},
        "builtInTools": {}
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.3
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "e7a1190c-4094-4153-a335-a1bfd3524328",
  "connections": {
    "OpenAI 4.1 mini": {
      "ai_languageModel": [
        [
          {
            "node": "Create Content with AI",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "OpenAi 4.1 Mini": {
      "ai_languageModel": [
        [
          {
            "node": "Create Suitable Content Including Image",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "On form submission": {
      "main": [
        [
          {
            "node": "Create Content with AI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Pexels Image Search": {
      "main": [
        [
          {
            "node": "Create Suitable Content Including Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Content with AI": {
      "main": [
        [
          {
            "node": "Find suitable content keyword",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find suitable content keyword": {
      "main": [
        [
          {
            "node": "Pexels Image Search",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Content and Image Keyword": {
      "main": [
        []
      ]
    },
    "OpenAI 4.1 mini for Content and Image": {
      "ai_languageModel": [
        [
          {
            "node": "Create Content and Find Suitable Image",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Create Content and Find Suitable Image": {
      "main": [
        [
          {
            "node": "Extract Content and Image Keyword",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Suitable Content Including Image": {
      "main": [
        [
          {
            "node": "View the Result",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}