# 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": "FL_PromptSelector",
"inputs": {
"append_text": "sitting on a table in a green room",
"index": [
"2",
0
],
"prepend_text": "a 1960s fine art crayon illustration of",
"prompts": "an apple\nan orange\na peach\na grape"
},
"_meta": {
"title": "FL Prompt Selector"
}
},
"2": {
"class_type": "PrimitiveInt",
"inputs": {
"value": 5
},
"_meta": {
"title": "Integer Counter"
}
},
"3": {
"class_type": "SaveImage",
"inputs": {
"filename_prefix": "ComfyUI",
"images": [
"98:8",
0
]
},
"_meta": {
"title": "Save Image"
}
},
"98:13": {
"class_type": "EmptySD3LatentImage",
"inputs": {
"batch_size": 1,
"height": 1024,
"width": 1024
},
"_meta": {
"title": "EmptySD3LatentImage"
}
},
"98:27": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"98:30",
0
],
"text": [
"1",
0
]
},
"_meta": {
"title": "CLIP Text Encode (Prompt)"
}
},
"98:28": {
"class_type": "UNETLoader",
"inputs": {
"unet_name": "z_image_turbo_bf16.safetensors",
"weight_dtype": "default"
},
"_meta": {
"title": "Load Diffusion Model"
}
},
"98:29": {
"class_type": "VAELoader",
"inputs": {
"vae_name": "ae.safetensors"
},
"_meta": {
"title": "Load VAE"
}
},
"98:30": {
"class_type": "CLIPLoader",
"inputs": {
"clip_name": "qwen_3_4b.safetensors",
"device": "default",
"type": "lumina2"
},
"_meta": {
"title": "Load CLIP"
}
},
"98:33": {
"class_type": "ConditioningZeroOut",
"inputs": {
"conditioning": [
"98:27",
0
]
},
"_meta": {
"title": "Conditioning Zero Out"
}
},
"98:8": {
"class_type": "VAEDecode",
"inputs": {
"samples": [
"98:97",
0
],
"vae": [
"98:29",
0
]
},
"_meta": {
"title": "VAE Decode"
}
},
"98:97": {
"class_type": "KSampler",
"inputs": {
"cfg": 1,
"denoise": 1,
"latent_image": [
"98:13",
0
],
"model": [
"98:28",
0
],
"negative": [
"98:33",
0
],
"positive": [
"98:27",
0
],
"sampler_name": "res_multistep",
"scheduler": "simple",
"seed": 792310888931084,
"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)