{
  "id": "GlIrAbfX0bjOfWJp",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Instantly Auto Replier",
  "tags": [
    {
      "id": "rOIFigwJWbPW1fP2",
      "name": "MyFlow",
      "createdAt": "2025-06-30T14:14:00.681Z",
      "updatedAt": "2025-06-30T14:14:00.681Z"
    }
  ],
  "nodes": [
    {
      "id": "3539f0c8-32ed-4c01-a239-823471bbf3f1",
      "name": "Webhook",
      "type": "n8n-nodes-base.webhook",
      "position": [
        -640,
        -160
      ],
      "parameters": {
        "path": "NewName",
        "options": {},
        "httpMethod": "POST"
      },
      "typeVersion": 2
    },
    {
      "id": "ec613343-6c15-4e26-8b7b-341542387836",
      "name": "Edit Fields",
      "type": "n8n-nodes-base.set",
      "position": [
        -440,
        -160
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "b3960f99-51bb-4cfb-9bec-0fafebee435e",
              "name": "targetEmail",
              "type": "string",
              "value": "={{ $('Webhook').item.json.body.lead_email }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "6a8c8c70-2635-43a3-b2d2-be64245007d6",
      "name": "Get_ALL_Data",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -240,
        -160
      ],
      "parameters": {
        "url": "https://api.instantly.ai/api/v2/emails",
        "options": {},
        "jsonHeaders": "{\n  \"Authorization\": \"Bearer YOUR_TOKEN_HERE\",\n  \"Content-Type\": \"application/json\"\n}",
        "sendHeaders": true,
        "specifyHeaders": "json"
      },
      "typeVersion": 4.2
    },
    {
      "id": "251808c4-23f2-4370-a15b-8350aac578d4",
      "name": "HTTP Request2",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        -20,
        240
      ],
      "parameters": {
        "url": "https://api.instantly.ai/api/v2/emails/reply",
        "method": "POST",
        "options": {},
        "sendBody": true,
        "sendHeaders": true,
        "bodyParameters": {
          "parameters": [
            {
              "name": "eaccount",
              "value": "={{ $json.eaccount }}"
            },
            {
              "name": "reply_to_uuid",
              "value": "={{ $json.reply_to_uuid }}"
            },
            {
              "name": "subject",
              "value": "={{ $json.subject }}"
            },
            {
              "name": "body",
              "value": "={{ $json.body }}"
            }
          ]
        },
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer YOUR_TOKEN_HERE"
            },
            {
              "name": "Content-Type\t",
              "value": "application/json"
            }
          ]
        }
      },
      "typeVersion": 4.2,
      "alwaysOutputData": true
    },
    {
      "id": "0d973fe7-ef31-48a6-9c2e-500836c5ee5f",
      "name": "Write_message",
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "position": [
        -660,
        240
      ],
      "parameters": {
        "modelId": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1",
          "cachedResultName": "GPT-4.1"
        },
        "options": {},
        "messages": {
          "values": [
            {
              "role": "system",
              "content": "=You are helpful sales assitant, Your job is to reply to emails that you recive\nWrite the reply naturally and professionally without using any h******************* clear language.\n\nAlways return the message content in plain HTMl format like this  \n\"Hey Name<br><br>Thanks for showing your interest<br><br>....\""
            },
            {
              "content": "=Write a******************showing your interest<br><br>....\""
            },
            {
              "content": "=First Name: \"{{ $('Webhook').item.json.body.firstName }}\"\n\nLead's message:\n\"{{ $('Webhook').item.json.body.reply_text }}\""
            }
          ]
        }
      },
      "credentials": {
        "openAiApi": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.8
    },
    {
      "id": "c5c7ec1f-ec9a-4f14-858b-772fb158d37f",
      "name": "Match_values",
      "type": "n8n-nodes-base.code",
      "position": [
        -40,
        -160
      ],
      "parameters": {
        "jsCode": "// Get target email from Webhook or Edit Fields node\nconst targetEmail = $node['Webhook']?.json.lead_email || $node['Edit Fields']?.json.targetEmail;\n\nif (!targetEmail) {\n  console.log('Missing target email from Webhook or Edit Fields node');\n  return [];\n}\n\n// Get all email items array from Get_ALL_Data node\nconst allEmails = $node['Get_ALL_Data']?.json.items;\n\nif (!allEmails || allEmails.length === 0) {\n  console.log('No emails found in Get_ALL_Data node');\n  return [];\n}\n\n// Log for debugging\nconsole.log('Target Email:', targetEmail);\nconsole.log('All leads:', allEmails.map(e => e.lead));\n\n// Find item where lead matches target email (case insensitive, trimmed)\nconst match = allEmails.find(item => \n  item.lead?.toLowerCase().trim() === targetEmail.toLowerCase().trim()\n);\n\nif (!match) {\n  console.log(`No match found for ${targetEmail}`);\n  return [];\n}\n\n// Return matched item as output\nreturn [{ json: match }];\n"
      },
      "typeVersion": 2,
      "alwaysOutputData": false
    },
    {
      "id": "9465f0d1-d95d-4f83-9880-56f056cf43cc",
      "name": "Code1",
      "type": "n8n-nodes-base.code",
      "position": [
        -360,
        240
      ],
      "parameters": {
        "jsCode": "return [{\n  json: {\n    eaccount: $node[\"Match_values\"].json.eaccount,\n    reply_to_uuid: $node[\"Match_values\"].json.id,\n    subject: $node[\"Webhook\"].json.body.reply_subject,\n    body: {\n      text: $node[\"Write_message\"].json.message.content\n    }\n  }\n}];\n"
      },
      "typeVersion": 2
    },
    {
      "id": "cd7d1390-00b4-49a0-8cbf-5ae9c47e496b",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -700,
        -340
      ],
      "parameters": {
        "width": 920,
        "height": 360,
        "content": "# Step-1\n\n## Get Positive Replies from Instantly"
      },
      "typeVersion": 1
    },
    {
      "id": "ab7e207f-eb70-4492-a445-a6ee3d18d260",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -700,
        60
      ],
      "parameters": {
        "color": 5,
        "width": 500,
        "height": 400,
        "content": "# Step-2\n\n## Generate Email Replies to book meetings"
      },
      "typeVersion": 1
    },
    {
      "id": "1e9030ce-3463-4b05-bdce-523ec12b79a9",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -160,
        60
      ],
      "parameters": {
        "color": 7,
        "width": 380,
        "height": 400,
        "content": "# Step-3\n\n## Send Replies Automatically"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "6681d841-821b-43ef-91b8-2dd3426fc410",
  "connections": {
    "Code1": {
      "main": [
        [
          {
            "node": "HTTP Request2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Webhook": {
      "main": [
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Get_ALL_Data",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get_ALL_Data": {
      "main": [
        [
          {
            "node": "Match_values",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Match_values": {
      "main": [
        [
          {
            "node": "Write_message",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Write_message": {
      "main": [
        [
          {
            "node": "Code1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}