Skip to content

Lead Webhooks

Mcdavid Obioha edited this page Dec 2, 2026 · 1 revision

Lead Webhooks

Available from v0.8.0, in the React package and the script-tag build.

When a visitor presses Open in WhatsApp, they leave your site. If they never press send in WhatsApp, you never hear from them. A lead webhook sends you the form details at the moment they submit, so you keep the lead either way.

<WhatsAppChat
  phoneNumber="+2348012345678"
  includePageContext
  webhookUrl="https://hooks.example.com/whatsapp-lead"
  onBeforeHandoff={async (lead) => { await analytics.track('whatsapp_lead', lead); }}
/>

Props

Prop Type What it does
webhookUrl string POSTs every submission to this URL as JSON, without blocking the handoff. Failures are ignored.
onBeforeHandoff (payload: FormattedSubmission) => void | Promise<void> Called with the same payload just before WhatsApp opens. An async callback is awaited for up to 1.5 seconds. Errors are ignored.

Use either or both. webhookUrl is for your CRM, Slack or an automation. onBeforeHandoff is for code that already runs in the page: analytics, a CRM SDK, a dataLayer push.

The payload

{
  "name": "Ada",
  "subject": "Pricing",
  "message": "Hello, how much is the Pro plan?",
  "selectedAgent": { "id": "sales", "name": "Sales", "role": "Quotes" },
  "pageContext": { "title": "Pricing", "url": "https://site.com/pricing", "referrer": "https://google.com/" },
  "utm": { "utm_source": "ads", "utm_campaign": "spring" },
  "timestamp": 1790000000000
}
Field Notes
name, subject, message As submitted. Hidden fields are empty strings. message is the AI-refined text when routing refined it.
selectedAgent Present when agents is used. Only id, name and role: never phone numbers.
pageContext Present when includePageContext is on. The URL has no query string or hash. referrer only when you enabled it.
utm UTM parameters from the page URL. {} when includePageContext is off.
timestamp Milliseconds since the epoch.
sessionId Live chat only: identifies the conversation. See Live Chat.

How it is sent

  1. navigator.sendBeacon(url, blob) with Content-Type: application/json. The browser finishes this request even while the page is navigating to WhatsApp, which is why it is used rather than a plain fetch.
  2. If sendBeacon is missing or refuses the request (too large, blocked), the widget falls back to fetch(url, { method: 'POST', keepalive: true }), which also survives navigation.
  3. Any error, in either path, is swallowed. A broken webhook never stops the visitor reaching WhatsApp.

The request is not awaited and its result is not shown anywhere, so you will not see failures in the page. Watch your webhook's own logs.

The order of events on submit

  1. The form is validated. Nothing happens if a required field is empty or the number is invalid.
  2. If AI routing is set, it runs (and may show an instant reply).
  3. onSend is called.
  4. webhookUrl is fired.
  5. onBeforeHandoff runs (up to 1.5 seconds if async).
  6. WhatsApp opens (or the live chat starts).

If the visitor taps That solved it on an AI instant reply, none of steps 3 to 6 happen: there is no lead.

Popup blockers

Browsers only allow window.open shortly after a click. With no async steps, the widget opens WhatsApp synchronously and there is no problem. If an await ran first (async onBeforeHandoff, AI routing) and the browser says the click is no longer fresh, the widget shows an Open WhatsApp button instead of a popup that would be blocked. The visitor taps it once. This is automatic.

To avoid the extra tap: keep onBeforeHandoff fast, or do not use an async one.

CORS

sendBeacon and the fallback fetch send Content-Type: application/json from your site to another origin, which makes the browser send a preflight (OPTIONS) request first. Your endpoint must answer it with:

Access-Control-Allow-Origin: https://yoursite.com
Access-Control-Allow-Methods: POST, OPTIONS
Access-Control-Allow-Headers: Content-Type

n8n, Make and Zapier webhooks already do this. If you use your own endpoint, add it. A webhook on the same origin as your site needs no CORS.

Recipes

n8n or Make

  1. Add a Webhook node (n8n) or Custom webhook (Make): method POST, respond immediately.
  2. Copy the production URL (not the test URL) into webhookUrl.
  3. Connect the next step: a Slack message, a Google Sheets row, an email, HubSpot.

Zapier

Use the Webhooks by Zapier trigger, Catch Hook. Paste its URL as webhookUrl. The fields appear as name, subject, message, selectedAgent__id, utm__utm_source and so on.

Slack

Slack's own incoming webhooks expect { "text": "..." }, which this payload is not, so point webhookUrl at an automation (n8n, Make, Zapier) that formats the message. (Slack's Workflow Builder webhook trigger only accepts flat text fields, and this payload has nested objects.) A tiny serverless function also works:

// Cloudflare Worker or any serverless function
export default {
  async fetch(req: Request, env: { SLACK_WEBHOOK_URL: string }) {
    const cors = { 'Access-Control-Allow-Origin': 'https://yoursite.com', 'Access-Control-Allow-Headers': 'Content-Type', 'Access-Control-Allow-Methods': 'POST, OPTIONS' };
    if (req.method === 'OPTIONS') return new Response(null, { status: 204, headers: cors });
    const lead = (await req.json()) as { name: string; subject: string; message: string; pageContext?: { url?: string } };
    await fetch(env.SLACK_WEBHOOK_URL, {
      method: 'POST',
      body: JSON.stringify({ text: `*New WhatsApp lead*: ${lead.name}\n*${lead.subject}*\n${lead.message}\n${lead.pageContext?.url ?? ''}` }),
    });
    return new Response('ok', { headers: cors });
  },
};

HubSpot

Either use a HubSpot workflow with a webhook trigger (Operations Hub), or send the lead through a small function that calls the Forms API (POST https://api.hsforms.com/submissions/v3/integration/submit/<portalId>/<formGuid>) with your form's field names. Keep any private app token on the server, not in the page.

Google Analytics or your own analytics

onBeforeHandoff={(lead) => {
  window.gtag?.('event', 'generate_lead', { method: 'whatsapp', utm_source: lead.utm.utm_source });
}}

Privacy

The webhook carries personal data (a name and free-text message), and it fires when the form is submitted, even if the visitor never sends the WhatsApp message. Tell visitors, for example through hint:

<WhatsAppChat hint="We save your message so we can follow up, even if you don't send it on WhatsApp." ... />

and cover it in your privacy policy. The widget itself stores nothing and sets no cookies. Do not put secrets in webhookUrl query strings: the URL is visible in the page. Use an unguessable path, or a proxy that checks the origin.

Troubleshooting

  • Nothing arrives. Open the network tab and submit the form. Look for a request to your URL (it may show as a beacon). A CORS failure shows in the console. Use the production URL, not the test URL.
  • It arrives twice. You set both webhookUrl and a webhook call inside onBeforeHandoff. Use one.
  • utm is empty. includePageContext is off, or the page URL has no UTM parameters.
  • Order of fields. JSON objects have no field order. Select fields by name.

Clone this wiki locally