ComfyUI: batch run from command line with API

While AUTOMATIC1111 can generate images based on prompt variations, I haven’t found the same possibility in ComfyUI. However, you can achieve the same result thanks to ComfyUI API and curl.

When you click “queue prompt” in ComfyUI, it actually sends a POST request with the whole workflow as JSON data to .

To get the workflow as JSON, go to the UI and click on the settings icon, then enable Dev mode Options and click close. You will now see a new button Save (API format).

When you have a workflow you are happy with, save it in API format.

You need to enclose the whole prompt in a JSON field “prompt” like so:

Remember to add a closing bracket at the end of the file and save it.

You can now send this with curl as follows and ComfyUI will render your prompt as if you sent it through the web UI:

curl -X POST --data @workflow.json

What if you want to send a different prompt?

Well, you need to find the node with your prompt and replace the prompt with a placeholder, for example _POSITIVEPROMPT_. Like so:

Then, you can write prompt(s) in a text file and use the usual tools to sequentially run your prompts:

cat prompts | while read prompt<br>do<br>    sed -e "s/_POSITIVEPROMPT_/$prompt/" workflow.json &gt; /dev/shm/data.json<br>    curl -X POST --data @/dev/shm/data.json<br>done

If you run ComfyUI on Windows and you don’t have a native curl, you can run the Powershell command Invoke-RestMethod. You can also make ComfyUI listen on another (or all) IP addresses using the --listen option and run curl from WSL2 or another computer.

python --directml --listen

You can of course change other fields in the workflow by repeating the same operation:

  • replace the value with a placeholder
  • add a replace operation in sed
  • run curl
sed -e "s/toto/tata/" -e "s/titi/tutu/" -e etc
This entry was posted in artificial intelligence, Computer and tagged , , , , . Bookmark the permalink.

2 Responses to ComfyUI: batch run from command line with API

  1. Hi, Thanks for putting this together. I have a question. I’m trying to access the API via a C# Unity script. The way I was successfully doing this with the Auto1111 API was to use the following code:
    var jsonBytes = Encoding.UTF8.GetBytes(json); //json contains the {prompt}
    var www = new UnityWebRequest(“”, “POST”);
    www.uploadHandler = new UploadHandlerRaw(jsonBytes);
    www.downloadHandler = new DownloadHandlerBuffer();
    www.SetRequestHeader(“Content-Type”, “application/json”);
    www.SetRequestHeader(“Accept”, ” text/plain”);
    yield return www.SendWebRequest();

    Would I use the end point for ComfyUI the same way?
    var www = new UnityWebRequest(“”, “POST”);

    Appreciate any help. Thanks in advance

  2. blogger says:

    Yes, as long as you send the full workflow, that should work.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.