intermediate

Mailchimp lead capture chatbot for n8n, with opt-in consent

An n8n chatbot that stores website leads in a Mailchimp audience and only subscribes them after a separate, explicit yes. One API call per save.

ChatbotAI agentCRMLead capture

Prerequisites

  • An n8n instance, cloud or self-hosted
  • A Mailchimp account with at least one audience
  • A Mailchimp API key and its data centre (the usXX part)
  • An OpenAI API key (or another chat model credential)
  • Your FAQ or services copy as plain text

A chat widget that answers visitor questions and stores their details in a Mailchimp audience, without subscribing anyone who has not clearly agreed to marketing.

What you'll build

The bot answers from your own content and, when there is a reason, captures the visitor's email. Marketing consent is asked separately and only recorded if the answer is a clear yes.

How it works

Mailchimp does more of the work than most CRMs, so this is the simplest of the consent recipes: one tool and one API call per save.

  • A webhook receives each widget message and checks a shared secret.
  • An AI agent answers from your knowledge text and asks for an email when there is a reason.
  • The capture_lead tool is a separate workflow. Mailchimp identifies members by the MD5 hash of their lowercased email in the API URL, and the tool computes that itself, so there is nothing to configure.
  • One PUT request creates or updates the member. Without consent the member is stored with Mailchimp's transactional status, which means captured but not subscribed to marketing. After a separate, explicit yes, a second call upgrades the same member to subscribed.

Warning: The transactional pre-consent status is a real Mailchimp member status, but it has not been confirmed against a live account for this recipe. Check that it behaves as you expect before relying on it. The fallback is pending, Mailchimp's double opt-in state, which emails the contact a confirmation link rather than capturing them silently.

What it costs

The workflow is free to download and use. Your AI provider charges per conversation, and your n8n hosting and the plan for the service you connect apply as normal. For N8N Chat UI plans, see pricing. Mailchimp's own plan limits also apply, including the contact cap on the free plan.

Set it up

  1. Get your API key and data centre. In Mailchimp go to Account → Extras → API keys and create a key. The data centre is the usXX suffix on the key, and it matches the usXX in the URL Mailchimp redirects you to after login.
  2. Create the credential. In n8n, create an HTTP Basic Auth credential. The username can be any string (Mailchimp ignores it) and the password is your API key.
  3. Import the workflow. Download the file with the button at the top of this page and import it with Workflows → Import from File. It holds the main chatbot and its tool workflow on one canvas, each in a labelled section.
  4. Move each tool into its own workflow. n8n tools call a separate workflow, so for each tool section below the main flow: select its nodes, copy them, create a new empty workflow and paste. Save it, then open the matching tool node under the agent and select that workflow. Attach the Basic Auth credential to the Upsert Mailchimp member node in the capture lead workflow.
  5. Fill in the settings. In Chatbot settings set company_name, consent_wording, knowledge and a random webhook_secret. In the capture lead workflow's Tool settings set mailchimp_datacenter and list_id, which is your audience ID from the audience settings.
  6. Add your AI credential. Open GPT-4.1 mini model and add your OpenAI credential. Swap the node for another chat model if you prefer.

Test it

Send a test message before you connect a widget. n8n's built-in chat panel needs a Chat Trigger, so it will not open for this workflow. Instead, click Test workflow, then send a POST request to the Test URL shown on the Chat webhook node:

curl -X POST 'YOUR_TEST_URL' \
  -H 'Content-Type: application/json' \
  -d '{"chatInput":"Hello","sessionId":"test-1","metadata":{"webhook_secret":"YOUR_SECRET"}}'

The test URL listens for one request each time you click Test workflow. For a longer conversation, activate the workflow and send requests to the production URL, then read each run under Executions. Reuse the same sessionId to continue a conversation, because memory is kept per session.

Things to try:

  • Ask a question and give no email. Nothing should be created.
  • Give an email and decline marketing. The member appears as transactional, not subscribed.
  • Give an email and agree. The status becomes subscribed.
  • Come back with the same email. The same member is updated, not duplicated.

Add it to your site

Activate the workflow and use its production webhook URL as your widget's chat endpoint. Send the same secret in the widget metadata, for example metadata: { page_url: window.location.href, webhook_secret: 'YOUR_SECRET' }. The metadata docs explain how metadata reaches n8n.

Any chat front end that can POST chatInput, sessionId and metadata to a webhook and read { "output": "..." } back will work. N8N Chat UI is built for exactly this.

Getting someone's email so you can answer their question, and adding them to a marketing list, are two different things. Treating them as one is how businesses end up with complaints they cannot answer. Many chatbot-to-CRM setups subscribe anyone who types an email address, which is a problem under UK GDPR and the EU equivalent, where consent has to be specific and freely given rather than bundled into something else the person wanted.

This workflow splits it in two. The agent never writes to the CRM directly. When it has an email and a reason to save it, it calls the capture_lead tool with consent_given set to false, and the contact is created but subscribed to nothing. Only after a separate, explicit yes does it call the tool a second time with consent_given set to true. That second call adds the contact to your list and records the exact wording the bot used and a timestamp.

The agent is told that ambiguity is not consent. "Sure, whatever" in reply to a bundled question does not count, and it is instructed never to bundle the two asks. Because the tool is a separate workflow, the gate is ordinary n8n branching that either fires or does not, not a suggestion to the model.

None of this is legal advice, and your obligations depend on where you and your contacts are. What it gives you is a record: if someone asks how they ended up on your list, the answer is on their contact record with a date against it.

Troubleshooting

I get a 401 from Mailchimp

The data centre in the URL must match the suffix on your API key exactly. A mismatch fails authentication even with a correct key.

The member does not get the status I expected

See the warning above. Check the status directly in your Mailchimp audience and fall back to pending if transactional does not work in your account.

A previously unsubscribed contact cannot be re-subscribed

Mailchimp does not let an API call re-subscribe someone who unsubscribed themselves. The tool tells the agent something went wrong, and the agent is instructed to say a person will follow up by email rather than retry.

The bot replies but the tool never runs

The tool node is not linked to its workflow. Open it and select the workflow you created from the tool section. A bare placeholder ID means it is still unlinked.

My conversations reset when n8n restarts

Memory is an in-memory buffer so the template works as soon as you import it. A restart or redeploy drops conversations in progress. Before a busy launch, swap Conversation memory for Postgres Chat Memory. Nothing else changes.

I get a 401 as soon as I test

The Request from your widget? check compares metadata.webhook_secret in the request with webhook_secret in Chatbot settings. If they differ, or the request has no metadata, the workflow returns a 401 before the agent runs.

Take it further

The consent principle carries over to other email platforms, but Klaviyo's API and member model differ enough that it would need its own recipe. See also the ActiveCampaign and Google Sheets versions.