Streamline Your Workflows with the ComfyUI Execution API

12 May 2025
Streamline Your Workflows with the ComfyUI Execution API

The ComfyUI Workflow Execution API is designed to empower developers by simplifying the execution of complex workflows. This service enables you to run specified ComfyUI workflows, offering flexibility through customizable JSON parameters, such as seeds and prompts. With support for various input file formats and output in popular image formats like webp, jpg, or png, this API enhances your workflow automation. Whether you're generating images, processing videos, or executing intricate tasks, the ComfyUI Workflow Execution API provides a fast and efficient solution.

Common use cases include automating image generation for creative projects, processing video files for visual effects, and debugging workflows with advanced options. The API's ability to randomize parameters and return temporary files for troubleshooting makes it an invaluable tool for developers looking to streamline their processes.

Prerequisites

To get started with the ComfyUI Workflow Execution API, you will need an API key and a basic understanding of making API calls.

Execute ComfyUI Workflow

The "Execute ComfyUI Workflow" action allows you to run a specified ComfyUI workflow with ease. It solves the problem of executing complex workflows by providing a structured way to input parameters and receive outputs in desired formats. This action is categorized under workflow orchestration, making it ideal for developers looking to automate and streamline their processes.

Input Requirements: You must provide a JSON object that includes several parameters:

  • clearCache: A boolean indicating whether to reset the ComfyUI cache (default: false).
  • imageQuality: An integer specifying the output image quality, ranging from 0 to 100 (default: 95).
  • outputFormat: A string that denotes the desired output format, such as webp, jpg, or png (default: webp).
  • inputMediaFile: A string representing the input file, which can be an image, video, tar, or zip, or a URL to the input.
  • randomizeSeeds: A boolean to enable the randomization of seeds for each execution (default: true).
  • workflowDescription: A string that contains the ComfyUI workflow in JSON format.
  • provideTemporaryFiles: A boolean to include temporary files in the output (default: false).

Expected Output: The output will be a URL to the generated image or file, such as:

https://assets.cognitiveactions.com/invocations/4c2c889f-e943-4f3b-a602-4dd09650e910/6148a9de-f27f-4ffa-946c-f9cdd31e7108.webp

Use Cases for this specific action:

  • Image Generation: Quickly generate images based on various parameters for artistic or commercial projects.
  • Video Processing: Automate the processing of video files to extract frames or apply effects.
  • Debugging Workflows: Use the temporary files feature to troubleshoot and refine your workflows effectively.
  • Automation in Creative Processes: Streamline repetitive tasks in creative workflows, making it easier to focus on artistic elements.
import requests
import json

# Replace with your actual Cognitive Actions API key and endpoint
# Ensure your environment securely handles the API key
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
# This endpoint URL is hypothetical and should be documented for users
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute"

action_id = "ffdddfa9-17f5-477d-af61-0fee46298fd4" # Action ID for: Execute ComfyUI Workflow

# Construct the exact input payload based on the action's requirements
# This example uses the predefined example_input for this action:
payload = {
  "clearCache": false,
  "imageQuality": 80,
  "outputFormat": "webp",
  "randomizeSeeds": true,
  "workflowDescription": "{\n  \"3\": {\n    \"inputs\": {\n      \"seed\": 156680208700286,\n      \"steps\": 10,\n      \"cfg\": 2.5,\n      \"sampler_name\": \"dpmpp_2m_sde\",\n      \"scheduler\": \"karras\",\n      \"denoise\": 1,\n      \"model\": [\n        \"4\",\n        0\n      ],\n      \"positive\": [\n        \"6\",\n        0\n      ],\n      \"negative\": [\n        \"7\",\n        0\n      ],\n      \"latent_image\": [\n        \"5\",\n        0\n      ]\n    },\n    \"class_type\": \"KSampler\",\n    \"_meta\": {\n      \"title\": \"KSampler\"\n    }\n  },\n  \"4\": {\n    \"inputs\": {\n      \"ckpt_name\": \"SDXL-Flash.safetensors\"\n    },\n    \"class_type\": \"CheckpointLoaderSimple\",\n    \"_meta\": {\n      \"title\": \"Load Checkpoint\"\n    }\n  },\n  \"5\": {\n    \"inputs\": {\n      \"width\": 1024,\n      \"height\": 1024,\n      \"batch_size\": 1\n    },\n    \"class_type\": \"EmptyLatentImage\",\n    \"_meta\": {\n      \"title\": \"Empty Latent Image\"\n    }\n  },\n  \"6\": {\n    \"inputs\": {\n      \"text\": \"beautiful scenery nature glass bottle landscape, purple galaxy bottle,\",\n      \"clip\": [\n        \"4\",\n        1\n      ]\n    },\n    \"class_type\": \"CLIPTextEncode\",\n    \"_meta\": {\n      \"title\": \"CLIP Text Encode (Prompt)\"\n    }\n  },\n  \"7\": {\n    \"inputs\": {\n      \"text\": \"text, watermark\",\n      \"clip\": [\n        \"4\",\n        1\n      ]\n    },\n    \"class_type\": \"CLIPTextEncode\",\n    \"_meta\": {\n      \"title\": \"CLIP Text Encode (Prompt)\"\n    }\n  },\n  \"8\": {\n    \"inputs\": {\n      \"samples\": [\n        \"3\",\n        0\n      ],\n      \"vae\": [\n        \"4\",\n        2\n      ]\n    },\n    \"class_type\": \"VAEDecode\",\n    \"_meta\": {\n      \"title\": \"VAE Decode\"\n    }\n  },\n  \"9\": {\n    \"inputs\": {\n      \"filename_prefix\": \"ComfyUI\",\n      \"images\": [\n        \"8\",\n        0\n      ]\n    },\n    \"class_type\": \"SaveImage\",\n    \"_meta\": {\n      \"title\": \"Save Image\"\n    }\n  }\n}\n",
  "provideTemporaryFiles": false
}

headers = {
    "Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
    "Content-Type": "application/json",
    # Add any other required headers for the Cognitive Actions API
}

# Prepare the request body for the hypothetical execution endpoint
request_body = {
    "action_id": action_id,
    "inputs": payload
}

print(f"--- Calling Cognitive Action: {action.name or action_id} ---")
print(f"Endpoint: {COGNITIVE_ACTIONS_EXECUTE_URL}")
print(f"Action ID: {action_id}")
print("Payload being sent:")
print(json.dumps(request_body, indent=2))
print("------------------------------------------------")

try:
    response = requests.post(
        COGNITIVE_ACTIONS_EXECUTE_URL,
        headers=headers,
        json=request_body
    )
    response.raise_for_status() # Raise an exception for bad status codes (4xx or 5xx)

    result = response.json()
    print("Action executed successfully. Result:")
    print(json.dumps(result, indent=2))

except requests.exceptions.RequestException as e:
    print(f"Error executing action {action_id}: {e}")
    if e.response is not None:
        print(f"Response status: {e.response.status_code}")
        try:
            print(f"Response body: {e.response.json()}")
        except json.JSONDecodeError:
            print(f"Response body (non-JSON): {e.response.text}")
    print("------------------------------------------------")

Conclusion

The ComfyUI Workflow Execution API's "Execute ComfyUI Workflow" action offers significant benefits for developers. It simplifies the execution of complex workflows, enhances creativity through automation, and provides useful debugging options. As you explore this API, consider how it can streamline your processes and improve efficiency in your projects. The next step is to integrate this API into your applications and unlock its full potential for workflow automation.