Mastering Workflow Automation with ComfyUI Cognitive Actions

22 Apr 2025
Mastering Workflow Automation with ComfyUI Cognitive Actions

In the realm of image and video processing, the fofr/any-comfyui-workflow Cognitive Actions offer developers a powerful API for orchestrating complex workflows using ComfyUI. These actions allow for seamless integration of image and video inputs, customizable configurations, and high-quality outputs. By leveraging these pre-built actions, developers can save time and enhance their applications' capabilities, enabling them to focus on delivering unique user experiences.

Prerequisites

Before diving into the integration of Cognitive Actions, ensure you have the following:

  • An API key for accessing the Cognitive Actions platform.
  • Basic knowledge of JSON and API requests.
  • Familiarity with Python programming, as we will provide conceptual examples in this language.

Authentication typically involves passing your API key in the headers of your API requests, allowing secure access to the available actions.

Cognitive Actions Overview

Execute ComfyUI Workflow

The Execute ComfyUI Workflow action allows you to run any workflow using ComfyUI. This action supports flexible image and video inputs through URLs or local uploads and enables customizable JSON configurations. The output can be generated in various formats, including webp, jpg, or png, and it supports random seed generation for diverse results.

Input

The input for this action consists of the following fields:

  • inputFile (string, required): URI to an image, video, tar, or zip file. You can also use URLs in your JSON workflow to allow automatic downloading. More guidance can be found here.
  • outputFormat (string, optional): Specifies the format for the output images. Defaults to webp. Options are webp, jpg, and png.
  • workflowJson (string, required): ComfyUI workflow provided as a JSON string or URL in API format. Retrieve this from ComfyUI using 'Save (API format)'. Instructions are available here.
  • outputQuality (integer, optional): Defines the quality of the output images on a scale from 0 (lowest quality) to 100 (highest quality). Default is 95.
  • randomiseSeeds (boolean, optional): Decides whether to automatically randomize seeds. Defaults to true.
  • forceResetCache (boolean, optional): Determines whether to reset the ComfyUI cache before workflow execution. Defaults to false.
  • returnTempFiles (boolean, optional): Specifies whether to return temporary files for debugging purposes. Defaults to false.

Example Input:

{
  "outputFormat": "webp",
  "workflowJson": "{\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",
  "outputQuality": 80,
  "randomiseSeeds": true,
  "forceResetCache": false,
  "returnTempFiles": false
}

Output

Upon successful execution, the action typically returns a URL link to the processed output.

Example Output:

[
  "https://assets.cognitiveactions.com/invocations/3c01685c-eb32-4f2a-954a-ac830fd7615b/b28b7f2a-d0e8-4d2d-9d6f-56f938b5a75e.webp"
]

Conceptual Usage Example (Python)

Here’s how you might call the Execute ComfyUI Workflow action using Python:

import requests
import json

# Replace with your Cognitive Actions API key and endpoint
COGNITIVE_ACTIONS_API_KEY = "YOUR_COGNITIVE_ACTIONS_API_KEY"
COGNITIVE_ACTIONS_EXECUTE_URL = "https://api.cognitiveactions.com/actions/execute"  # Hypothetical endpoint

action_id = "61aefe5b-b0e5-49d1-9fd2-5ded8eb50237"  # Action ID for Execute ComfyUI Workflow

# Construct the input payload based on the action's requirements
payload = {
    "outputFormat": "webp",
    "workflowJson": "{...}",  # Provide your workflow JSON here
    "outputQuality": 80,
    "randomiseSeeds": true,
    "forceResetCache": false,
    "returnTempFiles": false
}

headers = {
    "Authorization": f"Bearer {COGNITIVE_ACTIONS_API_KEY}",
    "Content-Type": "application/json"
}

try:
    response = requests.post(
        COGNITIVE_ACTIONS_EXECUTE_URL,
        headers=headers,
        json={"action_id": action_id, "inputs": payload}  # Hypothetical structure
    )
    response.raise_for_status()  # Raise an exception for bad status codes (4xx or 5xx)

    result = response.json()
    print("Action executed successfully:")
    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: {e.response.text}")

In this example, replace {...} in workflowJson with your specific workflow JSON string. The action_id corresponds to the Execute ComfyUI Workflow action, and the endpoint URL is illustrative.

Conclusion

The fofr/any-comfyui-workflow Cognitive Actions present a powerful way to automate and manage image and video processing workflows within your applications. With its flexible input options, customizable settings, and high-quality output formats, you can easily enhance your projects and provide unique features to your users.

Explore the possibilities of integrating these Cognitive Actions into your applications, and consider experimenting with different workflows to see what innovative solutions you can create!