Streamline Your Workflow Automation with ComfyUI Cognitive Actions

23 Apr 2025
Streamline Your Workflow Automation with ComfyUI Cognitive Actions

In the realm of application development, integrating advanced workflows can significantly enhance functionality and user experience. The fofr/any-comfyui-workflow API offers a powerful set of Cognitive Actions designed to execute workflows within ComfyUI. These pre-built actions allow developers to easily handle various multimedia inputs, customize workflows, and output results in different formats. By leveraging these actions, you can automate complex processes and ensure your applications operate smoothly and efficiently.

Prerequisites

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

  • An API key for the Cognitive Actions platform.
  • Basic knowledge of JSON structure and Python programming.
  • An understanding of how to structure API requests conceptually, particularly passing the API key in headers.

Authentication is typically handled via an API key included in the request headers, allowing secure access to the actions available in the ComfyUI workflow.

Cognitive Actions Overview

Execute ComfyUI Workflow

The Execute ComfyUI Workflow action is designed to run any workflow within ComfyUI using the specified API JSON format. This action supports a wide range of input options, including images, videos, and compressed files, and it allows for customization through various parameters.

Input

The input for this action is structured as follows:

{
  "inputFile": "string (uri)",
  "outputFormat": "string (enum: webp, jpg, png)",
  "workflowJson": "string",
  "outputQuality": "integer (0-100)",
  "randomizeSeeds": "boolean",
  "forceResetCache": "boolean",
  "returnTempFiles": "boolean"
}
  • inputFile: URI of the input file (image, video, tar, or zip). URLs can also be utilized.
  • outputFormat: The desired format for the output images, with options including webp, jpg, or png. The default is webp.
  • workflowJson: A JSON string representing your ComfyUI workflow. Ensure to use the API version obtained from ComfyUI.
  • outputQuality: The quality of the output images, ranging from 0 (lowest) to 100 (highest). Default is 95.
  • randomizeSeeds: When set to true, seeds are randomized for each execution. Default is true.
  • forceResetCache: If true, resets the ComfyUI cache prior to execution. Default is false.
  • returnTempFiles: If true, returns temporary files for debugging. Default is 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,
  "randomizeSeeds": true,
  "forceResetCache": false,
  "returnTempFiles": false
}

Output

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

Example Output:

[
  "https://assets.cognitiveactions.com/invocations/6a04084a-41a2-4db0-9ba7-6072d1c5f572/4f4037ad-fded-47ae-b3b7-13f33cc8cf18.webp"
]

Conceptual Usage Example (Python)

Here’s how you might structure a conceptual Python code snippet to call this action:

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 = "3dcaa452-4f28-4293-93ca-fa094b7be3d1"  # Action ID for Execute ComfyUI Workflow

# Construct the input payload based on the action's requirements
payload = {
    "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  ...\n}",
    "outputQuality": 80,
    "randomizeSeeds": 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 snippet, you'll replace "YOUR_COGNITIVE_ACTIONS_API_KEY" with your actual API key. The action_id is set to the ID for the Execute ComfyUI Workflow action, and the input payload is structured according to the requirements discussed. The response handling section provides a way to handle potential errors gracefully.

Conclusion

Integrating the Execute ComfyUI Workflow Cognitive Action into your application can significantly streamline your multimedia processing tasks. With the flexibility to customize workflows, choose output formats, and control various parameters, developers can create dynamic and efficient applications. As you explore these capabilities, consider potential use cases such as automated content generation, media processing, or even interactive applications that leverage the power of ComfyUI workflows. Start integrating today and enhance your application’s functionality!