An OpenAI-compatible API wrapper for ComfyUI. Drop your ComfyUI workflow JSON files into a directory and instantly expose them as /v1/images/generations and /v1/chat/completions endpoints — compatible with any client that speaks the OpenAI API.
- OpenAI-compatible endpoints —
/v1/images/generations,/v1/chat/completions,/v1/models - Drop-in workflow support — Export a workflow from ComfyUI (API format) and drop the
.jsonfile intoworkflows/ - Automatic parameter injection — Prompts, dimensions, seeds, and batch sizes are injected into your workflow automatically
- Streaming support — SSE streaming for chat completions
- Bearer token auth — Optional API key authentication
- Auto-cleanup — Generated images are automatically cleaned up after a configurable TTL
git clone https://github.com/<your-username>/ComfyUI2API.git
cd ComfyUI2API
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txtcp .env.example .envEdit .env and set your ComfyUI server URL:
COMFYUI_URL=http://your-comfyui-host:8188
API_PORT=8881
API_KEY= # optional — leave empty for no authExport a workflow from ComfyUI in API format (Save (API Format) button) and drop the .json file into the workflows/ directory. You can optionally add a .yaml sidecar file to configure injection point mappings.
python run.pyThe API will be available at http://localhost:8881. Interactive docs at http://localhost:8881/docs.
List available models (workflows).
Generate images — OpenAI Images API compatible.
curl -X POST http://localhost:8881/v1/images/generations \
-H "Content-Type: application/json" \
-d '{
"prompt": "a beautiful sunset over mountains",
"size": "1024x1024",
"n": 1
}'Chat completions that generate images — OpenAI Chat API compatible.
curl -X POST http://localhost:8881/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "your-workflow-name",
"messages": [
{"role": "user", "content": "a cyberpunk cityscape at night"}
]
}'Supports --size 512x768, --width, and --height inline in prompts.
Health check — reports ComfyUI connectivity and loaded workflows.
All settings are loaded from environment variables (.env file):
| Variable | Default | Description |
|---|---|---|
COMFYUI_URL |
http://localhost:8188 |
ComfyUI server URL |
API_PORT |
8881 |
Port for this API server |
API_KEY |
(empty) | Bearer token for auth (empty = no auth) |
DEFAULT_MODEL |
(empty) | Default workflow name (empty = first found) |
IMAGE_TTL_SECONDS |
3600 |
How long generated images are kept |
WORKFLOWS_DIR |
workflows |
Directory containing workflow JSONs |
BASE_URL |
(empty) | Base URL for image serving (auto-detected) |
ComfyUI2API/
├── run.py # Entry point
├── requirements.txt # Python dependencies
├── .env.example # Example environment config
├── workflows/ # Drop ComfyUI workflow JSONs here
├── outputs/ # Generated images (auto-cleaned)
└── app/
├── main.py # FastAPI app setup & lifespan
├── config.py # Settings from environment
├── comfyui/
│ ├── client.py # Async ComfyUI HTTP + WebSocket client
│ └── workflow_manager.py # Workflow loading & parameter injection
├── routers/
│ ├── chat.py # /v1/chat/completions
│ ├── images.py # /v1/images/generations
│ ├── models.py # /v1/models
│ └── health.py # /health
├── models/
│ └── openai_models.py # Pydantic models for OpenAI compat
├── middleware/
│ └── auth.py # Bearer token authentication
└── services/
└── output_manager.py # Image storage & TTL cleanup
MIT