# 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({
"4": {
"class_type": "SaveVideo",
"inputs": {
"filename_prefix": "video/Grok",
"format": "auto",
"format.codec": "auto",
"video": [
"19",
0
]
},
"_meta": {
"title": "Save Video"
}
},
"5": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_reference_2_FILENAME"
},
"_meta": {
"title": "Load Image"
}
},
"6": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_reference_1_FILENAME"
},
"_meta": {
"title": "Load Image"
}
},
"19": {
"class_type": "GrokVideoReferenceNode",
"inputs": {
"model": "grok-imagine-video",
"model.aspect_ratio": "16:9",
"model.duration": 6,
"model.reference_images.reference_0": [
"6",
0
],
"model.reference_images.reference_1": [
"5",
0
],
"model.resolution": "720p",
"prompt": "A cinematic studio piece on a seamless slate-grey cyclorama with cool, low, misty light. The blackened cast-iron storm lantern and the oxblood leather flask with its brass cap from the first reference sit apart on the floor, frost on the metal. The man from the second reference, in the long glossy midnight-blue vinyl coat, charcoal tunic, grey wool trousers and burgundy boots, walks in from the left, kneels, lifts the lantern and strikes it alight; a warm amber flame blooms behind the seeded glass and throws moving shadows across the floor. He tucks the flask into his coat, stands and holds the lantern up, looking into the distance as fog rolls in around his boots. The camera starts low and wide, then arcs smoothly around him and pushes in to a medium shot as the light catches the vinyl coat. Same people and objects as the references, no text.",
"seed": 0
},
"_meta": {
"title": "Grok Reference-to-Video"
}
}
})
workflow.set_input("6", "image", client.assets.from_url("https://comfy.org/catalog-media/f93e0d63-dad0-4dc3-a231-157f660c3a95/0dc166eb-b85f-4d09-97c6-9eea87bd173a"))
workflow.set_input("5", "image", client.assets.from_url("https://comfy.org/catalog-media/7d3ab090-01db-4f92-b00c-467412fe2e1c/37660f07-a749-42f8-919f-f5a45633231e"))
# 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)