A lightweight Python FastAPI proxy that exposes a local ComfyUI instance to the network with authentication and simplified workflow execution.
- Remote Access: Access your local ComfyUI from other devices on the network.
- Authentication: Secure your endpoint with API Key authentication (
X-API-Key). - Synchronous Execution: Queue a workflow and wait for the result in a single HTTP request.
- Dynamic Output: Automatically detects and returns the generated image, video, or text.
- Progress Tracking: Stream real-time progress updates via Server-Sent Events (SSE).
- Graceful Shutdown: Automatically interrupts running workflows and frees VRAM when the proxy stops.
- Image Upload: Helper endpoint to upload images for Image-to-Image or Image-to-Video workflows.
- Python 3.10+
- A running instance of ComfyUI (default port 7337, configurable)
- Important: If running via Docker, ComfyUI must be started with
--listento accept connections from the container.
-
Clone the repository:
git clone https://github.com/yourusername/comfyui-api-proxy.git cd comfyui-api-proxy -
Create a virtual environment (Recommended):
python -m venv venv # Windows .\venv\Scripts\activate # Linux/Mac source venv/bin/activate
-
Install dependencies:
pip install -r requirements.txt
You can configure the proxy using environment variables. You can either set them in your shell or create a .env file in the root directory:
COMFY_HOST=127.0.0.1
COMFY_PORT=7337
COMFY_API_KEY=your-secret-key-here| Variable | Description | Default |
|---|---|---|
COMFY_HOST |
Host where ComfyUI is running | 127.0.0.1 |
COMFY_PORT |
Port where ComfyUI is running | 7337 |
COMFY_API_KEY |
Secret key for authentication | Randomly generated if not set |
PORT |
Port for this proxy server | 8189 |
You can build and run the proxy as a Docker container.
-
Build the image:
docker build -t comfyui-proxy . -
Run the container:
# For Windows/Mac (Docker Desktop): docker run -p 8189:8189 \ -e COMFY_HOST=host.docker.internal \ -e COMFY_PORT=7337 \ -e COMFY_API_KEY=my-secret-key \ comfyui-proxy # For Linux: # Use your host's LAN IP (e.g., 192.168.1.100) instead of host.docker.internal
Note: Ensure your local ComfyUI instance is running with
--listen(e.g.,python main.py --listen) so it can accept connections from the Docker container.
-
Start the server (Local Python):
python main.py
The server will start on
http://0.0.0.0:8189. Check the console output for the generated API Key if you didn't set one. -
Important Note on Workflows:
⚠️ CRITICAL: Before using a workflow with this API, you MUST ensure it works natively in ComfyUI first.- Open ComfyUI in your browser.
- Create/Load your workflow.
- Enable "Dev Mode" in ComfyUI settings to see the "Save (API Format)" button.
- Save the workflow as API JSON.
- Use this JSON as the payload for the API.
-
Run a Workflow (Synchronous): Send a POST request to
/run_workflowwith your workflow JSON.curl -X POST "http://localhost:8189/run_workflow" \ -H "X-API-Key: secret-key" \ -H "Content-Type: application/json" \ -d @your_workflow_api.json \ --output result.png
-
Run a Workflow (Streaming Progress): Send a POST request to
/run_workflow_streamto get real-time updates via SSE.curl -N -X POST "http://localhost:8189/run_workflow_stream" \ -H "X-API-Key: secret-key" \ -H "Content-Type: application/json" \ -d @your_workflow_api.json
Streamed Events:
event: progress->data: {"type": "progress", "data": {"value": 1, "max": 10}}event: executing->data: {"type": "executing", "data": {"node": "123"}}event: result->data: {"type": "result", "data": {"filename": "...", "subfolder": "...", "type": "output"}}
-
Upload an Image (for Img2Img/Img2Vid):
curl -X POST "http://localhost:8189/upload" \ -H "X-API-Key: secret-key" \ -F "image=@input.jpg"
Use the returned filename in your workflow JSON (e.g., in the
LoadImagenode).
POST /run_workflow: Execute a workflow and get the result (Image/Video/Text).POST /run_workflow_stream: Execute a workflow and stream progress events (SSE).POST /upload: Upload an image file.GET /ws: WebSocket proxy (requirestokenquery param).GET /*: Proxy all other ComfyUI static assets and endpoints.
This usually means the workflow failed to execute in ComfyUI.
- Check the API Logs: The proxy now logs the exact error from ComfyUI. Look for
ComfyUI Error. - Verify Locally: Run the exact same workflow JSON in ComfyUI manually. If it fails there (e.g., missing nodes, model load errors), it will fail in the proxy.
- Custom Nodes: Errors like
OSErrororModuleNotFoundin custom nodes (e.g.,pig.py,ComfyUI-GGUF) are common. Ensure your ComfyUI environment is correctly set up and all dependencies are installed.
If the Docker container cannot connect to ComfyUI:
- Ensure ComfyUI is running with
--listen(e.g.,python main.py --listen). - Use
COMFY_HOST=host.docker.internal(Windows/Mac) or your LAN IP (Linux). - Check if a firewall is blocking the connection.
To access this API from outside your local network (e.g., over the internet), you have two main options:
If you have a public IP address, you can forward the port on your router.
- Log in to your router's admin panel.
- Find the Port Forwarding / NAT settings.
- Forward external port
8189(or your configured port) to your local machine's IP address (e.g.,192.168.1.X) on port8189. - You can then access the API via
http://<YOUR_PUBLIC_IP>:8189.
⚠️ SECURITY WARNING: Port forwarding exposes your machine directly to the internet. Ensure you have a strongCOMFY_API_KEYset.
Use a service like Ngrok or Cloudflare Tunnel to expose the port without opening your router.
# Example with Ngrok
ngrok http 8189This gives you a secure public URL (e.g., https://xyz.ngrok-free.app) that tunnels to your local API.
MIT