ComfyUI has a built-in HTTP API on the same port as the UI. To run a workflow from code, export it in API format, then POST it to /prompt as {"prompt": <workflow>}.
Steps
- Export the workflow. The official example says to use File, then Export (API) in the interface to get the API-format JSON for a specific workflow.
- Queue it. From the repo's
basic_api_example.py:
import json
from urllib import request
def queue_prompt(prompt):
p = {"prompt": prompt}
data = json.dumps(p).encode("utf-8")
req = request.Request("http://127.0.0.1:8188/prompt", data=data)
request.urlopen(req)
prompt = json.load(open("workflow_api.json"))
queue_prompt(prompt)
- Edit inputs first if you need to: the exported JSON is keyed by node id, so a script changes a prompt or a seed by setting, for example,
prompt["6"]["inputs"]["text"], as the example does.
Other routes in server.py
| Route | Use |
|---|---|
GET /history/{prompt_id} | Result and outputs of a queued prompt |
GET /view | Fetch an output image |
GET /queue | Running and pending jobs |
POST /interrupt | Stop the current run |
GET /object_info | Every node and its inputs |
POST /upload/image | Upload an input image |
POST /free | Unload models and free memory |
GET /ws | WebSocket for live progress |
The repo's websockets_api_example.py shows the WebSocket flow. The port is 8188 unless you change it (ComfyUI default port), and you decide who can reach it. For freeing memory from a script see how to clear VRAM in ComfyUI. Workflows that use custom nodes need those nodes installed on the server: the ComfyUI custom node install guide.
Sources
- ComfyUI basic_api_example.py. github.com
- ComfyUI websockets_api_example.py. github.com
- ComfyUI server.py (routes). github.com