# 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({
"79": {
"class_type": "LoadImage",
"inputs": {
"image": "UPLOADED_image_FILENAME"
},
"_meta": {
"title": "Load Image"
}
},
"95": {
"class_type": "MaskPreview",
"inputs": {
"mask": [
"99:75",
0
]
},
"_meta": {
"title": "Preview Mask"
}
},
"106": {
"class_type": "PreviewImage",
"inputs": {
"images": [
"111",
0
]
},
"_meta": {
"title": "Preview Image"
}
},
"111": {
"class_type": "JoinImageWithAlpha",
"inputs": {
"alpha": [
"114",
0
],
"image": [
"79",
0
]
},
"_meta": {
"title": "Join Image with Alpha"
}
},
"114": {
"class_type": "InvertMask",
"inputs": {
"mask": [
"95",
0
]
},
"_meta": {
"title": "Invert Mask"
}
},
"99:75": {
"class_type": "SAM3_Detect",
"inputs": {
"bboxes": {
"height": 512,
"width": 512,
"x": 0,
"y": 0
},
"conditioning": [
"99:78",
0
],
"image": [
"79",
0
],
"individual_masks": False,
"model": [
"99:77",
0
],
"refine_iterations": 2,
"threshold": 0.5
},
"_meta": {
"title": "SAM3 Detect"
}
},
"99:77": {
"class_type": "CheckpointLoaderSimple",
"inputs": {
"ckpt_name": "sam3.1_multiplex_fp16.safetensors"
},
"_meta": {
"title": "Load Checkpoint"
}
},
"99:78": {
"class_type": "CLIPTextEncode",
"inputs": {
"clip": [
"99:77",
1
],
"text": "guitar"
},
"_meta": {
"title": "CLIP Text Encode (Prompt)"
}
}
})
workflow.set_input("79", "image", client.assets.from_url("https://comfy.org/catalog-media/25deb958-999c-47e3-a623-bc77ad279f88/5b44f36d-78bc-470b-b91d-7b55c563052c"))
# 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("111"):
output.to_file(output.name)