# Python 3.10+: pip install comfy-sdk==0.4.0
import os
from comfy_sdk import Comfy
api_key = os.environ.get("COMFY_API_KEY", "").strip()
if not api_key: raise RuntimeError("Set COMFY_API_KEY")
os.environ.setdefault("COMFY_BASE_URL", "https://cloud.comfy.org")
client = Comfy(api_key=api_key)
workflow = client.workflows.from_json({
"3": {
"class_type": "KSampler",
"inputs": {
"cfg": 2.5,
"denoise": 1,
"latent_image": [
"122",
0
],
"model": [
"66",
0
],
"negative": [
"108",
1
],
"positive": [
"108",
0
],
"sampler_name": "euler",
"scheduler": "simple",
"seed": 134554158057228,
"steps": 20
},
"_meta": {
"title": "KSampler"
}
},
"6": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"38",
0
],
"text": "The Queen, on a throne, surrounded by Knights, HD, Realistic, Octane Render, Unreal engine"
},
"_meta": {
"title": "CLIP Text Encode (Positive Prompt)"
}
},
"7": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"38",
0
],
"text": " "
},
"_meta": {
"title": "CLIP Text Encode (Negative Prompt)"
}
},
"8": {
"class_type": "VAEDecode",
"inputs": {
"samples": [
"3",
0
],
"vae": [
"39",
0
]
},
"_meta": {
"title": "VAE Decode"
}
},
"37": {
"class_type": "UNETLoader",
"inputs": {
"unet_name": "qwen_image_fp8_e4m3fn.safetensors",
"weight_dtype": "default"
},
"_meta": {
"title": "Load Diffusion Model"
}
},
"38": {
"class_type": "CLIPLoader",
"inputs": {
"clip_name": "qwen_2.5_vl_7b_fp8_scaled.safetensors",
"device": "default",
"type": "qwen_image"
},
"_meta": {
"title": "Load CLIP"
}
},
"39": {
"class_type": "VAELoader",
"inputs": {
"vae_name": "qwen_image_vae.safetensors"
},
"_meta": {
"title": "Load VAE"
}
},
"60": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "ComfyUI",
"images": [
"8",
0
]
},
"_meta": {
"title": "Save Image"
}
},
"66": {
"class_type": "ModelSamplingAuraFlow",
"inputs": {
"model": [
"37",
0
],
"sampling": "flow",
"shift": 3.1000000000000005
},
"_meta": {
"title": "ModelSamplingAuraFlow"
}
},
"71": {
"class_type": "LoadImage",
"inputs": {
"image": "image_qwen_image_instantx_inpainting_controlnet_input_image.png"
},
"_meta": {
"title": "Load Image"
}
},
"76": {
"class_type": "VAEEncode",
"inputs": {
"pixels": [
"172",
0
],
"vae": [
"39",
0
]
},
"_meta": {
"title": "VAE Encode"
}
},
"84": {
"class_type": "ControlNetLoader",
"inputs": {
"control_net_name": "Qwen-Image-InstantX-ControlNet-Inpainting.safetensors"
},
"_meta": {
"title": "Load ControlNet Model"
}
},
"108": {
"class_type": "ControlNetInpaintingAliMamaApply",
"inputs": {
"control_net": [
"84",
0
],
"end_percent": 1,
"image": [
"172",
0
],
"mask": [
"121:253",
0
],
"negative": [
"7",
0
],
"positive": [
"6",
0
],
"start_percent": 0,
"strength": 1,
"vae": [
"39",
0
]
},
"_meta": {
"title": "Apply ControlNet Inpainting (AliMama)"
}
},
"122": {
"class_type": "SetLatentNoiseMask",
"inputs": {
"mask": [
"121:253",
0
],
"samples": [
"76",
0
]
},
"_meta": {
"title": "Set Latent Noise Mask"
}
},
"124": {
"class_type": "MaskPreview",
"inputs": {
"mask": [
"121:253",
0
]
},
"_meta": {
"title": "Preview Mask"
}
},
"126": {
"class_type": "ImageCompositeMasked",
"inputs": {
"destination": [
"172",
0
],
"mask": [
"121:253",
0
],
"resize_source": False,
"source": [
"8",
0
],
"x": 0,
"y": 0
},
"_meta": {
"title": "Image Composite Masked"
}
},
"163": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "ComfyUI",
"images": [
"126",
0
]
},
"_meta": {
"title": "Save Image"
}
},
"172": {
"class_type": "ImageScaleToMaxDimension",
"inputs": {
"image": [
"71",
0
],
"largest_size": 1536,
"upscale_method": "area"
},
"_meta": {
"title": "Scale Image to Max Dimension"
}
},
"121:199": {
"class_type": "GrowMask",
"inputs": {
"expand": 0,
"mask": [
"71",
1
],
"tapered_corners": True
},
"_meta": {
"title": "Grow Mask"
}
},
"121:251": {
"class_type": "MaskToImage",
"inputs": {
"mask": [
"121:199",
0
]
},
"_meta": {
"title": "Convert Mask to Image"
}
},
"121:252": {
"class_type": "ImageBlur",
"inputs": {
"blur_radius": 31,
"image": [
"121:251",
0
],
"sigma": 1
},
"_meta": {
"title": "Blur Image"
}
},
"121:253": {
"class_type": "ImageToMask",
"inputs": {
"channel": "red",
"image": [
"121:252",
0
]
},
"_meta": {
"title": "Convert Image to Mask"
}
}
})
# run() also passes api_key to the partner nodes in this workflow.
job = client.run(workflow, api_key=api_key)