# 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({
"2": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_reference_2_FILENAME"
},
"_meta": {
"title": "Load Image"
}
},
"3": {
"class_type": "Kling4ReferenceToVideoNode",
"inputs": {
"model": "kling-4.0",
"model.aspect_ratio": "adaptive",
"model.duration": 5,
"model.prompt": "Three shots, hard cuts on the beat. Fashion-editorial / MV title fragment inside the brutalist hall from @Image2; the woman from @Image1 keeps her orange-red bob, freckles, burgundy lips, camel leather trench, black turtleneck, wide black trousers and boots exactly as they are. Do not change the hall: dark concrete walls, stone bench, arched window, hard golden god-ray and dust in the beam.\n\n0-1.5s: extreme close-up of her face filling the frame, god-ray rim light on her hair and cheek; she looks dead into camera with a small confident almost-smile, then a tiny decisive chin tilt; fast snap zoom-in on her eyes then hard hold.\n1.5-3.2s: hard cut - she is now standing mid-hall inside the golden beam beside the stone bench (new position); medium-wide; she turns on her heel, trench coat flares, and walks two sharp steps toward camera; whip-pan left with her motion then stops.\n3.2-5s: hard cut - centered in the thickest part of the god-ray under the arched window (new position); medium three-quarter; both arms relaxed at her sides, no pockets; camera makes a smooth 120° orbit right around her as she holds a still fashion stare into lens, dust sparkling in the beam, ending locked on a clean beauty frame.\n\nEach shot has its own camera move; never one continuous slow pan. Sound: coat leather rustle, boot heel on stone, soft hall reverb; no dialogue, no subtitles.",
"model.reference_images.image_1": [
"5",
0
],
"model.reference_images.image_2": [
"2",
0
],
"model.resolution": "720p",
"model.seed": 42
},
"_meta": {
"title": "Kling 4.0 Reference to Video"
}
},
"4": {
"class_type": "SaveVideo",
"inputs": {
"codec": "auto",
"filename_prefix": "video/kling4_r2v",
"format": "mp4",
"format.codec": "auto",
"video": [
"3",
0
]
},
"_meta": {
"title": "Save Video"
}
},
"5": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_reference_1_FILENAME"
},
"_meta": {
"title": "Load Image"
}
}
})
workflow.set_input("5", "image", client.assets.from_url("https://comfy.org/catalog-media/ea9dd0e9-285f-49b4-b091-789cbfcfcf7d/a5af77aa-e731-4248-b5db-ce4c8ba242a5"))
workflow.set_input("2", "image", client.assets.from_url("https://comfy.org/catalog-media/49803ae0-6e0c-4dd4-a1e8-720cb6b364d7/6cf86f42-350e-440c-9c0a-2c5b3a8837c8"))
# 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("4"):
output.to_file(output.name)