A webhook-to-CRM lead router in n8n that survives retries and duplicates
Most small-business automations start with the same request: "When someone fills in the website form, put them in the CRM and tell the right salesperson." It sounds like a ten-minute n8n workflow. In production it is whe
Most small-business automations start with the same request: "When someone fills in the website form, put them in the CRM and tell the right salesperson." It sounds like a ten-minute n8n workflow. In production it is where duplicate contacts, lost leads and 2 a.m. alert storms come from.
This post walks through the version we now build by default: a webhook-to-CRM lead router in n8n that is safe to retry, does not create duplicates and tells a human when it fails. The examples use HubSpot as the CRM, but the pattern is the same for Pipedrive, Zoho or a custom CRM with a REST API.
The workflow at a glance
Webhook (POST /lead)
-> Set: normalise fields
-> IF: required fields present?
no -> Respond 422 + log
yes -> Code: build idempotency key
-> CRM: search contact by email
-> IF: contact exists?
yes -> CRM: update contact + add note
no -> CRM: create contact
-> Switch: route by region / service
-> Slack or email: notify owner
-> Respond to Webhook 200
Error workflow (Error Trigger) -> alert channel
Seven ideas carry most of the reliability.
1. Answer the form quickly, then do the slow work
Set the Webhook node's response mode to "Using 'Respond to Webhook' node" and respond as soon as you have validated the payload. The visitor's form should not wait for three API calls to finish. If your form tool retries on slow responses (many do), a slow workflow is also how you get the same lead three times.
2. Normalise before you compare
Use a Set node (or a small Code node) to trim whitespace, lower-case the email, strip spaces from phone numbers and map the form's field names to your CRM's property names. Every later step, including duplicate detection, depends on this.
// Code node: normalise and build an idempotency key
const crypto = require('crypto');
return $input.all().map(item => {
const email = (item.json.email || '').trim().toLowerCase();
const message = (item.json.message || '').trim();
const day = new Date().toISOString().slice(0, 10);
const key = crypto.createHash('sha256')
.update(`${email}|${message}|${day}`)
.digest('hex');
return { json: { ...item.json, email, message, idempotencyKey: key } };
});
(On self-hosted n8n, built-in modules like crypto may need to be allowed with the NODE_FUNCTION_ALLOW_BUILTIN environment variable.)
3. Search, then create or update
The most common bug in lead workflows is "Create contact" on every submission. Search the CRM by email first and branch:
- Found: update the fields that may have changed (phone, company, last enquiry) and add the message as a note or activity, so sales sees the history.
- Not found: create the contact with a lifecycle stage and lead source.
HubSpot's CRM search API and n8n's HubSpot node both support looking a contact up by email. For a custom CRM, add an endpoint that accepts an email and returns the record or a 404.
4. Make retries harmless
Turn on Retry On Fail in the settings of each node that calls an external API (two or three tries with a wait is plenty). Retries are only safe if the step is idempotent. The search-then-upsert pattern above handles contacts. For notes and notifications, store the idempotency key (in a CRM property, a small database table or n8n's data store options) and skip the step if you have already processed that key.
n8n's Remove Duplicates node can also drop repeated items within a run, and recent versions can remember items from previous executions. Treat that as a second line of defence, not the only one.
5. Route with data, not with nested IFs
Sales routing rules change every quarter. Instead of a tree of IF nodes, keep a small lookup table (a Google Sheet, an Airtable base or a JSON object in a Set node) that maps region and service to an owner. A single Switch or Code node reads it. When the rules change, the sales manager edits the table, not your workflow.
6. Fail loudly, in one place
Create a separate workflow that starts with an Error Trigger node and set it as the error workflow in the lead router's settings. Send the workflow name, the failed node, the error message and the execution URL to a Slack channel or email address someone actually reads. Do not send the whole form payload: it contains personal data and does not belong in a chat channel.
7. Keep personal data out of execution logs where you can
n8n stores execution data by default. For workflows that handle names, emails and phone numbers, decide how long you need it: the "Save successful production executions" setting and execution pruning settings let you keep failures for debugging without keeping every lead forever. If you serve UK or EU visitors, this is part of your UK GDPR / GDPR storage-limitation story; Canadian and Australian privacy law has similar expectations.
A test checklist before you go live
- Submit the same form twice in a row: you should get one contact and one note (or two notes, by design, but never two contacts).
- Submit with an upper-case email that already exists: it should match the existing contact.
- Break the CRM credentials on purpose: the error workflow should alert you within a minute.
- Submit with a missing required field: the form should get a clear error and nothing should reach the CRM.
- Check the CRM record: lead source, owner and the original message should all be there.
FAQ
Should I use n8n Cloud or self-host?
Both work for this pattern. Self-hosting gives you control over where data lives and over environment settings; Cloud removes server maintenance. Choose based on who will patch and monitor the server.
Is a native form-to-CRM integration better?
If your form tool's native integration handles deduplication and routing the way you need, use it. n8n earns its place when you need validation, routing rules, enrichment or more than one destination.
How long does a workflow like this take to build?
The happy path is quick. Most of the time goes into the edge cases above and into agreeing the routing rules with sales.
If you would rather have this built and monitored for you, our team provides n8n automation services for businesses in the USA, UK, Canada and Australia, from single workflows like this to AI-assisted lead qualification.
`
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.