Seamlessly Execute ComfyUI Workflows with Cognitive Actions

26 Apr 2025
Seamlessly Execute ComfyUI Workflows with Cognitive Actions

Integrating AI into your applications has never been easier, especially with the "Any ComfyUI Workflow" service. This powerful API allows developers to execute any ComfyUI workflow, enabling a vast range of creative possibilities. By leveraging the flexibility of this service, you can customize workflows, modify settings, and integrate various input types, including images and controlnet. The benefits of using this API include enhanced speed, improved efficiency, and simplified processes, making it a valuable tool for developers looking to automate image processing and other tasks.

Common use cases for the Any ComfyUI Workflow include generating unique images based on textual prompts, automating image enhancement processes, and creating dynamic visual content for applications. Whether you are a game developer looking to generate assets, a marketer creating visuals for campaigns, or a researcher analyzing image data, this workflow offers solutions tailored to your needs.

Prerequisites

Before diving into the implementation, ensure you have a Cognitive Actions API key and a basic understanding of making API calls. This will allow you to authenticate your requests and seamlessly integrate the service into your applications.

Execute ComfyUI Workflow

The Execute ComfyUI Workflow action is designed to run any ComfyUI workflow using the Replicate API. This action provides developers with the ability to customize their workflows by modifying JSON settings such as seeds, prompts, and input types.

Purpose

This action addresses the need for flexibility in executing complex workflows. It allows you to easily integrate various inputs and control output formats, quality, and debugging features, making it ideal for developers who require precise control over their image processing tasks.

Input Requirements

To successfully utilize this action, you will need to provide the following inputs:

  • Input File: A URI string for an input image, tar, or zip file. You can also provide URLs in your JSON workflow for the API to download.
  • Output Format: Choose from webp, jpg, or png for the output image format; the default is webp.
  • Workflow Json: A string representing your ComfyUI workflow in JSON format. Make sure to use the API version of your workflow.
  • Output Quality: An integer defining the image quality, ranging from 0 to 100, with a default of 95.
  • Randomize Seeds: A boolean indicating whether to randomize seeds, defaulting to true.
  • Force Reset Cache: A boolean to forcibly reset ComfyUI's cache before execution, useful for debugging.
  • Return Temp Files: A boolean specifying whether to return temporary files for debugging purposes.

Expected Output

The output of this action will be a URL pointing to the generated image, based on the workflow you executed. For example:

[
  "https://assets.cognitiveactions.com/invocations/890b2e26-6606-4218-b4fd-d90b7c72ceee/6d887a38-7613-4630-91f3-3a39faed321b.webp"
]

Use Cases for this Specific Action

  • Image Generation: Create unique images based on user-defined prompts, perfect for applications in gaming and marketing.
  • Automated Workflows: Streamline image processing tasks, such as enhancement and filtering, by automating workflows.
  • Debugging Visuals: Use the returnTempFiles option to analyze intermediate outputs for troubleshooting and optimization.

```python
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 = "b4feb299-9365-4645-b56d-0531bad222f3" # 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 = {
  "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
}

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 Any ComfyUI Workflow service provides a robust solution for executing customizable workflows with ease. By harnessing the flexibility of the Execute ComfyUI Workflow action, developers can automate image processing tasks, generate creative content, and enhance their applications' capabilities. As you explore this API, consider how it can streamline your development process and unlock new possibilities in your projects. 

Take the next step by integrating this action into your applications and experience the power of automated workflows today!