Skip to content
TheAgent Ecosystem
Automation

Turn an n8n Workflow Into an AI API: Webhook to Claude to JSON

Three nodes. One canvas. No backend required.

Muhammad Qasim HammadAI-assisted11 min read2,131 words

AI-drafted, reviewed by Muhammad Qasim Hammad on June 22, 2026. See our AI disclosure.

n8n + Claude: Turn an n8n Workflow Into an AI API
Table of contents
  1. What does it mean to turn an n8n workflow into an AI API?
  2. Which three nodes do you need?
  3. How much does each AI API request cost?
  4. How do you secure a public AI endpoint?
  5. How do you guarantee the response is valid JSON?
  6. How do you build it in n8n, step by step?
  7. Step 1: Add a Webhook node
  8. Step 2: Set Respond to "Using Respond to Webhook Node"
  9. Step 3: Add the AI node and attach Claude
  10. Step 4: Add a Structured Output Parser (optional but recommended)
  11. Step 5: Add the Respond to Webhook node
  12. Step 6: Set Header Auth and activate
  13. Where can this go wrong?
  14. Where to go from here

You have a prompt that works in the editor. Now another app, a frontend, or a teammate's script needs to call it programmatically, and you do not want to stand up an Express server, write auth middleware, and run a deploy pipeline just to expose one prompt. An n8n AI API is just three nodes: a Webhook node that receives the HTTP request, an AI node that runs Claude, and a Respond to Webhook node that returns JSON, all on the same canvas.

No Docker. No Lambda. No framework.

If you have ever lost an afternoon reading Express boilerplate just to wrap a single prompt, this is the shortcut. I expose AI workflows this way instead of writing a backend, and the endpoint lives on the same canvas where I tested the prompt, not in a separate code repo.

What does it mean to turn an n8n workflow into an AI API?#

Turning an n8n workflow into an AI API means three nodes do the whole job of an endpoint: receive a request, do work, return a response. The Webhook node receives, the AI node works, and the Respond to Webhook node returns. No separate server exists, since the endpoint lives on the same canvas where you tested your prompt.

The n8n Webhook node creates a real HTTP endpoint that triggers the workflow on each inbound request. It exposes two URLs at the top of the node panel: a Test URL (active only while you click "Listen for test event") and a Production URL (live whenever the workflow is Active). The HTTP Method field supports POST, GET, PUT, PATCH, DELETE, and HEAD. For an AI endpoint that receives a payload, use POST.

One Webhook node setting makes this behave like a real API instead of a fire-and-forget trigger. Set the Respond field to Using 'Respond to Webhook' Node, and you control the response body, status code, and headers completely.

Which three nodes do you need?#

The three required nodes are Webhook (receives the HTTP request), an AI node such as Basic LLM Chain or AI Agent with a Claude Chat Model sub-node (processes the prompt), and Respond to Webhook (sends the JSON answer back to the caller). Wire them left to right, and the workflow becomes a callable endpoint.

The Webhook node's Path field sets the route. Something like /ai-query works. The AI node reads the caller's payload using an expression: {{ $json.body.input }} pulls the input field from a POST body. The Respond to Webhook node's Respond With field offers several options: JSON (you define the body), First Incoming Item (passes the AI node's output straight through), Text, Binary File, and others. For a clean API, choose JSON and reference the AI node's output in the body.

The Respond to Webhook node also exposes Response Code (200, 400, 500, etc.) and Response Headers under Node options. Add a Content-Type: application/json header there so every client knows what it is receiving.

Six ordered steps to build an n8n AI API: Webhook node, Respond setting, AI node with Claude, output parser, Respond to Webhook, then Header Auth and activate.Each step touches one specific field, nothing left to guesswork.

How much does each AI API request cost?#

Serving 1,000 AI API requests costs about $2.00 in Claude tokens on Haiku 4.5 (as of mid-2026), assuming 500 input tokens and 300 output tokens per request. Self-hosted n8n adds $0 in platform fees. Output tokens drive the bill because they cost 5x more than input tokens on every Claude tier, so keeping your JSON response tight directly cuts costs.

The arithmetic: 1,000 requests at 500 input tokens each = 0.5M input tokens; 1,000 requests at 300 output tokens each = 0.3M output tokens. Multiply by the per-million price for each model.

ModelInput $/1MOutput $/1MCost per 1,000 requestsBest for
Claude Haiku 4.5$1.00$5.00$2.00Extraction, routing, short answers
Claude Sonnet 4.6$3.00$15.00$6.00Moderate reasoning, summaries
Claude Opus 4.8$5.00$25.00$10.00Complex analysis, long context

