# 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-upscaled",
"images": [
"87:65",
0
]
},
"_meta": {
"title": "Save Image"
}
},
"77": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_image_FILENAME"
},
"_meta": {
"title": "Load Image"
}
},
"86": {
"class_type": "ImageCompare",
"inputs": {
"compare_view": {
"__value__": [
"",
""
]
},
"image_a": [
"77",
0
],
"image_b": [
"87:65",
0
]
},
"_meta": {
"title": "Compare Images"
}
},
"87:62": {
"class_type": "CLIPLoader",
"inputs": {
"clip_name": "qwen_3_4b.safetensors",
"device": "default",
"type": "lumina2"
},
"_meta": {
"title": "Load CLIP"
}
},
"87:63": {
"class_type": "VAELoader",
"inputs": {
"vae_name": "ae.safetensors"
},
"_meta": {
"title": "Load VAE"
}
},
"87:65": {
"class_type": "VAEDecode",
"inputs": {
"samples": [
"87:69",
0
],
"vae": [
"87:63",
0
]
},
"_meta": {
"title": "VAE Decode"
}
},
"87:66": {
"class_type": "UNETLoader",
"inputs": {
"unet_name": "z_image_turbo_bf16.safetensors",
"weight_dtype": "default"
},
"_meta": {
"title": "Load Diffusion Model"
}
},
"87:67": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"87:62",
0
],
"text": "masterpiece, 8k"
},
"_meta": {
"title": "CLIP Text Encode (Positive Prompt)"
}
},
"87:69": {
"class_type": "KSampler",
"inputs": {
"cfg": 1,
"denoise": 0.33,
"latent_image": [
"87:80",
0
],
"model": [
"87:70",
0
],
"negative": [
"87:71",
0
],
"positive": [
"87:67",
0
],
"sampler_name": "dpmpp_2m_sde",
"scheduler": "beta",
"seed": 824287194145573,
"steps": 5
},
"_meta": {
"title": "KSampler"
}
},
"87:70": {
"class_type": "ModelSamplingAuraFlow",
"inputs": {
"model": [
"87:66",
0
],
"sampling": "flow",
"shift": 3
},
"_meta": {
"title": "ModelSamplingAuraFlow"
}
},
"87:71": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"87:62",
0
],
"text": ""
},
"_meta": {
"title": "CLIP Text Encode (Negative Prompt)"
}
},
"87:76": {
"class_type": "UpscaleModelLoader",
"inputs": {
"model_name": "RealESRGAN_x4plus.safetensors"
},
"_meta": {
"title": "Load Upscale Model"
}
},
"87:78": {
"class_type": "ImageScaleToTotalPixels",
"inputs": {
"image": [
"77",
0
],
"megapixels": 1,
"resolution_steps": 1,
"upscale_method": "lanczos"
},
"_meta": {
"title": "Scale Image to Total Pixels"
}
},
"87:79": {
"class_type": "ImageUpscaleWithModel",
"inputs": {
"image": [
"87:78",
0
],
"upscale_model": [
"87:76",
0
]
},
"_meta": {
"title": "Upscale Image (using Model)"
}
},
"87:80": {
"class_type": "VAEEncode",
"inputs": {
"pixels": [
"87:81",
0
],
"vae": [
"87:63",
0
]
},
"_meta": {
"title": "VAE Encode"
}
},
"87:81": {
"class_type": "ImageScaleBy",
"inputs": {
"image": [
"87:79",
0
],
"scale_by": 0.5,
"upscale_method": "lanczos"
},
"_meta": {
"title": "Upscale Image By"
}
}
})
workflow.set_input("77", "image", client.assets.from_url("https://comfy.org/catalog-media/b6246849-5e0a-4dd7-bd25-e2c86e5f8a06/b21718bf-6c2f-48b3-95bb-ac9dd2444bc2"))
# 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)