{
  "name": "Create Pipedrive deals from website chatbot leads using OpenAI",
  "nodes": [
    {
      "parameters": {
        "content": "## Create Pipedrive deals from website chatbot leads using OpenAI\n\nFor sales teams that want interested website visitors to arrive in Pipedrive as a person with a linked deal and a note.\n\n### How it works\n1. A webhook receives the widget message and checks a shared secret.\n2. An OpenAI agent answers from your knowledge text and saves a lead once it has an email and a reason.\n3. The capture lead tool finds or creates the person, creates a deal in your chosen stage and adds a note with interest, intent, objection and summary.\n4. The agent calls it once per conversation, because every call creates a new deal.\n\n### Setup\n- [ ] Edit **Chatbot settings**: company name, your knowledge text and a random `webhook_secret`.\n- [ ] Add your OpenAI credential to **GPT-4.1 mini model**.\n- [ ] Copy each tool section below the main flow into its own new workflow, save it, then select it in the matching tool node.\n- [ ] In each tool workflow, add your Pipedrive credential and fill in **Tool settings**.\n- [ ] Test with a POST to the webhook Test URL (the n8n chat panel needs a Chat Trigger). Then activate and add the production URL to your widget, with the same secret as `metadata.webhook_secret`.\n\n### Requirements\nn8n, an OpenAI API key and a Pipedrive API token, company domain and pipeline stage ID.\n\n### Customization tips\nChange the deal title, stage or note format, or add custom fields.",
        "width": 480,
        "height": 1104
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -560,
        0
      ],
      "id": "d272dc2f-2c81-4231-a62d-807dcca0c0fa",
      "name": "Template overview"
    },
    {
      "parameters": {
        "content": "## 1. Receive and verify the message\nThe webhook takes the widget message. Requests without your secret are rejected.",
        "width": 944,
        "height": 432,
        "color": 7
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        0
      ],
      "id": "fbda1b2d-a596-42c6-a7e0-83ac6284618e",
      "name": "Section 1: Receive and verify the message"
    },
    {
      "parameters": {
        "content": "## 2. Answer with the AI agent\nThe agent answers from your settings, remembers the session and calls its tools when needed.",
        "width": 704,
        "height": 512,
        "color": 7
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        528
      ],
      "id": "f4993ec4-5e39-4c0e-8eaf-d8cabbe9fd51",
      "name": "Section 2: Answer with the AI agent"
    },
    {
      "parameters": {
        "content": "## 3. Tool: capture_lead (copy to its own workflow)\nCopy these nodes into a new workflow and save it. Then select that workflow in the capture_lead tool node.",
        "width": 2864,
        "height": 576,
        "color": 7
      },
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        1136
      ],
      "id": "c8419353-b9fc-47d7-9881-c0f4eb9d328b",
      "name": "Section 3: Tool: capture_lead (copy to its own workflow)"
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "pipedrive-lead-capture",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook",
      "name": "Chat webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        64,
        112
      ],
      "webhookId": "pipedrive-lead-capture-webhook"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "c1",
              "name": "company_name",
              "value": "Your Company",
              "type": "string"
            },
            {
              "id": "c2",
              "name": "knowledge",
              "value": "PASTE YOUR FAQ OR SERVICES COPY HERE.\n\nThe bot answers only from this text.",
              "type": "string"
            },
            {
              "id": "c3",
              "name": "webhook_secret",
              "value": "CHANGE_ME_TO_A_RANDOM_STRING",
              "type": "string"
            },
            {
              "id": "c4",
              "name": "message",
              "value": "={{ $json.body.chatInput || $json.body.message || '' }}",
              "type": "string"
            },
            {
              "id": "c5",
              "name": "session_id",
              "value": "={{ $json.body.sessionId || 'no-session' }}",
              "type": "string"
            },
            {
              "id": "c6",
              "name": "page_url",
              "value": "={{ $json.body.metadata?.page_url || '' }}",
              "type": "string"
            },
            {
              "id": "c7",
              "name": "sent_secret",
              "value": "={{ $json.body.metadata?.webhook_secret || (typeof $json.body.metadata === 'string' ? ($json.body.metadata.match(/\"webhook_secret\"\\s*:\\s*\"([^\"]*)\"/) || [])[1] : '') || '' }}",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "config",
      "name": "Chatbot settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        304,
        112
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "sec1",
              "leftValue": "={{ $json.sent_secret }}",
              "rightValue": "={{ $json.webhook_secret }}",
              "operator": {
                "type": "string",
                "operation": "equals"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "check-secret",
      "name": "Request from your widget?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        544,
        112
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ output: 'Sorry, something went wrong. Please try again later.' }) }}",
        "options": {
          "responseCode": 401
        }
      },
      "id": "reject",
      "name": "Reject unknown requests",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        784,
        272
      ]
    },
    {
      "parameters": {
        "promptType": "define",
        "text": "={{ $json.message }}",
        "options": {
          "systemMessage": "=You are the assistant on {{ $json.company_name }}'s website. Answer visitor questions accurately and, where appropriate, capture their details as a lead in the sales pipeline.\n\nWHAT YOU KNOW\n{{ $json.knowledge }}\n\nANSWERING\n- Answer only from the information above. Never invent pricing, availability, or capability.\n\nCAPTURING DETAILS\n- Do not ask for an email in your first reply. Answer their question first.\n- Ask only when there is a reason: they cannot be answered, want a demo, or ask to be contacted.\n- Once you have an email and a reason to save it, call capture_lead. Call it only ONCE per conversation - it creates a new deal every time it's called, so don't call it again just because the visitor keeps chatting.\n\nTAGS - choose only from these values\n- interest: pricing, services, integrations, demo, support, partnership, careers\n- intent: hot, warm, browsing\n- objection: price, timing, competitor, technical-fit, authority (leave blank if none apparent)\n\nAlways pass session_id \"{{ $json.session_id }}\" and page_url \"{{ $json.page_url }}\".\n\nTONE\n- Short. Plain. No exclamation marks."
        }
      },
      "id": "agent",
      "name": "Answer questions and capture leads",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "typeVersion": 1.7,
      "position": [
        64,
        640
      ]
    },
    {
      "parameters": {
        "model": "gpt-4.1-mini",
        "options": {}
      },
      "id": "model",
      "name": "GPT-4.1 mini model",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "typeVersion": 1.2,
      "position": [
        64,
        880
      ]
    },
    {
      "parameters": {
        "sessionIdType": "customKey",
        "sessionKey": "={{ $('Chatbot settings').item.json.session_id }}",
        "contextWindowLength": 20
      },
      "id": "memory",
      "name": "Conversation memory",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "typeVersion": 1.3,
      "position": [
        304,
        880
      ]
    },
    {
      "parameters": {
        "name": "capture_lead",
        "description": "Save a lead to Pipedrive as a Person with a linked Deal. Call ONCE per conversation, when you have an email and a reason to save it - calling it again creates a duplicate deal, it does not update the existing one.",
        "workflowId": {
          "__rl": true,
          "mode": "list",
          "value": "PASTE_THE_CAPTURE_LEAD_WORKFLOW_ID_HERE",
          "cachedResultName": "capture_lead (Pipedrive)"
        },
        "specifyInputSchema": true,
        "jsonSchemaExample": "{\n  \"email\": \"sam@example.com\",\n  \"name\": \"Sam\",\n  \"interest\": \"services\",\n  \"intent\": \"hot\",\n  \"objection\": \"\",\n  \"summary\": \"Asked about X. Wants a callback.\",\n  \"session_id\": \"\",\n  \"page_url\": \"\"\n}"
      },
      "id": "tool-capture",
      "name": "capture_lead",
      "type": "@n8n/n8n-nodes-langchain.toolWorkflow",
      "typeVersion": 1.2,
      "position": [
        544,
        880
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ JSON.stringify({ output: $json.output }) }}",
        "options": {}
      },
      "id": "respond",
      "name": "Respond to widget",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        544,
        640
      ]
    },
    {
      "parameters": {
        "inputSource": "passthrough"
      },
      "id": "sub-trigger",
      "name": "When called by the agent",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "typeVersion": 1.1,
      "position": [
        64,
        1388
      ]
    },
    {
      "parameters": {
        "jsCode": "const q = $input.first().json.query ?? $input.first().json;\nconst d = typeof q === 'string' ? JSON.parse(q) : q;\n\nconst email = String(d.email || '').trim().toLowerCase();\nconst valid = /^[^@\\s]+@[^@\\s]+\\.[^@\\s]{2,}$/.test(email);\n\nreturn [{ json: {\n  valid,\n  email,\n  name: d.name || email,\n  interest: d.interest || '',\n  intent: d.intent || 'browsing',\n  objection: d.objection || '',\n  summary: d.summary || '',\n  session_id: d.session_id || '',\n  page_url: d.page_url || ''\n} }];"
      },
      "id": "parse-input",
      "name": "Parse and validate",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        284,
        1388
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "v1",
              "leftValue": "={{ $json.valid }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "if-valid",
      "name": "Valid email?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        504,
        1388
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "e1",
              "name": "response",
              "value": "That email address doesn't look valid. Ask them to confirm it, then try again. Nothing has been saved.",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "invalid-response",
      "name": "Tell the agent it failed",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        724,
        1548
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "s1",
              "name": "pipedrive_domain",
              "value": "yourcompany",
              "type": "string"
            },
            {
              "id": "s2",
              "name": "pipeline_stage_id",
              "value": "PASTE_YOUR_STAGE_ID_HERE",
              "type": "string"
            }
          ]
        },
        "includeOtherFields": true,
        "options": {}
      },
      "id": "sub-config",
      "name": "Tool settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        724,
        1248
      ]
    },
    {
      "parameters": {
        "url": "={{ 'https://' + $json.pipedrive_domain + '.pipedrive.com/api/v1/persons/search' }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpQueryAuth",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "term",
              "value": "={{ $json.email }}"
            },
            {
              "name": "fields",
              "value": "email"
            },
            {
              "name": "exact_match",
              "value": "true"
            }
          ]
        },
        "options": {}
      },
      "id": "search-person",
      "name": "Search for existing person",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        944,
        1248
      ]
    },
    {
      "parameters": {
        "jsCode": "const cfg = $('Tool settings').first().json;\nconst body = $input.first().json;\nconst items = body?.data?.items || [];\n\nreturn [{ json: {\n  ...cfg,\n  existing_person_id: items.length > 0 ? items[0].item.id : null\n} }];"
      },
      "id": "check-person-found",
      "name": "Did we find a person?",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1164,
        1248
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "loose",
            "version": 2
          },
          "conditions": [
            {
              "id": "pf1",
              "leftValue": "={{ !!$json.existing_person_id }}",
              "rightValue": true,
              "operator": {
                "type": "boolean",
                "operation": "true",
                "singleValue": true
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "id": "if-person-found",
      "name": "Person already exists?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        1384,
        1248
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ 'https://' + $json.pipedrive_domain + '.pipedrive.com/api/v1/persons' }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpQueryAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ name: $json.name, email: [{ value: $json.email, primary: true }] }) }}",
        "options": {}
      },
      "id": "create-person",
      "name": "Create new person",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1604,
        1348
      ]
    },
    {
      "parameters": {
        "jsCode": "// Normalise both branches (existing person found / new person created) into\n// one shape before creating the deal.\nconst cfg = $('Did we find a person?').first().json;\nconst wasExisting = !!cfg.existing_person_id;\n\nlet personId;\nif (wasExisting) {\n  personId = cfg.existing_person_id;\n} else {\n  const created = $input.first().json;\n  personId = created?.data?.id;\n}\n\nif (!personId) {\n  throw new Error('Pipedrive did not return a person id from either the search or the create call. Check the API token and domain.');\n}\n\nreturn [{ json: { ...cfg, person_id: personId } }];"
      },
      "id": "extract-person-id",
      "name": "Extract person ID",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        1824,
        1248
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ 'https://' + $json.pipedrive_domain + '.pipedrive.com/api/v1/deals' }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpQueryAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ title: 'Chat lead: ' + $json.name + ($json.interest ? ' (' + $json.interest + ')' : ''), person_id: $json.person_id, stage_id: Number($json.pipeline_stage_id) }) }}",
        "options": {}
      },
      "id": "create-deal",
      "name": "Create linked deal",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2044,
        1248
      ]
    },
    {
      "parameters": {
        "jsCode": "const cfg = $('Extract person ID').first().json;\nconst deal = $input.first().json;\nconst dealId = deal?.data?.id;\n\nif (!dealId) {\n  throw new Error('Pipedrive did not return a deal id. Check pipeline_stage_id is a valid stage in your account.');\n}\n\nreturn [{ json: { ...cfg, deal_id: dealId } }];"
      },
      "id": "extract-deal-id",
      "name": "Extract deal ID",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        2264,
        1248
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ 'https://' + $json.pipedrive_domain + '.pipedrive.com/api/v1/notes' }}",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpQueryAuth",
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ deal_id: $json.deal_id, content: 'Captured via chat.\\n\\nInterest: ' + ($json.interest || '-') + '\\nIntent: ' + ($json.intent || '-') + '\\nObjection: ' + ($json.objection || '-') + '\\n\\nSummary: ' + ($json.summary || '-') + '\\n\\nPage: ' + ($json.page_url || '-') + '\\nSession: ' + ($json.session_id || '-') }) }}",
        "options": {
          "response": {
            "response": {
              "neverError": true
            }
          }
        }
      },
      "id": "add-note",
      "name": "Add note to deal",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        2484,
        1248
      ]
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "r1",
              "name": "response",
              "value": "Saved. A new deal has been created in the pipeline and someone will follow up.",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "id": "success-response",
      "name": "Tell the agent it worked",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        2704,
        1248
      ]
    }
  ],
  "pinData": {},
  "connections": {
    "Chat webhook": {
      "main": [
        [
          {
            "node": "Chatbot settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Chatbot settings": {
      "main": [
        [
          {
            "node": "Request from your widget?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Request from your widget?": {
      "main": [
        [
          {
            "node": "Answer questions and capture leads",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Reject unknown requests",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Answer questions and capture leads": {
      "main": [
        [
          {
            "node": "Respond to widget",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "GPT-4.1 mini model": {
      "ai_languageModel": [
        [
          {
            "node": "Answer questions and capture leads",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Conversation memory": {
      "ai_memory": [
        [
          {
            "node": "Answer questions and capture leads",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "capture_lead": {
      "ai_tool": [
        [
          {
            "node": "Answer questions and capture leads",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "When called by the agent": {
      "main": [
        [
          {
            "node": "Parse and validate",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse and validate": {
      "main": [
        [
          {
            "node": "Valid email?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Valid email?": {
      "main": [
        [
          {
            "node": "Tool settings",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Tell the agent it failed",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Tool settings": {
      "main": [
        [
          {
            "node": "Search for existing person",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search for existing person": {
      "main": [
        [
          {
            "node": "Did we find a person?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Did we find a person?": {
      "main": [
        [
          {
            "node": "Person already exists?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Person already exists?": {
      "main": [
        [
          {
            "node": "Extract person ID",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create new person",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create new person": {
      "main": [
        [
          {
            "node": "Extract person ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract person ID": {
      "main": [
        [
          {
            "node": "Create linked deal",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create linked deal": {
      "main": [
        [
          {
            "node": "Extract deal ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract deal ID": {
      "main": [
        [
          {
            "node": "Add note to deal",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add note to deal": {
      "main": [
        [
          {
            "node": "Tell the agent it worked",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "tags": []
}