AnyComfy API 首次上手
2025/07/28

AnyComfy API 首次上手

本文档主要介绍如何使用 AnyComfy API 进行图像生成

1. 准备工作

若要通过API接口运行ComfyUI工作流,需要先获取相应工作流的JSON配置文件(API兼容格式)。以下的操作流程同样适用于本地部署的ComfyUI WebUI操作界面。

获取工作流文件

点击"Try ComfyUI on AnyComfy"按钮,进入AnyComfy提供的ComfyUI WebUI操作界面。 Homepage

在ComfyUI WebUI操作界面中,选择您需要的工作流。 Choose Workflow

在工作流界面中,点击左上角导航栏中的"Workflow"按钮,然后选择"Export(API)"选项,浏览器将自动下载一个JSON格式的工作流配置文件。 Export Workflow

2. 使用POSTMAN调用API

Postman 是一款主流的 API 开发和测试工具,用于发送 HTTP 请求、测试 API 端点并管理接口文档。

如果您尚未安装 Postman,请访问"Postman 官方网站"下载安装。

创建请求

打开POSTMAN,新建一个POST请求,并将请求命名为"prompt"。 Create Request

配置请求参数

  • 请求URL:
https://anycomfy.com/api/prompt
  • 请求体格式:选择"raw"和"JSON" Configure Request

设置请求内容

  • 将下载的工作流JSON文件内容粘贴到"prompt"字段
  • 在"api_key"字段填入您的API密钥
{
    "prompt": {"您的工作流内容"},
    "api_key": "您的API密钥"
}

本文档中的示例工作流:

(您只需要将"您的API密钥"部分替换为您的实际API密钥)

{
  "prompt": {
    "8": {
      "inputs": {
        "samples": [
          "40",
          0
        ],
        "vae": [
          "10",
          0
        ]
      },
      "class_type": "VAEDecode",
      "_meta": {
        "title": "VAE Decode"
      }
    },
    "10": {
      "inputs": {
        "vae_name": "ae.safetensors"
      },
      "class_type": "VAELoader",
      "_meta": {
        "title": "Load VAE"
      }
    },
    "11": {
      "inputs": {
        "clip_name1": "t5xxl_fp8_e4m3fn.safetensors",
        "clip_name2": "clip_l.safetensors",
        "type": "flux",
        "device": "default"
      },
      "class_type": "DualCLIPLoader",
      "_meta": {
        "title": "DualCLIPLoader"
      }
    },
    "17": {
      "inputs": {
        "scheduler": "normal",
        "steps": 25,
        "denoise": 1,
        "model": [
          "46",
          0
        ]
      },
      "class_type": "BasicScheduler",
      "_meta": {
        "title": "BasicScheduler"
      }
    },
    "38": {
      "inputs": {
        "model": [
          "46",
          0
        ],
        "conditioning": [
          "42",
          0
        ]
      },
      "class_type": "BasicGuider",
      "_meta": {
        "title": "BasicGuider"
      }
    },
    "39": {
      "inputs": {
        "filename_prefix": "ComfyUI",
        "images": [
          "8",
          0
        ]
      },
      "class_type": "SaveImage",
      "_meta": {
        "title": "Save Image"
      }
    },
    "40": {
      "inputs": {
        "noise": [
          "45",
          0
        ],
        "guider": [
          "38",
          0
        ],
        "sampler": [
          "47",
          0
        ],
        "sigmas": [
          "17",
          0
        ],
        "latent_image": [
          "44",
          0
        ]
      },
      "class_type": "SamplerCustomAdvanced",
      "_meta": {
        "title": "SamplerCustomAdvanced"
      }
    },
    "42": {
      "inputs": {
        "guidance": 3.5,
        "conditioning": [
          "43",
          0
        ]
      },
      "class_type": "FluxGuidance",
      "_meta": {
        "title": "FluxGuidance"
      }
    },
    "43": {
      "inputs": {
        "text": "best quality,a cute anime girl,sunshine,soft features,swing,a blue white edged dress,solo,flower,blue eyes,blush,blue flower,long hair,barefoot,sitting,looking at viewer,blue rose,blue theme,rose,light particles,pale skin,blue background,off shoulder,full body,smile,collarbone,long hair,blue hair,vines,plants,",
        "clip": [
          "11",
          0
        ]
      },
      "class_type": "CLIPTextEncode",
      "_meta": {
        "title": "CLIP Text Encode (Prompt)"
      }
    },
    "44": {
      "inputs": {
        "width": 1024,
        "height": 1024,
        "batch_size": 1
      },
      "class_type": "EmptySD3LatentImage",
      "_meta": {
        "title": "EmptySD3LatentImage"
      }
    },
    "45": {
      "inputs": {
        "noise_seed": 1114957019097397
      },
      "class_type": "RandomNoise",
      "_meta": {
        "title": "RandomNoise"
      }
    },
    "46": {
      "inputs": {
        "max_shift": 1.15,
        "base_shift": 0.5,
        "width": 1024,
        "height": 1024,
        "model": [
          "48",
          0
        ]
      },
      "class_type": "ModelSamplingFlux",
      "_meta": {
        "title": "ModelSamplingFlux"
      }
    },
    "47": {
      "inputs": {
        "sampler_name": "euler"
      },
      "class_type": "KSamplerSelect",
      "_meta": {
        "title": "KSamplerSelect"
      }
    },
    "48": {
      "inputs": {
        "unet_name": "flux1-dev.safetensors",
        "weight_dtype": "default"
      },
      "class_type": "UNETLoader",
      "_meta": {
        "title": "Load Diffusion Model"
      }
    }
  },
  "api_key": "您的API密钥"
}

