I like poking at prompts in a browser as much as anyone, but the moment I want to wire generation into a script or a side project, I want an endpoint instead of a canvas. So I blocked out an hour, cloned ComfyUI, dropped in a Stable Diffusion checkpoint, and ended up with a local server I could call from curl and from Python.
Hands-on time was roughly 60 minutes. The unpredictable part was the model download — how long that takes depends entirely on the file size and your connection. I'd rate the whole thing as intermediate: nothing exotic, but you need to be comfortable in a terminal and able to read install docs for your own GPU.
One note on how I wrote this: I deliberately don't quote version pins, VRAM numbers, or benchmark timings, because those go stale fast. Where a value depends on your machine or your ComfyUI release, I say "check the docs" instead of pretending a number will hold.
What you need before you touch a terminal
Here's the checklist I ran through first:
- OS: Linux or macOS (the bash blocks below), or Windows 10/11 (use PowerShell; my paths are Linux/macOS, so swap
/for\). - Python 3: ComfyUI documents which Python versions it supports — read its docs rather than trusting whatever is on your PATH.
- git, to clone the repository.
- curl, which I use to prove the HTTP API works.
- A GPU: an NVIDIA card with a current driver makes generation fast. ComfyUI also runs CPU-only, but it's slow. Confirm the current hardware requirements in the docs.
- Disk space: checkpoints are multi-gigabyte. Look at the size on the model's download page before you start.
- Network access to GitHub and the PyTorch package index during installation.
Quick sanity check:
python3 --version git --version curl --version You also need one Stable Diffusion checkpoint in .safetensors format — either already on disk or a download link you can pull from.
Install ComfyUI and put a checkpoint where it looks
Clone, isolate, install:
git clone https://github.com/comfyanonymous/ComfyUI.git cd ComfyUI python3 -m venv venv source venv/bin/activate # Windows PowerShell: .\venv\Scripts\Activate.ps1 For PyTorch, go to the official "Get Started" selector and generate the install command for your OS and accelerator (CUDA, ROCm, or CPU). Those flags change between releases, so don't lift a flag set from an old blog post — including this one.
Then the remaining dependencies and the checkpoint itself:
pip install -r requirements.txt mkdir -p models/checkpoints cp /path/to/<your-model>.safetensors models/checkpoints/ The virtual environment matters: it keeps ComfyUI's packages away from your system Python. And models/checkpoints is not arbitrary — it's the folder the Load Checkpoint node scans, so a misplaced file simply won't show up in the dropdown.
Verify both pieces before moving on:
python -c "import torch; print(torch.__version__, torch.cuda.is_available())" ls -la models/checkpoints You want a version line ending in True (the exact version string will differ, which is fine) and a listing that contains your .safetensors. If torch.cuda.is_available() is False on a machine with an NVIDIA card, you installed the CPU-only build — rerun the PyTorch command from the selector.
Build the smallest working graph in the browser
Start the server bound to localhost:
python main.py --listen 127.0.0.1 --port 8188 Then open http://127.0.0.1:8188 and assemble the most boring graph that produces an image:
- Load Checkpoint — pick your
.safetensors. - CLIP Text Encode for the positive prompt, wired to the checkpoint's
CLIPoutput; a second one for the negative prompt. - Empty Latent Image — 512×512 is enough to start, so you can iterate quickly.
- KSampler — model, positive, negative, latent go in.
- VAE Decode — the sampler's
LATENTplus the checkpoint'sVAE. - Save Image — the decoded
IMAGE.
Hit Queue once and confirm you get a picture in the UI. Then use Save (API Format) from the menu to write workflow_api.json.
That export is the whole trick: the browser canvas and the HTTP API are two front ends onto the same engine, and the API accepts exactly the JSON you just saved. Anything you can assemble by hand, your own application can trigger.
Queue a job with curl
curl -s -X POST http://127.0.0.1:8188/prompt \ -H "Content-Type: application/json" \ --data-binary @workflow_api.json A successful call returns a JSON object with a prompt_id, something like {"prompt_id": "8f0c...", "number": 1, "node_errors": {}}. The field names are stable, but the full schema is version-specific — check the docs if something looks different.
One gotcha worth knowing before it bites you: the request body is expected to wrap the workflow in a prompt key. If you post the exported file bare and get a validation error back, that's why. The Python client below does the wrapping for you.
While the job runs, poll the history endpoint:
curl -s http://127.0.0.1:8188/history/<prompt_id> It returns {} (or a payload with no outputs) until the job finishes, then fills in the node outputs. Meanwhile a new PNG lands in ComfyUI/output/.
A minimal Python client
This is the 30-line version I keep around for testing:
import json, time, urllib.request BASE = "http://127.0.0.1:8188" with open("workflow_api.json") as f: workflow = json.load(f) payload = json.dumps({"prompt": workflow}).encode() req = urllib.request.Request( f"{BASE}/prompt", data=payload, headers={"Content-Type": "application/json"}, ) prompt_id = json.loads(urllib.request.urlopen(req).read())["prompt_id"] print("queued", prompt_id) while True: hist = json.loads(urllib.request.urlopen(f"{BASE}/history/{prompt_id}").read()) if prompt_id in hist: print(json.dumps(hist[prompt_id], indent=2)[:500]) break time.sleep(1) No dependencies, no client library — just the standard library and a one-second poll.
The endpoints I actually use
| Endpoint | Method | What it does |
|---|---|---|
/prompt | POST | Queues a workflow, returns a prompt_id |
/history/{prompt_id} | GET | Fetches results once the job is done |
/view?filename=... | GET | Downloads a generated image by filename |
/queue | GET | Shows pending and running jobs |
/interrupt | POST | Stops whatever is currently executing |
That's a small enough surface to wrap in any language. Paths and payload fields match the release you installed; if a call 404s, check the docs for your version before assuming you're doing something wrong.
Verify from the terminal, then close the door
Browser success only proves the UI works. What I actually care about is whether my application path works, so I run the script and look at the disk:
python client.py ls -la output/ | tail -n 5 You should see a prompt_id, then a chunk of history JSON, and a freshly timestamped .png in output/. Open the file — it should depict your prompt, not a black square or noise.
One failure mode I hit: the history showed a completed job while the folder stayed empty. That's almost always the Save Image node's filename_prefix pointing somewhere you weren't looking.
Now the part people skip. By default the server is bound to localhost, which is good. If you need remote access:
python main.py --listen 0.0.0.0 --port 8188 python main.py --port 8189 # a second instance on another port ComfyUI ships without user accounts. Anyone who can reach the port can queue jobs on your GPU, so terminate TLS and basic auth at nginx or Caddy and keep port 8188 off the public internet. Whether your particular build offers any auth option of its own is something to check in the docs — I wouldn't count on it.
Snags worth knowing in advance
-
torch.cuda.is_available()isFalse— CPU-only PyTorch build. Reinstall using the CUDA command from the official selector. - Checkpoint missing in the node dropdown — the file isn't in
models/checkpoints, or it isn't.safetensors. - Empty
output/after a completed job — checkfilename_prefixon the Save Image node. - 404 on an API call — your installed version may not expose that route or field; consult the docs for that release.
- Slow generations — expect CPU inference to be much slower than GPU. That's the tradeoff for not having CUDA set up.
The takeaway: you don't need a hosted service to get an image API. One clone, one checkpoint, one exported workflow, and about an hour gets you a localhost endpoint that accepts JSON and writes PNGs — and from there it's just normal application code.
This article was originally published on AI Release — https://ai-release.org. If you want more practical write-ups like this one, there's a channel at https://t.me/ai_release1.
