{
  "name": "Turn every incoming WhatsApp message into a CRM lead — and answer it instantly",
  "nodes": [
    {
      "parameters": {
        "content": "What you get: a lead (or a note on the existing contact) in your CRM for every first message, plus an immediate acknowledgement to the customer.\nWhat it replaces: copying phone numbers from your phone into the CRM, and messages that sit unanswered for hours.\nSetup time: about 10 minutes.\n\nNothing your customers write on WhatsApp gets lost outside the CRM again.\n\nWorks with a normal WhatsApp number — no Meta Business account needed.\n\nSetup:\n1. Create an API key at https://developers.wasync.app/keys (scopes: whatsapp.send (add whatsapp.events too if you'll use the optional Register Webhook node)).\n2. In n8n, go to Credentials -> New -> Header Auth. Name the credential \"WASync API key\". Set its Name field to `Authorization` and its Value field to `Bearer wsk_live_<your key>`.\n3. Open the \"Settings\" node at the top of this workflow and fill in your own values. Never commit real portal URLs, tokens, connection ids, or phone numbers.\n\nPoint WASync at this workflow's Webhook node: copy its Production URL (Webhook node -> double-click -> \"Production URL\"), then either paste it at developers.wasync.app (Webhooks section) for event `message.received`, or call `PUT https://developers.wasync.app/api/v1/webhook` with `{\"url\": \"<that URL>\"}` using your API key. The disabled \"Register WASync Webhook (one-time)\" HTTP Request node below does the same call -- fill in the URL, enable it, execute it once, then disable it again (PUT is idempotent, so running it again just moves the endpoint).\n\nThis template uses only built-in n8n nodes, so it works on n8n Cloud; if you self-host, you can also use our community node n8n-nodes-wasync.\n\nDocs: https://docs.wasync.app/docs/guides/n8n?utm_source=n8n&utm_medium=template&utm_campaign=t2-http",
        "height": 560,
        "width": 460
      },
      "name": "Read Me First",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        0,
        -80
      ]
    },
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "wasync-inbound",
        "responseMode": "onReceived",
        "options": {}
      },
      "name": "WhatsApp Inbound Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [
        0,
        200
      ]
    },
    {
      "parameters": {
        "mode": "manual",
        "assignments": {
          "assignments": [
            {
              "id": "Settings-0",
              "name": "bitrixPortalUrl",
              "value": "https://yourportal.bitrix24.com",
              "type": "string"
            },
            {
              "id": "Settings-1",
              "name": "bitrixWebhookToken",
              "value": "REPLACE_WITH_YOUR_INBOUND_WEBHOOK_TOKEN",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "name": "Settings",
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        0,
        460
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://developers.wasync.app/api/v1/messages/read",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Accept",
              "value": "application/json"
            }
          ]
        },
        "options": {},
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ connectionId: $('WhatsApp Inbound Webhook').item.json.body.connection_id, messageId: $('WhatsApp Inbound Webhook').item.json.body.message.id }) }}"
      },
      "name": "Mark Message As Read",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        260,
        460
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "WASync API key"
        }
      }
    },
    {
      "parameters": {
        "method": "GET",
        "url": "={{ $('Settings').item.json.bitrixPortalUrl }}/rest/{{ $('Settings').item.json.bitrixWebhookToken }}/crm.contact.list",
        "options": {},
        "sendQuery": true,
        "specifyQuery": "keypair",
        "queryParameters": {
          "parameters": [
            {
              "name": "filter[PHONE]",
              "value": "={{ $('WhatsApp Inbound Webhook').item.json.body.message.from }}"
            },
            {
              "name": "select[]",
              "value": "ID"
            }
          ]
        }
      },
      "name": "Find Contact by Phone",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        520,
        460
      ]
    },
    {
      "parameters": {
        "conditions": {
          "options": {
            "caseSensitive": true,
            "leftValue": "",
            "typeValidation": "strict"
          },
          "conditions": [
            {
              "id": "Contact Not Found?-cond0",
              "leftValue": "={{ $json.result ? $json.result.length : 0 }}",
              "rightValue": 0,
              "operator": {
                "type": "number",
                "operation": "equal"
              }
            }
          ],
          "combinator": "and"
        },
        "options": {}
      },
      "name": "Contact Not Found?",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.2,
      "position": [
        780,
        460
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "={{ $('Settings').item.json.bitrixPortalUrl }}/rest/{{ $('Settings').item.json.bitrixWebhookToken }}/crm.lead.add",
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ fields: { NAME: 'WhatsApp ' + $('WhatsApp Inbound Webhook').item.json.body.message.from, PHONE: [{ VALUE: $('WhatsApp Inbound Webhook').item.json.body.message.from, VALUE_TYPE: 'WORK' }], COMMENTS: $('WhatsApp Inbound Webhook').item.json.body.message.text, SOURCE_ID: 'WHATSAPP' } }) }}",
        "options": {}
      },
      "name": "Create Lead",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1040,
        340
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://developers.wasync.app/api/v1/messages",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Accept",
              "value": "application/json"
            }
          ]
        },
        "options": {},
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ connectionId: $('WhatsApp Inbound Webhook').item.json.body.connection_id, to: $('WhatsApp Inbound Webhook').item.json.body.message.from, text: \"Thanks, we'll get back to you within 1 hour.\" }) }}"
      },
      "name": "Send Auto-Reply",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        1300,
        460
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "WASync API key"
        }
      }
    },
    {
      "parameters": {
        "method": "PUT",
        "url": "https://developers.wasync.app/api/v1/webhook",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Accept",
              "value": "application/json"
            }
          ]
        },
        "options": {},
        "sendBody": true,
        "contentType": "json",
        "specifyBody": "json",
        "jsonBody": "={{ JSON.stringify({ url: 'https://YOUR-N8N-INSTANCE/webhook/wasync-inbound' }) }}"
      },
      "name": "Register WASync Webhook (one-time)",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        0,
        -320
      ],
      "credentials": {
        "httpHeaderAuth": {
          "name": "WASync API key"
        }
      },
      "disabled": true
    }
  ],
  "connections": {
    "WhatsApp Inbound Webhook": {
      "main": [
        [
          {
            "node": "Settings",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Settings": {
      "main": [
        [
          {
            "node": "Mark Message As Read",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Mark Message As Read": {
      "main": [
        [
          {
            "node": "Find Contact by Phone",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Find Contact by Phone": {
      "main": [
        [
          {
            "node": "Contact Not Found?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Contact Not Found?": {
      "main": [
        [
          {
            "node": "Create Lead",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Send Auto-Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create Lead": {
      "main": [
        [
          {
            "node": "Send Auto-Reply",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}
