n8n AI Automation Basics
Build a small n8n workflow that accepts a webhook, calls an AI model, validates the response and logs the result.
Prerequisites
- • n8n installed or a cloud account
- • Basic understanding of APIs
- • An AI provider API key stored securely
What you will learn
- Create a webhook-triggered workflow
- Call an AI model with a structured prompt
- Validate the response and handle errors
What you will build
A webhook receives a support message, an AI step classifies it and drafts a reply, and a validation step rejects malformed output. Nothing is sent automatically.
This is deliberately small. The pattern—trigger, prepare, AI, validate, approve—scales to larger workflows.
Step 1: Create the webhook
Add a Webhook node with a POST endpoint. Configure it to accept JSON. In test mode, n8n gives you a test URL. Send a sample payload:
curl -X POST "$N8N_TEST_WEBHOOK_URL" \
-H "Content-Type: application/json" \
-d '{"message":"My export has failed for two days and I need the data today.","customer":"Acme"}'
Step 2: Prepare the input
Add a Set or Code node to normalise the fields. Remove unnecessary personal data before it reaches the model. Keep the customer name only if the reply needs it.
Step 3: Add the AI step
Use an AI or HTTP Request node with a prompt that returns JSON only:
Classify the support message and draft a reply.
Return JSON with:
- category: one of billing, bug, how-to, cancellation, other
- urgency: low, medium, high
- draft_reply: under 120 words
- missing_information: array of facts needed before sending
Do not promise refunds, timelines or fixes. If the message is ambiguous, set category to "other" and list the missing information.
Message: {{ $json.message }}
Set the model temperature low for classification. Do not put API keys in the prompt or in a code node; use n8n credentials.
Step 4: Validate the output
Add a Code node:
const allowed = ["billing", "bug", "how-to", "cancellation", "other"];
const data = $json;
if (!allowed.includes(data.category)) {
throw new Error("Invalid category: " + data.category);
}
if (!Array.isArray(data.missing_information)) {
throw new Error("missing_information must be an array");
}
if (typeof data.draft_reply !== "string" || data.draft_reply.length > 1200) {
throw new Error("Invalid draft_reply");
}
return [{ json: data }];
Validation is not optional. Models occasionally return prose around JSON or omit fields.
Step 5: Route the result
Send valid results to a review channel with the original message and the draft. Send invalid results to an error workflow. Log the run ID, category and validation status—not the full message.
Step 6: Add an error workflow
In workflow settings, configure an error workflow that notifies the owner with the execution ID. Test it by forcing a validation failure.
Step 7: Test before activating
Test at least ten messages, including an angry customer, a billing question, an ambiguous message and a message containing instructions to the AI. Confirm that nothing is sent automatically and that malformed output is rejected.
Common problems
- The AI node returns text, not JSON. Use a structured output mode if available, or parse and validate explicitly.
- The webhook is public and unprotected. Add authentication or a shared secret and restrict access.
- Secrets appear in execution logs. Use credentials and disable logging of sensitive fields.
- The workflow duplicates actions on retry. Add an idempotency key and check for an existing record.
Exercises
Do these before moving on. Reading is not the same as being able to apply it.
Exercise 1
Add schema validation
Extend the workflow to reject responses missing required fields and send the failure to an error channel.
Exercise 2
Add an approval step
Insert a human approval before the final write action and log the approver.
References
Finished this tutorial? Continue with a guided path.
Browse learning paths →