Prices are from Anthropic's official pricing page as of mid-2026 (June 2026). They change, so check before you budget. n8n self-hosted runs under the fair-code license, meaning the platform itself costs $0. n8n Cloud Starter is €20/month if you prefer managed hosting.

Start on Haiku. Reach for Sonnet only when the endpoint genuinely needs multi-step reasoning or longer context.

Table of Claude model pricing for an n8n AI API: Haiku 4.5 costs $2.00 per 1,000 requests, Sonnet 4.6 $6.00, Opus 4.8 $10.00, assuming 500 input and 300 output tokens.Assumes 500 input, 300 output tokens per request; self-hosted n8n is $0.

How do you secure a public AI endpoint?#

Set the Webhook node's Authentication to Header Auth and create a Header Auth credential with a header Name (for example x-api-key) and a secret header Value. Every caller must include that header on each request, or n8n rejects it before the workflow starts. This is built into the Webhook node; you write zero middleware.

The Header Auth credential lives in n8n's credential store and holds two fields: the header name and the expected value. Treat the value like a password: generate something random, store it in your calling app's environment variables, and rotate it if it leaks.

Beyond auth, add an input validation guard. A missing or malformed field should return a 400 before it reaches the AI node, not a confused model answer. Drop an IF node or a short Code node between the Webhook and the AI node. When validation fails, connect that branch to a second Respond to Webhook node that returns {"error": "bad_request"} with a 400 status code.

How do you guarantee the response is valid JSON?#

You guarantee valid JSON by setting the Respond to Webhook node's Respond With to JSON and attaching a Structured Output Parser sub-node to the AI node. The parser accepts a JSON Schema or a pasted example and forces the model to match that shape. Without it, Claude may wrap its answer in prose or markdown fences that break parsing.

The Structured Output Parser node (n8n-nodes-langchain.outputParserStructured) attaches as a sub-node directly to the Basic LLM Chain or AI Agent. Click "Generate from JSON Example," paste the exact structure you want back, and the parser builds the schema. From that point on, every response from the AI node is validated JSON. For a deeper dive into forcing structured output, see the n8n structured output guide.

Also add Content-Type: application/json in the Respond to Webhook node's Response Headers under Node options. It costs nothing and saves callers from having to guess the content type.

Linear flow of an n8n AI API request: caller hits the URL, Webhook receives it, Header Auth check, Claude processes the prompt, JSON returned to caller.Every inbound request follows this path through the workflow.

How do you build it in n8n, step by step?#

The full build is six steps: add the Webhook node, configure the Respond setting, add the AI node and Claude model, optionally add the Structured Output Parser, add the Respond to Webhook node, then lock it with Header Auth and activate. Each step touches a specific field, so nothing is left to guesswork.

Step 1: Add a Webhook node#

Drag a Webhook node onto the canvas. Set HTTP Method to POST. Set Path to something descriptive like /ai-query. The node shows both URLs at the top of the panel.

Step 2: Set Respond to "Using Respond to Webhook Node"#

In the Webhook node, find the Respond field and choose Using 'Respond to Webhook' Node. Save. This single setting is what separates a real API from a trigger.

Step 3: Add the AI node and attach Claude#

Add a Basic LLM Chain node and connect it after the Webhook node. Inside it, add a Claude Chat Model sub-node. Select Claude Haiku 4.5 as the model. Write your system prompt in the prompt field. Map the caller's payload with an expression:

code
{{ $json.body.input }}

This pulls the input field from the POST body. Adjust the field name to match what your callers send.

Add a Structured Output Parser sub-node to the Basic LLM Chain. Click Generate from JSON Example and paste the JSON shape you want. The parser enforces it on every response.

Step 5: Add the Respond to Webhook node#

Add a Respond to Webhook node at the end of the chain. Set Respond With to JSON. Reference the AI node's output in the body. Under Node options, set Response Code to 200 and add a Response Headers entry:

code
Content-Type: application/json

Step 6: Set Header Auth and activate#

Back in the Webhook node, set Authentication to Header Auth. Create a new Header Auth credential. Set the Name to x-api-key and the Value to a secret string. Click Activate on the workflow. Call the Production URL:

terminal
curl -X POST https://your-n8n-instance/webhook/ai-query \
  -H "Content-Type: application/json" \
  -H "x-api-key: YOUR_SECRET" \
  -d '{"input": "Summarize this for me: n8n is great."}'

The response comes back as JSON. Done.

Pros and cons of an n8n AI API endpoint versus a custom Express or FastAPI backend: n8n wins on speed and zero boilerplate, custom code wins on control at scale.Pick n8n for speed; pick custom code for maximum control at scale.

