# 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": "Flux2",
"images": [
"68:8",
0
]
},
"_meta": {
"title": "Save Image"
}
},
"45": {
"class_type": "ImageScaleToTotalPixels",
"inputs": {
"image": [
"46",
0
],
"megapixels": 1,
"resolution_steps": 1,
"upscale_method": "lanczos"
},
"_meta": {
"title": "Scale Image to Total Pixels"
}
},
"46": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_image_FILENAME"
},
"_meta": {
"title": "Load Image"
}
},
"68:10": {
"class_type": "VAELoader",
"inputs": {
"vae_name": "flux2-vae.safetensors"
},
"_meta": {
"title": "Load VAE"
}
},
"68:12": {
"class_type": "UNETLoader",
"inputs": {
"unet_name": "flux2_dev_fp8mixed.safetensors",
"weight_dtype": "default"
},
"_meta": {
"title": "Load Diffusion Model"
}
},
"68:13": {
"class_type": "SamplerCustomAdvanced",
"inputs": {
"guider": [
"68:22",
0
],
"latent_image": [
"68:47",
0
],
"noise": [
"68:25",
0
],
"sampler": [
"68:16",
0
],
"sigmas": [
"68:48",
0
]
},
"_meta": {
"title": "SamplerCustomAdvanced"
}
},
"68:16": {
"class_type": "KSamplerSelect",
"inputs": {
"sampler_name": "euler"
},
"_meta": {
"title": "KSamplerSelect"
}
},
"68:22": {
"class_type": "BasicGuider",
"inputs": {
"conditioning": [
"68:43",
0
],
"model": [
"68:92",
0
]
},
"_meta": {
"title": "Basic Guider"
}
},
"68:25": {
"class_type": "RandomNoise",
"inputs": {
"noise_seed": 649422536169327
},
"_meta": {
"title": "RandomNoise"
}
},
"68:26": {
"class_type": "FluxGuidance",
"inputs": {
"conditioning": [
"68:6",
0
],
"guidance": 4
},
"_meta": {
"title": "FluxGuidance"
}
},
"68:38": {
"class_type": "CLIPLoader",
"inputs": {
"clip_name": "mistral_3_small_flux2_bf16.safetensors",
"device": "default",
"type": "flux2"
},
"_meta": {
"title": "Load CLIP"
}
},
"68:43": {
"class_type": "ReferenceLatent",
"inputs": {
"conditioning": [
"68:26",
0
],
"latent": [
"68:44",
0
]
},
"_meta": {
"title": "Set Reference Latent"
}
},
"68:44": {
"class_type": "VAEEncode",
"inputs": {
"pixels": [
"45",
0
],
"vae": [
"68:10",
0
]
},
"_meta": {
"title": "VAE Encode"
}
},
"68:47": {
"class_type": "EmptyFlux2LatentImage",
"inputs": {
"batch_size": 1,
"height": [
"68:72",
1
],
"width": [
"68:72",
0
]
},
"_meta": {
"title": "Empty Flux 2 Latent"
}
},
"68:48": {
"class_type": "Flux2Scheduler",
"inputs": {
"height": [
"68:72",
1
],
"steps": [
"68:93",
0
],
"width": [
"68:72",
0
]
},
"_meta": {
"title": "Flux2Scheduler"
}
},
"68:6": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"68:38",
0
],
"text": "Turn this studio car portrait into a snowy winter night on a quiet city street. Keep the woman exactly as she is: her face, slicked-back dark hair, black cat-eye sunglasses, expression and the arm resting on the window sill. Keep the olive-gold vintage coupe, its chrome door handle, side mirror and the white leather handbag unchanged. Dress her in a camel wool coat with a thick cream knit scarf over her black top. Replace the plain studio backdrop with a softly blurred street of warm glowing shop windows and gently falling snowflakes. Add a thin layer of fresh snow on the car roof and window sill, cool blue ambient light and warm amber reflections across the metallic paint. Photorealistic, same camera angle and composition."
},
"_meta": {
"title": "CLIP Text Encode (Positive Prompt)"
}
},
"68:72": {
"class_type": "GetImageSize",
"inputs": {
"image": [
"45",
0
]
},
"_meta": {
"title": "Get Image Size"
}
},
"68:8": {
"class_type": "VAEDecode",
"inputs": {
"samples": [
"68:13",
0
],
"vae": [
"68:10",
0
]
},
"_meta": {
"title": "VAE Decode"
}
},
"68:89": {
"class_type": "LoraLoaderModelOnly",
"inputs": {
"lora_name": "Flux_2-Turbo-LoRA_comfyui.safetensors",
"model": [
"68:12",
0
],
"strength_model": 1
},
"_meta": {
"title": "Load LoRA"
}
},
"68:90": {
"class_type": "PrimitiveInt",
"inputs": {
"value": 8
},
"_meta": {
"title": "Steps"
}
},
"68:91": {
"class_type": "PrimitiveInt",
"inputs": {
"value": 20
},
"_meta": {
"title": "Steps"
}
},
"68:92": {
"class_type": "ComfySwitchNode",
"inputs": {
"on_false": [
"68:12",
0
],
"on_true": [
"68:89",
0
],
"switch": [
"68:94",
0
]
},
"_meta": {
"title": "Switch(model)"
}
},
"68:93": {
"class_type": "ComfySwitchNode",
"inputs": {
"on_false": [
"68:91",
0
],
"on_true": [
"68:90",
0
],
"switch": [
"68:94",
0
]
},
"_meta": {
"title": "Switch(steps)"
}
},
"68:94": {
"class_type": "PrimitiveBoolean",
"inputs": {
"value": False
},
"_meta": {
"title": "Enable 8 steps lora"
}
}
})
workflow.set_input("46", "image", client.assets.from_url("https://comfy.org/catalog-media/7fdd890a-cbb8-4d98-b2cb-35f01890994d/f593ebe0-8ecc-4d03-bb09-673f2b92ef5f"))
# 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)