# 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({
"9": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "z-image-turbo",
"images": [
"70:43",
0
]
},
"_meta": {
"title": "Save Image"
}
},
"56": {
"class_type": "PreviewImage",
"inputs": {
"images": [
"57",
0
]
},
"_meta": {
"title": "Preview Image"
}
},
"57": {
"class_type": "Canny",
"inputs": {
"high_threshold": 0.32,
"image": [
"58",
0
],
"low_threshold": 0.1
},
"_meta": {
"title": "Detect Edges (Canny)"
}
},
"58": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_image_FILENAME"
},
"_meta": {
"title": "Load Image"
}
},
"70:39": {
"class_type": "CLIPLoader",
"inputs": {
"clip_name": "qwen_3_4b.safetensors",
"device": "default",
"type": "lumina2"
},
"_meta": {
"title": "Load CLIP"
}
},
"70:40": {
"class_type": "VAELoader",
"inputs": {
"vae_name": "ae.safetensors"
},
"_meta": {
"title": "Load VAE"
}
},
"70:41": {
"class_type": "EmptySD3LatentImage",
"inputs": {
"batch_size": 1,
"height": [
"70:69",
1
],
"width": [
"70:69",
0
]
},
"_meta": {
"title": "EmptySD3LatentImage"
}
},
"70:42": {
"class_type": "ConditioningZeroOut",
"inputs": {
"conditioning": [
"70:45",
0
]
},
"_meta": {
"title": "Conditioning Zero Out"
}
},
"70:43": {
"class_type": "VAEDecode",
"inputs": {
"samples": [
"70:44",
0
],
"vae": [
"70:40",
0
]
},
"_meta": {
"title": "VAE Decode"
}
},
"70:44": {
"class_type": "KSampler",
"inputs": {
"cfg": 1,
"denoise": 1,
"latent_image": [
"70:41",
0
],
"model": [
"70:47",
0
],
"negative": [
"70:42",
0
],
"positive": [
"70:45",
0
],
"sampler_name": "res_multistep",
"scheduler": "simple",
"seed": 582911328872997,
"steps": 9
},
"_meta": {
"title": "KSampler"
}
},
"70:45": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"70:39",
0
],
"text": "Realistic photo of a moonlit greenhouse library at night, built inside the same room layout. The ceiling slats become dark walnut beams strung with tiny warm bulbs, and the tall window wall turns into old greenhouse glass fogged with condensation, tropical leaves pressing against it from outside. The low sofa is upholstered in deep emerald velvet, the stone coffee table becomes a slab of black marble stacked with open books, and the lounge chair is worn burgundy leather. The shelving grid on the right holds trailing ferns and small brass lanterns. Cool blue moonlight falls through the glass, warm lamplight pools on the terrazzo floor, quiet and cosy, light film grain."
},
"_meta": {
"title": "CLIP Text Encode (Prompt)"
}
},
"70:46": {
"class_type": "UNETLoader",
"inputs": {
"unet_name": "z_image_turbo_bf16.safetensors",
"weight_dtype": "default"
},
"_meta": {
"title": "Load Diffusion Model"
}
},
"70:47": {
"class_type": "ModelSamplingAuraFlow",
"inputs": {
"model": [
"70:60",
0
],
"sampling": "flow",
"shift": 3
},
"_meta": {
"title": "ModelSamplingAuraFlow"
}
},
"70:60": {
"class_type": "QwenImageDiffsynthControlnet",
"inputs": {
"end_percent": 1,
"image": [
"57",
0
],
"model": [
"70:46",
0
],
"model_patch": [
"70:64",
0
],
"start_percent": 0,
"strength": 1,
"vae": [
"70:40",
0
]
},
"_meta": {
"title": "Apply Qwen Image DiffSynth ControlNet"
}
},
"70:64": {
"class_type": "ModelPatchLoader",
"inputs": {
"name": "Z-Image-Turbo-Fun-Controlnet-Union.safetensors"
},
"_meta": {
"title": "Load Model Patch"
}
},
"70:69": {
"class_type": "GetImageSize",
"inputs": {
"image": [
"57",
0
]
},
"_meta": {
"title": "Get Image Size"
}
}
})
workflow.set_input("58", "image", client.assets.from_url("https://comfy.org/catalog-media/d582dae6-4b4a-4ef9-9a66-ce170401a12d/6ac33887-7efb-472b-b4bf-e29fa62ab9d3"))
# run() also passes api_key to the partner nodes in this workflow.
job = client.run(workflow, api_key=api_key)
for output in job.get_outputs("9"):
output.to_file(output.name)