Skip to content

ScjMitsui/ComfyUI2API

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ComfyUI2API

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.

Features

  • 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 .json file into workflows/
  • 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

Quick Start

1. Clone & install

git clone https://github.com/<your-username>/ComfyUI2API.git
cd ComfyUI2API
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

2. Configure

cp .env.example .env

Edit .env and set your ComfyUI server URL:

COMFYUI_URL=http://your-comfyui-host:8188
API_PORT=8881
API_KEY=           # optional — leave empty for no auth

3. Add workflows

Export 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.

4. Run

python run.py

The API will be available at http://localhost:8881. Interactive docs at http://localhost:8881/docs.

API Endpoints

GET /v1/models

List available models (workflows).

POST /v1/images/generations

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
  }'

POST /v1/chat/completions

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.

GET /health

Health check — reports ComfyUI connectivity and loaded workflows.

Configuration

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)

Project Structure

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

License

MIT

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages