You can use 3-rd party ComfyUI API developed by ComfyICU Team. Focus on building next-gen AI experiences rather than on maintaining own GPU infrastructure. We are not affiliated with ComfyICU Team and provide the following info as it is.
You can find examples here: https://github.com/comfyicu/examples
Step 1: Create an API Key
Start by generating an API key from your account settings page. Once you have the key, set it as an environment variable:
export COMFYICU_API_KEY=XXX
Step 2: Install node-fetch
for API Requests
Install the node-fetch
package to make API requests:
npm install node-fetch@2 --save
Step 3: Create a Workflow
Next, create a workflow on the ComfyICU website. Run some experiments to ensure everything is functioning properly. When satisfied, open a specific “run” and click the “View API code” button to copy the workflow_id
and prompt, which you’ll use in the next steps:
const workflow_id = "XXX";
const prompt = {
// ComfyUI API JSON
};
Step 4: Run the Workflow Using the API
Use the following function to run your workflow via the API:
const fetch = require("node-fetch");
async function runWorkflow(body) {
const url = `https://comfy.icu/api/v1/workflows/${body.workflow_id}/runs`;
const resp = await fetch(url, {
headers: {
accept: "application/json",
"content-type": "application/json",
authorization: `Bearer ${process.env.COMFYICU_API_KEY}`,
},
body: JSON.stringify(body),
method: "POST",
});
return await resp.json();
}
const run = await runWorkflow({ workflow_id, prompt });
console.log(run);
Step 5: Check the Run Status
You can check the status of a run either by using a webhook for real-time updates or by periodically polling the API.
Webhooks
To receive real-time updates, specify a webhook URL when creating the run:
const webhook = "https://your-public-web-server.com/api/comfyicu-webhook";
const run = await runWorkflow({ workflow_id, prompt, files, webhook });
console.log(run);
Here’s an example of a simple server using ExpressJS to handle webhook requests:
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
const port = 5000;
app.use(bodyParser.json());
app.post("/api/comfyicu-webhook", (req, res) => {
const data = req.body;
console.log(`Received webhook update: ${JSON.stringify(data, null, 2)}`);
res.status(200).json({ status: "success" });
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
Polling
If you prefer not to use webhooks, you can poll the API to check the run’s status:
async function getRunStatus(workflow_id, run_id) {
const url = `https://comfy.icu/api/v1/workflows/${workflow_id}/runs/${run_id}`;
const resp = await fetch(url, {
headers: {
accept: "application/json",
"content-type": "application/json",
authorization: `Bearer ${process.env.COMFYICU_API_KEY}`,
},
});
return await resp.json();
}
const status = await getRunStatus(workflow_id, run.id);
console.log(status);
You can set up polling with a delay between checks:
async function pollRunStatus(workflow_id, run_id, maxAttempts = 30, delay = 10000) {
for (let attempt = 0; attempt < maxAttempts; attempt++) {
try {
const status = await getRunStatus(workflow_id, run_id);
console.log(`Attempt ${attempt + 1}: Run status is ${status.status}`);
if (status.status === "COMPLETED" || status.status === "ERROR") {
return status;
}
await new Promise((resolve) => setTimeout(resolve, delay));
} catch (error) {
console.error(`Error during polling: ${error.message}`);
throw error;
}
}
throw new Error("Max polling attempts reached");
}
Step 6: Add Input Images and Videos
You can add input files to your workflow using the files
field. For example, to load an image into a “Load Image” node:
const files = {
"/input/image1.jpg": "http://public-url-for-assets.com/image1.jpg",
};
const prompt = {
// ComfyUI API JSON
45: {
_meta: {
title: "Load Image",
},
inputs: {
image: "image1.jpg", // input filename
upload: "image",
},
class_type: "LoadImage",
},
};
const run = await runWorkflow({ workflow_id, prompt, files });
console.log(run);
Step 7: Add Custom Models, LoRAs, and Embeddings
Similar to input files, you can add custom models via the files
field:
const files = {
"/models/loras/thickline_fp16.safetensors": "https://civitai.com/api/download/models/16368?type=Model&format=SafeTensor&size=full&fp=fp16",
};
const prompt = {
43: {
inputs: {
clip: ["4", 1],
model: ["4", 0],
lora_name: "thickline_fp16.safetensors", // specify the lora filename
strength_clip: 1,
strength_model: 1,
},
class_type: "LoraLoader",
},
};
const run = await runWorkflow({ workflow_id, prompt, files });
console.log(run);
Step 8: Change the GPU Accelerator
You can specify the GPU accelerator by adding the accelerator
option:
const accelerator = "A100_40GB";
const run = await runWorkflow({ workflow_id, prompt, files, accelerator });
console.log(run);
Now you’re all set to run custom ComfyUI workflows using the ComfyICU API. Happy coding!