# 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": "LoadVideo",
"inputs": {
"file": "UPLOADED_video_FILENAME"
},
"_meta": {
"title": "Load Video"
}
},
"2": {
"class_type": "GrokVideoExtendNode",
"inputs": {
"model": "grok-imagine-video",
"model.duration": 6,
"prompt": "The horse keeps galloping straight past the camera, which whips around in a fast pan to follow it from behind as it races toward a shallow meltwater river. It plunges into the water at full speed, sending a huge wall of icy spray into the air that catches the first gold rays of the rising sun breaking through the clouds. The camera rises into a slow crane shot, revealing the horse splashing out onto the far bank and running on toward the snow-dusted volcanic ridges, small against the vast tundra. Sound: thundering hooves, splashing water, howling wind, and a low swelling orchestral score with deep drums that builds as the sun breaks through.",
"seed": 1577960711,
"video": [
"1",
0
]
},
"_meta": {
"title": "Grok Video Extend"
}
},
"3": {
"class_type": "SaveVideo",
"inputs": {
"filename_prefix": "video/Grok",
"format": "auto",
"format.codec": "auto",
"video": [
"2",
0
]
},
"_meta": {
"title": "Save Video"
}
}
})
workflow.set_input("1", "file", client.assets.from_url("https://comfy.org/catalog-media/86bcfacc-ccbf-42d8-9fc3-dfa8cb78dc81/3cacc844-7687-4f3b-801f-5147b1b1b767"))
# 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("3"):
output.to_file(output.name)