In this post you’ll learn how to set up an AI‑powered automation using n8n, why it matters for everyday development tasks, and how to keep the flow reliable. You’ll walk away with a ready‑to‑run example that calls a language model, stores the result, and sends a notification.
What is n8n?
n8n (pronounced n‑eight‑en) is an open‑source workflow engine. Think of it as a visual version of a script where each block, called a node, does one job – fetch data, transform it, call an API, write to a database, etc. You connect nodes with arrows, and n8n runs them in order.
- Node – a single operation, like "HTTP Request" or "Google Sheets".
- Workflow – a collection of nodes linked together.
- Execution – when n8n runs the workflow, it passes data from one node to the next.
Because n8n is self‑hosted, you have full control over secrets and can add any custom code you need. That makes it a good fit for AI tasks where you might want to keep API keys private.
Why add AI to a workflow?
AI services – for example large language models (LLMs) – can turn raw data into insights, summarize text, generate code snippets, or even classify support tickets. Embedding those calls inside a workflow means you can automate repetitive thinking work.
Common use‑cases:
- Email triage – pull new messages, ask an LLM to label urgency, then forward to the right team.
- Content creation – fetch a list of topics, ask the model to draft short blurbs, and publish them to a CMS.
- Data enrichment – read rows from a CSV, ask the model to fill missing fields, write back to a database.
Setting up n8n (quick recap)
If you already have n8n running, skip to the next section. Otherwise:
- Install Docker (or use the desktop app).
- Run the official image:
docker run -d \
--name n8n \
-p 5678:5678 \
-v ~/.n8n:/home/node/.n8n \
n8nio/n8n
docker run -dstarts a container in the background.-p 5678:5678maps the container’s web UI port to your machine.-v ~/.n8n:/home/node/.n8npersists workflow definitions.
Open http://localhost:5678 in a browser, create a user, and you’re ready.
Adding an LLM node (generic approach)
n8n does not ship a built‑in node for every AI provider, but you can use the HTTP Request node to call any REST API. Let’s build a workflow that sends a prompt to an LLM and stores the reply.
Step‑by‑step workflow
- Trigger – Start the flow manually (for testing) or with a webhook.
- HTTP Request – Call the LLM endpoint.
- Set – Extract the text from the response.
- Send Email – Notify you of the result.
1. Trigger node
Add a Webhook node. Set the HTTP Method to POST and give it a path like /generate. This will be the URL you call from any client.
2. HTTP Request node
Configure it:
- Method:
POST - URL: the LLM endpoint, e.g.,
https://api.openai.com/v1/chat/completions - Headers:
Authorization: Bearer {{ $env.OPENAI_API_KEY }}(store the key in n8n’s environment variables)Content-Type: application/json
- Body (JSON):
{
"model": "gpt-4",
"messages": [
{ "role": "system", "content": "You are a helpful assistant." },
{ "role": "user", "content": "{{ $json.body.prompt }}" }
]
}
{{ $json.body.prompt }}pulls thepromptfield from the incoming webhook payload.
3. Set node (extract answer)
Add a Set node to keep only the answer text.
{
"answer": "{{ $json.choices[0].message.content }}"
}
$jsonrefers to the data coming from the previous node.choices[0].message.contentis the typical path in OpenAI’s response.
4. Send Email node
Use the built‑in Email node (or any SMTP provider). Map the Subject to AI Reply and Text to {{ $json.answer }}.
Putting it together
Your final workflow looks like:
Webhook → HTTP Request → Set → Email
When you POST { "prompt": "Summarize the benefits of unit testing" } to http://localhost:5678/webhook/generate, n8n will forward the prompt to the LLM, pick out the answer, and email it to you.
Making the flow resilient
AI calls can fail for network glitches or rate limits. n8n offers a few ways to handle that gracefully.
- Error Workflow – Attach an Error Trigger node to a separate workflow that logs the failure or retries later.
- Retry on Failure – In the HTTP Request node, enable Retry On Failure and set a small back‑off.
- Timeouts – Set a reasonable timeout (e.g., 30 seconds) so the workflow doesn’t hang forever.
A simple pattern is to wrap the HTTP Request in a Function node that catches exceptions and returns a default message. Here’s a tiny example:
try {
// assume $node["HTTP Request"].json contains the response
return [{ json: $node["HTTP Request"].json }];
} catch (e) {
return [{ json: { answer: "Sorry, the AI service is unavailable right now." } }];
}
- The
try…catchblock prevents the whole workflow from aborting. - You can still send the fallback answer via email, keeping the user informed.
Extending the pattern: multiple AI steps
Often you need more than one AI call. For instance, you might first generate a draft, then ask the model to improve grammar. You can chain two HTTP Request nodes, feeding the output of the first into the second.
Webhook → HTTP Request (draft) → Set (draft) → HTTP Request (polish) → Set (final) → Email
Remember to keep each request lightweight – large prompts cost more tokens and increase latency. If you need heavy processing, consider off‑loading to a background queue (e.g., RabbitMQ) and let n8n poll for results.
Real‑world example: Auto‑summarize GitHub issues
Let’s sketch a practical scenario you can adapt.
- Trigger – Use the GitHub Trigger node on New Issue.
- HTTP Request – Send the issue body to the LLM with a prompt like "Summarize this issue in two sentences."
- Set – Pull the summary.
- Comment – Add a comment to the same issue with the summary.
The workflow automates a step many teams do manually, saving time and keeping the backlog tidy.
Security considerations
- Secrets – Store API keys in n8n’s Credentials or environment variables, never hard‑code them.
- Data retention – If you log responses, be aware that LLM outputs may contain sensitive information. Clean logs or set a retention policy.
- Rate limits – Respect the provider’s limits; use the built‑in Throttle node if you need to cap calls per minute.
TL;DR checklist
- Install n8n (Docker or desktop).
- Create a webhook to receive input.
- Use an HTTP Request node to call the LLM.
- Extract the answer with a Set node.
- Notify or store the result.
- Add error handling (retry, fallback, error workflow).
- Keep secrets safe and watch rate limits.
FAQ
How do I store my API key securely in n8n?
Use the Credentials panel to create an "API Key" credential. Reference it in the HTTP Request node with {{ $credentials.myKey }}. This keeps the key out of workflow JSON files.
Can I run n8n on a free tier of a cloud provider?
Yes. n8n is lightweight; a small VPS or a free tier container service can host it. Just ensure the instance has outbound internet access to call the AI API.
What if the AI model changes its response format?
Wrap the extraction logic in a Function node that checks for the expected fields and falls back gracefully. Updating the path in one place avoids breaking the whole workflow.
Wrapping up
n8n gives you a visual canvas to stitch together APIs, and adding an LLM is as simple as an HTTP request. With a few nodes you can turn raw data into intelligent output, automate repetitive thinking tasks, and keep the process reliable with retries and error handling. Play around with the example, replace the LLM provider if you like, and you’ll soon have a toolbox of AI‑enhanced automations at your fingertips.