DEV Community

Hashim khan
Hashim khan

Posted on

How to Build a Lead Capture & Validation Workflow Using n8n

Introduction

Businesses receive enquiries through website contact forms, landing pages, and other online channels. Managing these enquiries manually can take time, especially when a business receives multiple leads every day.

With n8n, you can build a workflow that automatically receives lead information through a webhook, validates the submitted email address, formats the data, and returns a response.

In this tutorial, we'll build a simple lead capture and validation workflow using n8n. It's a practical starting point for developers, freelancers, and businesses looking to automate their lead management processes.

What You'll Build

The workflow will receive lead information from a website form and validate the submitted email address before returning a response.

Workflow structure:

Website Form → Webhook → Validate & Format Lead → Respond to Webhook

The workflow includes:

  • Webhook: Receives incoming lead information through an HTTP POST request.
  • Validation: Checks whether the email address has a valid format.
  • Data formatting: Organizes the lead's name, email, phone number, and enquiry.
  • Webhook response: Returns a success or validation message to the form.

This basic version does not save leads to a CRM or send emails. Those features can be added later.

Requirements

Before starting, you need:

  • An n8n account or self-hosted n8n instance.
  • Basic knowledge of workflows and JSON.
  • A tool such as Postman or cURL for testing the webhook.

Step 1: Create a New Workflow

Log in to your n8n dashboard and create a new workflow.

Add a Webhook node and configure it with the following settings:

Setting Value
HTTP Method POST
Path lead-intake
Response Mode Using Respond to Webhook Node

The webhook will receive lead data from your website or testing tool.

Step 2: Add the Validation Node

Add a Code node after the Webhook node.

Set the Code node to JavaScript and use the following code:

const input = $input.first().json.body ?? $input.first().json;

const name = String(input.name ?? '').trim();
const email = String(input.email ?? '').trim().toLowerCase();
const phone = String(input.phone ?? '').trim();
const enquiry = String(input.enquiry ?? '').trim();

const validEmail = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);

return [{
  json: {
    success: validEmail,
    message: validEmail
      ? 'Lead received successfully'
      : 'A valid email address is required',
    lead: validEmail
      ? {
          name,
          email,
          phone,
          enquiry,
          receivedAt: new Date().toISOString()
        }
      : null
  }
}];
Enter fullscreen mode Exit fullscreen mode

This code extracts the submitted fields, trims unnecessary spaces, converts the email to lowercase, and performs a basic email-format check.

If the email is valid, the workflow prepares a structured lead object. Otherwise, it returns a validation message.

This is format validation only; it does not verify whether an email inbox actually exists.

Step 3: Add Respond to Webhook

Add a Respond to Webhook node after the Code node.

Configure it as follows:

Setting Value
Respond With JSON
Response Body ={{ $json }}
Response Code ={{ $json.success ? 200 : 400 }}

Connect the nodes in this order:

Webhook → Code → Respond to Webhook

The response node sends the result back to the system that submitted the lead.

Step 4: Test the Workflow

Save your workflow and select Test Workflow. Copy the test URL from the Webhook node.

You can test it using cURL:

curl -X POST "YOUR_WEBHOOK_TEST_URL" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "John Smith",
    "email": "john@example.com",
    "phone": "+441234567890",
    "enquiry": "I need an AI chatbot for my business."
  }'
Enter fullscreen mode Exit fullscreen mode

Replace YOUR_WEBHOOK_TEST_URL with the actual URL generated by your n8n instance.

For a valid email address, the workflow returns a success response containing the formatted lead. If the email is missing or invalid, it returns a validation message.

Step 5: Importable Workflow JSON

You can also create the workflow by importing this JSON into n8n using Import from Clipboard.

{
  "name": "Lead Capture Webhook",
  "nodes": [
    {
      "parameters": {
        "httpMethod": "POST",
        "path": "lead-intake",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "a1b2c3d4-1111-4444-8888-123456789001",
      "name": "Lead Webhook",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 2,
      "position": [240, 300],
      "webhookId": "lead-intake-webhook"
    },
    {
      "parameters": {
        "jsCode": "const input = $input.first().json.body ?? $input.first().json;\nconst name = String(input.name ?? '').trim();\nconst email = String(input.email ?? '').trim().toLowerCase();\nconst phone = String(input.phone ?? '').trim();\nconst enquiry = String(input.enquiry ?? '').trim();\nconst validEmail = /^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$/.test(email);\nreturn [{ json: { success: validEmail, message: validEmail ? 'Lead received successfully' : 'A valid email address is required', lead: validEmail ? { name, email, phone, enquiry, receivedAt: new Date().toISOString() } : null } }];"
      },
      "id": "a1b2c3d4-2222-4444-8888-123456789002",
      "name": "Validate & Format Lead",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [500, 300]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ $json }}",
        "options": {
          "responseCode": "={{ $json.success ? 200 : 400 }}"
        }
      },
      "id": "a1b2c3d4-3333-4444-8888-123456789003",
      "name": "Return Response",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [760, 300]
    }
  ],
  "connections": {
    "Lead Webhook": {
      "main": [
        [
          {
            "node": "Validate & Format Lead",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Validate & Format Lead": {
      "main": [
        [
          {
            "node": "Return Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  }
}
Enter fullscreen mode Exit fullscreen mode

After importing, check the node connections and test the workflow with your own n8n instance. The workflow is inactive by default, so activate it only after successful testing.

Step 6: Extend the Workflow

Once the basic workflow is working, you can expand it to include additional automation features:

  • Google Sheets: Store submitted leads in a spreadsheet.
  • Airtable: Organize leads and track their status.
  • HubSpot: Send lead information to your CRM.
  • Email automation: Notify your sales team when a new lead arrives.
  • AI lead qualification: Use an AI model to categorize enquiries based on their content.
  • Follow-up automation: Send an acknowledgement or follow-up message through a configured email service.

These integrations require additional nodes, configuration, and credentials.

Important Considerations

For production use, protect your webhook against spam and unauthorized submissions. Consider adding authentication, rate limiting, input validation, and error handling. Avoid collecting unnecessary personal information, and configure suitable access controls and data retention policies.

The email validation in this tutorial is intentionally simple. For real business use, additional checks may be required.

Conclusion

With n8n, you can build a simple lead capture and validation workflow without writing a complete backend application. This template provides a starting point for receiving enquiries and preparing them for further automation.

You can extend it with CRM integration, email notifications, AI-powered lead qualification, and other business processes as your requirements grow.

About Aiotagen

At Aiotagen, we help businesses streamline operations with AI automation, AI chatbots, AI calling agents, n8n workflows, and custom AI solutions.

Explore our services and discover how automation can support your business.

🌐 Website: https://aiotagen.com/

Have you built a similar lead capture workflow in n8n? Share your approach or suggestions in the comments.

Top comments (0)