# 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({
"1": {
"class_type": "ComfySwitchNode",
"inputs": {
"on_false": [
"3",
0
],
"on_true": [
"2",
0
],
"switch": False
},
"_meta": {
"title": "If/Else Switch"
}
},
"2": {
"class_type": "PrimitiveString",
"inputs": {
"value": "true"
},
"_meta": {
"title": "Text"
}
},
"3": {
"class_type": "PrimitiveString",
"inputs": {
"value": "false"
},
"_meta": {
"title": "Text"
}
},
"4": {
"class_type": "PreviewAny",
"inputs": {
"source": [
"1",
0
]
},
"_meta": {
"title": "Preview as Text"
}
},
"5": {
"class_type": "ComfySwitchNode",
"inputs": {
"on_false": [
"7",
0
],
"on_true": [
"6",
0
],
"switch": True
},
"_meta": {
"title": "If/Else Switch"
}
},
"6": {
"class_type": "PrimitiveString",
"inputs": {
"value": "true"
},
"_meta": {
"title": "Text"
}
},
"7": {
"class_type": "PrimitiveString",
"inputs": {
"value": "false"
},
"_meta": {
"title": "Text"
}
},
"8": {
"class_type": "PreviewAny",
"inputs": {
"source": [
"5",
0
]
},
"_meta": {
"title": "Preview as Text"
}
},
"60": {
"class_type": "UNETLoader",
"inputs": {
"unet_name": "z_image_turbo_bf16.safetensors",
"weight_dtype": "default"
},
"_meta": {
"title": "Load Diffusion Model"
}
},
"61": {
"class_type": "ComfySwitchNode",
"inputs": {
"on_false": [
"60",
0
],
"on_true": [
"62",
0
],
"switch": [
"66",
0
]
},
"_meta": {
"title": "Model Switch (MODEL)"
}
},
"62": {
"class_type": "LoraLoaderModelOnly",
"inputs": {
"lora_name": "zimage-90_s_anime_melancholy.safetensors",
"model": [
"60",
0
],
"strength_model": 1
},
"_meta": {
"title": "Load LoRA"
}
},
"63": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "ComfyUI",
"images": [
"64:55",
0
]
},
"_meta": {
"title": "Save Image"
}
},
"65": {
"class_type": "ComfySwitchNode",
"inputs": {
"on_false": [
"67",
0
],
"on_true": [
"68",
0
],
"switch": [
"66",
0
]
},
"_meta": {
"title": "Prompt Switch (CLIP)"
}
},
"66": {
"class_type": "PrimitiveBoolean",
"inputs": {
"value": True
},
"_meta": {
"title": "LoRA Switch"
}
},
"67": {
"class_type": "PrimitiveStringMultiline",
"inputs": {
"value": "a 27 year old man wearing a baseball uniform, smiling at the camera and holding a bat"
},
"_meta": {
"title": "Prompt - No LoRA"
}
},
"68": {
"class_type": "PrimitiveStringMultiline",
"inputs": {
"value": "90s_melancholy, retro, a 27 year old man wearing a baseball uniform, smiling at the camera and holding a bat"
},
"_meta": {
"title": "Prompt - With LoRA Trigger"
}
},
"64:52": {
"class_type": "CLIPLoader",
"inputs": {
"clip_name": "qwen_3_4b.safetensors",
"device": "default",
"type": "lumina2"
},
"_meta": {
"title": "Load CLIP"
}
},
"64:53": {
"class_type": "VAELoader",
"inputs": {
"vae_name": "ae.safetensors"
},
"_meta": {
"title": "Load VAE"
}
},
"64:54": {
"class_type": "ConditioningZeroOut",
"inputs": {
"conditioning": [
"64:56",
0
]
},
"_meta": {
"title": "Conditioning Zero Out"
}
},
"64:55": {
"class_type": "VAEDecode",
"inputs": {
"samples": [
"64:58",
0
],
"vae": [
"64:53",
0
]
},
"_meta": {
"title": "VAE Decode"
}
},
"64:56": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"64:52",
0
],
"text": [
"65",
0
]
},
"_meta": {
"title": "CLIP Text Encode (Prompt)"
}
},
"64:57": {
"class_type": "EmptySD3LatentImage",
"inputs": {
"batch_size": 1,
"height": 1024,
"width": 1024
},
"_meta": {
"title": "EmptySD3LatentImage"
}
},
"64:58": {
"class_type": "KSampler",
"inputs": {
"cfg": 1,
"denoise": 1,
"latent_image": [
"64:57",
0
],
"model": [
"61",
0
],
"negative": [
"64:54",
0
],
"positive": [
"64:56",
0
],
"sampler_name": "res_multistep",
"scheduler": "simple",
"seed": 12673005598788,
"steps": 4
},
"_meta": {
"title": "KSampler"
}
}
})
# run() also passes api_key to the partner nodes in this workflow.
job = client.run(workflow, api_key=api_key)