"""Using API nodes when running ComfyUI headless or with alternative frontend
You can execute a ComfyUI workflow that contains API nodes by including an API key in the prompt.
The API key should be added to the `extra_data` field of the payload.
Below we show an example of how to do this.
See more:
- API nodes overview: https://docs.comfy.org/tutorials/api-nodes/overview
- To generate an API key, login here: https://platform.comfy.org/login
"""
import json
from urllib import request
SERVER_URL = "http://127.0.0.1:8188"
# We have a prompt/job (workflow in "API format") that contains API nodes.
workflow_with_api_nodes = """{
"11": {
"inputs": {
"prompt": "A dreamy, surreal half-body portrait of a young woman meditating. She has a short, straight bob haircut dyed in pastel pink, with soft bangs covering her forehead. Her eyes are gently closed, and her hands are raised in a calm, open-palmed meditative pose, fingers slightly curved, as if levitating or in deep concentration. She wears a colorful dress made of patchwork-like pastel tiles, featuring clouds, stars, and rainbows. Around her float translucent, iridescent soap bubbles reflecting the rainbow hues. The background is a fantastical sky filled with cotton-candy clouds and vivid rainbow waves, giving the entire scene a magical, dreamlike atmosphere. Emphasis on youthful serenity, whimsical ambiance, and vibrant soft lighting.",
"prompt_upsampling": false,
"seed": 589991183902375,
"aspect_ratio": "1:1",
"raw": false,
"image_prompt_strength": 0.4000000000000001,
"image_prompt": [
"14",
0
]
},
"class_type": "FluxProUltraImageNode",
"_meta": {
"title": "Flux 1.1 [pro] Ultra Image"
}
},
"12": {
"inputs": {
"filename_prefix": "ComfyUI",
"images": [
"11",
0
]
},
"class_type": "SaveImage",
"_meta": {
"title": "Save Image"
}
},
"14": {
"inputs": {
"image": "example.png"
},
"class_type": "LoadImage",
"_meta": {
"title": "Load Image"
}
}
}"""
prompt = json.loads(workflow_with_api_nodes)
payload = {
"prompt": prompt,
# Add the `api_key_comfy_org` to the payload.
# You can first get the key from the associated user if handling multiple clients.
"extra_data": {
"api_key_comfy_org": "comfyui-87d01e28d*******************************************************" # replace with actual key
},
}
data = json.dumps(payload).encode("utf-8")
req = request.Request(f"{SERVER_URL}/prompt", data=data)
request.urlopen(req)