ComfyUI with API

From A Technological Wiki
Revision as of 14:56, 3 November 2025 by Root (talk | contribs) (Created page with "ComfyUI allows to execute workflows from the command line using API workflows == Enable API workflows == You have to activate dev mode. In version 0.3.59 you have to click on comfyUI icon at top left, settings. Search for "dev mode" and activate it == Export a workflow in API version == Click on comfyUI icon at top left, File, Export (API) == Script for executing the workflow == <syntaxhighlight lang="python3"> import json from urllib import request, parse import rand...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

ComfyUI allows to execute workflows from the command line using API workflows

Enable API workflows

You have to activate dev mode. In version 0.3.59 you have to click on comfyUI icon at top left, settings. Search for "dev mode" and activate it

Export a workflow in API version

Click on comfyUI icon at top left, File, Export (API)

Script for executing the workflow

import json
from urllib import request, parse
import random

# load the workflow from file, assign it to variable named prompt_workflow
promptWorkflow = json.load(open('workflow_api.json'))

# give some easy-to-remember names to the nodes
loadImageNode = promptWorkflow["14"]
upscaleImageNode = promptWorkflow["71"]
wanImageToVideoNode = promptWorkflow["10"]
promptPositiveNode = promptWorkflow["13"]
KSamplerAdvancedNode = promptWorkflow["3"]

### Set workflow values from user input (parameter --source-image)
### @TODO: check that file exists before proceeding. If not, display error message
loadImageNode["inputs"]["image"] = @TODO: get from --source-image
sourceImageWidth = @TODO: get from --source-image
sourceImageHeight = @TODO: get from --source-image


# get origin image sizes and use them for the video output
upscaleImagNode["inputs"]["width"] = sourceImageWidth
upscaleImagNode["inputs"]["height"] = sourceImageHeight
wanImageToVideoNode["inputs"]["width"] = sourceImageWidth
wanImageToVideoNode["inputs"]["height"] = sourceImageHeight

# append the input prompt to the prompt list (parameter --prompt)
prompt_list = []
prompt_list.append(@TODO: get from --prompt)

# Set a random noise seed for the sampler
ksampler_node["inputs"]["noise_seed"] = random.randint(1, 18446744073709551614)

# Queue prompt
queue_prompt(prompt_workflow)