โšก n8n Automation Intermediate โฑ 20 min ๐Ÿ–ฅ๏ธ Local hardware

How n8n Connects to ComfyUI โ€” Automating AI Image & Video Workflows

A complete walkthrough of wiring n8n to ComfyUI so AI image and video generation runs hands-free in your pipeline.

Lesson Notes

The Question This Lesson Answers

You've got n8n running in a Docker container. You've got ComfyUI running directly on your Windows, Mac, or Linux machine (not in Docker). How do you get them to talk to each other? This trips up almost everyone the first time.

Why localhost Doesn't Work

When n8n runs inside Docker, it has its own isolated network. localhost inside that container refers to the container itself โ€” not your actual computer. So pointing n8n at localhost:8188 (ComfyUI's port) does nothing. You get a connection refused error and no clear explanation why.

The fix is host.docker.internal โ€” a special DNS hostname that Docker provides. It routes requests from inside the container out to the host machine where ComfyUI is actually running.

http://host.docker.internal:8188/prompt

Step 1 โ€” Export Your ComfyUI Workflow as API JSON

Build the workflow you want to run in ComfyUI. Once it produces images you're happy with, go to File โ†’ Export API (not the regular Export). This creates a JSON file with the exact format ComfyUI's API expects โ€” different from the visual workflow format.

Save it somewhere accessible. You'll paste the contents into n8n's HTTP request body.

Step 2 โ€” Build the n8n HTTP Request Node

  1. Method: POST
  2. URL: http://host.docker.internal:8188/prompt
  3. Authentication: None
  4. Headers: Content-Type: application/json
  5. Body: JSON โ†’ Expression mode

In the body, wrap your exported JSON like this:

{"prompt": [paste your ComfyUI API JSON here]}

Step 3 โ€” Wire in a Dynamic Prompt

Scroll through the JSON body to find the text input field inside the CLIP Text Encode node. Delete the hardcoded prompt text and drag in your image prompt variable from a previous node (e.g. a Google Sheets row). It will turn green when correctly linked.

Step 4 โ€” Fix the Seed (Important)

ComfyUI caches results by seed. If your seed is fixed, every execution returns the same cached image โ€” no new generation happens. Find the seed field in your JSON body and replace it with this expression:

{{Math.ceil(Math.random() * 1000000000000)}}

This generates a different random seed each run, so ComfyUI creates a fresh image every time.

Step 5 โ€” Handle the Response Options

In the HTTP request node, scroll to Options โ†’ Response and enable Include Response Headers and Status. Set the response format to Autodetect. This lets you see the full ComfyUI response including the prompt ID you'll need to poll for the result.

Step 6 โ€” Update Google Sheets When Done

Add a Google Sheets node after the ComfyUI call. Use Update Row, match on the ID column, and set the status column to complete. On the next run, your Google Sheets filter will skip this row and grab the next pending one.

Using Sub-Workflows

If your image generation is part of a larger pipeline (e.g. music + images + video), use n8n's Execute Workflow node to call a separate workflow from within your main one. Set the source to Database, choose the sub-workflow, and โ€” critically โ€” check Wait for Sub-Workflow to Complete. Without that, your main workflow continues before the images are ready.

Checklist Before You Run

  • ComfyUI is running and accessible at localhost:8188 in your browser
  • n8n HTTP request URL uses host.docker.internal:8188 (not localhost)
  • Workflow exported with File โ†’ Export API (not regular export)
  • Seed field replaced with random expression
  • Image prompt wired from upstream data source
๐Ÿ’ก Tip to Try

After wiring up the HTTP request to ComfyUI, add this JavaScript as your seed value in the JSON body: <code>{{Math.ceil(Math.random() * 1000000000000)}}</code>. Without it, a fixed seed means ComfyUI returns the cached image every time instead of generating a new one.

Tools mentioned in this lesson:

๐Ÿ’ฌ Q&A 1

A
Adiquet Jul 4, 2026

This is such a great tutorial - I was able to finally find someone that can explain it in a way I can understand.

โ–ฒ 0
C
CWO Bot Jul 4, 2026 โšก Hermes

Thanks for the kind words! I'm glad the tutorial helped make the n8n-ComfyUI connection clearer. These integrations can be tricky at first, but once you see how the data flows between nodes, it opens up a lot of automation possibilities. Keep experimenting with different workflows!

โ† Previous
First lesson in this category
● How n8n Connects to ComfyUI โ€” Automating AI Image & Video Workflows PHP 8.x UTF-8