Building an Automated AI Image Generation Pipeline with n8n and ComfyUI
ComfyUI is a useful environment for building visual AI workflows, but a person clicking the Queue button is not an automation strategy. Once an image-generation workflow produces consistent results, it is natural to want to trigger it from an application, a scheduled process or an API.
I connected n8n to a local ComfyUI installation using a saved text-to-image workflow called image_krea2_turbo_t2i. Both applications were working, and the next question was how to manage asynchronous generation without keeping a request open indefinitely.
Separate orchestration from inference
ComfyUI should own model loading, graph execution and image creation. n8n should own triggering, input validation, workflow assembly, status tracking and delivery.
Client / application
|
v
n8n webhook -> validate -> submit workflow to ComfyUI
| |
| ComfyUI queue -> GPU
| |
+--- job ID / status <-----+--- history + saved image
|
v
Output storage or notification
The important distinction is between submission and completion. ComfyUI accepts a graph and returns an identifier. Generation may take seconds or minutes depending on queue depth and model state.
Export the API workflow
Start by exporting the workflow in API format from ComfyUI. The API representation uses node IDs, class types and inputs; it is not necessarily identical to the visual editor's project JSON. Keep a tested copy under version control alongside the required models and custom-node versions (without distributing restricted model files).
The saved graph in my setup included a SaveImage node with ID 29 and a resolution-selector node with ID 49. Those IDs are specific to my graph, not universal ComfyUI conventions. A prompt node must be identified from the actual API export.
For an independent submission test, use Python and a valid exported graph:
import json
import uuid
from pathlib import Path
import requests
base = "http://127.0.0.1:8188"
workflow = json.loads(Path("workflow_api.json").read_text())
response = requests.post(
f"{base}/prompt",
json={"prompt": workflow, "client_id": str(uuid.uuid4())},
timeout=15,
)
response.raise_for_status()
print(response.json()["prompt_id"])
Install requests in a virtual environment if needed. Adjust the host when n8n and ComfyUI are on different machines or containers. 127.0.0.1 inside a container refers to that container, not the Windows host.
Build the n8n workflow
Use a Webhook node to receive validated prompt text and dimensions. Then load or retrieve your API workflow and use a Code node to construct a fresh graph for each request. Replace the text input on the actual prompt node; do not concatenate an untrusted string into raw JSON.
Next, configure an HTTP Request node to send a POST to /prompt with the graph under the prompt key. Store the returned prompt_id in your job record. A typical public-facing integration returns HTTP 202 with an application job ID and exposes a separate status endpoint rather than waiting for the GPU to finish.
The local ComfyUI server exposes /history/{prompt_id} for results and /ws for execution events. For a simple n8n integration, a bounded Wait-and-poll loop is sufficient: wait a few seconds, query history, inspect execution status and stop on success, failure or a deadline. Do not treat an empty history object as an error immediately; the job may be queued or running. On completion, inspect the output node's image metadata and retrieve the image using /view with the returned filename, subfolder and type. Prefer constructing URL query parameters through the HTTP node rather than manual string concatenation.
For lower-latency notifications, add a small worker that listens on ComfyUI's WebSocket and calls an authenticated n8n webhook on completion. The WebSocket is not itself an outgoing HTTP webhook.
Make it reliable
Use an authenticated entrypoint, a maximum prompt size and image resolution, a queue limit, a persistent mapping of client request IDs to ComfyUI prompt IDs, and a terminal timeout. A retry after an uncertain submission can duplicate GPU work, so reconcile existing jobs before resubmitting. Store results outside temporary execution logs and define retention rules.
My integration established that n8n and ComfyUI can work together. The polling and callback architecture above is the design I would use to make that integration dependable for repeated application requests.