Input Request

发送请求并查看响应

点击右上角的"Send"按钮发送请求,即可在响应区域查看API返回的结果。

API响应中的images字段包含一个base64编码的图片数据数组,您需要将其解码才能获得实际的图片文件。 Response Result

通过对返回的base64编码进行解码处理后,最终得到的图片如下: Decoded Image

3. 通过Python代码调用API

您同样可以通过编程的方式调用该API接口,以下是我们提供的参考示例代码。

准备环境

requests 是一个主流的 Python HTTP 库,用于发送 HTTP 请求。

如果您还没有安装 requests,请使用以下命令进行安装:

pip install requests

或者使用 conda 安装:

conda install requests

Python示例代码

接下来,您需要准备一个工作流配置文件,具体方法如前所述,然后将其命名为"prompt.json"。

随后,在同一目录下创建一个Python文件,例如"test_api.py",并复制下方的示例代码:

test_api.py
import requests
import json

# 配置部分
url = "https://anycomfy.com/api/prompt"
api_key = "xxxxxxxx"  # 替换为您的API密钥
json_file_path = "prompt.json"  # 确保文件存在

# 读取本地JSON文件
try:
    with open(json_file_path, "r", encoding="utf-8") as f:
        prompt_data = json.load(f)
except FileNotFoundError:
    print(f"Error: File {json_file_path} not found.")
    exit(1)
except json.JSONDecodeError:
    print(f"Error: File {json_file_path} is not a valid JSON format.")
    exit(1)

# 构造请求负载
payload = {
    "prompt": prompt_data,
    "api_key": api_key
}

# 发送POST请求
try:
    response = requests.post(url, json=payload)
except requests.exceptions.ConnectionError:
    print("Connection failed, please check your internet connection or confirm if anycomfy.com is accessible.")
    exit(1)

# 输出响应结果
print("Status code:", response.status_code)
print("Response content:")
try:
    print(json.dumps(response.json(), indent=2, ensure_ascii=False))
except json.JSONDecodeError:
    print(response.text)

项目结构

prompt.json
test_api.py

执行结果

在控制台中运行Python文件:

python test_api.py

如果一切正常,您将看到类似以下的输出结果:

Status code:: 200
Response content::
{
  "images": ["base64编码的图片数据"],
  "elapsed": 1.183,
  "cost": 0.01
}
  • images: 包含base64编码图片数据的数组
  • elapsed: 任务执行耗时(单位:秒)
  • cost: 消耗的积分数量

Python Output

4. 注意事项

  1. 请妥善保管您的API密钥
  2. 确保工作流文件格式正确
  3. 注意API调用的积分消耗
  4. 网络连接不稳定时建议增加异常处理

邮件列表

加入我们的社区

订阅邮件列表,及时获取最新消息和更新