Where can this go wrong?#

The four most common failure points are: leaving Authentication as None (open relay), shipping the Test URL instead of the Production URL, skipping input validation so bad payloads reach the model, and relying on raw LLM output without a Structured Output Parser.

Open relay. Flip the workflow Active and share the URL without Header Auth, and any party with the URL sends requests at your expense. Webhook URLs do get crawled. Set Header Auth first.

Test URL vs. Production URL. The Test URL only listens while you are in the editor with "Listen for test event" active. The moment you close the editor or stop listening, it goes silent. The Production URL stays live as long as the workflow is Active. Shipping the Test URL means callers get no response after you close the tab.

Unvalidated input. A missing field does not cause a graceful 400; it causes a malformed expression error inside the AI node or a confused model answer. Add an IF node that checks for required fields and routes the bad path to a Respond to Webhook node returning a 400 before the AI node ever runs.

Non-JSON output. Without a Structured Output Parser, Claude may return Here is your answer: {...} or a markdown code fence. That string is not parseable JSON. The parser removes this problem entirely. See the n8n AI agent tools guide if your endpoint also needs to call live tools. For cost controls on per-request token spend, the Claude cost control agent workflow is worth reading. When the endpoint grows into a multi-tool agent, the n8n MCP AI agent guide covers that path.

Where to go from here#

Go from here by hosting the whole thing on a self-hosted n8n VPS for $0 in infrastructure, then adding the Structured Output Parser and Header Auth. The result is a private AI API that costs roughly $2.00 per 1,000 requests in tokens alone, a real backend alternative for solo operators. Start with one endpoint, prove the pattern, then replicate it.

Frequently asked questions

Can n8n expose a workflow as an API endpoint?
Yes. The Webhook node creates a real HTTP endpoint that triggers the workflow on each request. Pair it with a Respond to Webhook node to return any body and status code you choose.
Which n8n nodes turn a workflow into an AI API?
Three nodes: a Webhook node (receives the HTTP request), an AI node such as Basic LLM Chain or AI Agent with a Claude Chat Model (runs the prompt), and a Respond to Webhook node (returns the JSON answer).
How do I secure an n8n webhook endpoint?
Set the Webhook node's Authentication to Header Auth. Create a Header Auth credential with a header Name (e.g. x-api-key) and a secret Value. Callers must send that header or n8n rejects the request.
How much does an AI API request cost on Claude?
Assuming 500 input tokens and 300 output tokens per request, 1,000 requests costs about $2.00 on Claude Haiku 4.5, $6.00 on Sonnet 4.6, and $10.00 on Opus 4.8, as of mid-2026.
How do I make the response always valid JSON?
Attach a Structured Output Parser node to the AI node. It accepts a JSON Schema or a JSON example, and forces the model output to match that shape before the Respond to Webhook node sends it back.
What is the difference between the Test URL and the Production URL?
The Test URL only listens while you click 'Listen for test event' in the editor. The Production URL is always live once the workflow is Active. Never ship the Test URL to callers.
Which Claude model should I use for an API endpoint?
Start with Claude Haiku 4.5 ($1.00/$5.00 per 1M tokens). It handles extraction, routing, and short-answer tasks well at the lowest cost. Step up to Sonnet 4.6 only when the endpoint needs heavier reasoning.

Sources

Primary references and vendor documentation used while drafting and reviewing this article.

  1. n8n Webhook node documentation
  2. n8n Respond to Webhook node documentation
  3. n8n Webhook credentials (Header Auth)
  4. n8n Structured Output Parser node documentation
  5. Anthropic Claude pricing (Haiku 4.5, Sonnet 4.6, Opus 4.8)
  6. n8n Cloud pricing
  7. n8n sustainable use license (self-host free)

Written by

Muhammad Qasim Hammad
Muhammad Qasim Hammad
AI agents & automationFounder · Cart Gaze LLCPMP-certified PM

Muhammad Qasim Hammad is an AI agent and automation expert and the founder of Cart Gaze LLC (cartgaze.com). He builds product for the love of it: when an idea lands, a working prototype is usually running within hours, built with the same AI agents and automations he sells. He puts his own output at roughly 20× what it was before agents, and the Agentic OS behind this site is the working proof, documented in public with the tools he actually ran and what they really cost.

AI & Automation Services

Want a pipeline like this running in your business?

I'm Qasim — I design and ship AI agents and n8n automations for solo operators and small teams. Tell me what's eating your team's week, and I'll scope a fix.

Related reading