Introduction
ComfyUI is a popular no-code interface for building complex stable diffusion workflows. Its ease of use, modular setup, and intuitive flowchart interface have helped the community build an extensive collection of workflows. Several websites offer shared workflows to help you get started: While experimenting with ComfyUI workflows is straightforward, guidance on production-scale deployment is limited. This tutorial shows how to use Cerebrium to deploy your pipelines as autoscaling API endpoints with pay-as-you-go compute costs. You can find the full example code here.Creating your Comfy UI workflow locally
Create your workflow either locally or using a rented GPU from Lambda Labs. First, ensure ComfyUI is installed in your local environment. This tutorial uses Stable Diffusion XL and ControlNet to create custom QR codes. If you already have a workflow, skip to “Export ComfyUI Workflow”.- Let us first create our Cerebrium project with:
cerebrium init 1-comfyui
- Inside your project, lets copy the ComfyUI GitHub project:
git clone https://github.com/comfyanonymous/ComfyUI
- Download the following models and install them in the appropriate folders within the ComfyUI folder:
- SDXL base in models/checkpoints.
- ControlNet in models/ControlNet.
- To run ComfyUI locally, run the command: python main.py —force-fp16 on MacOS. Make sure you run this inside the ComfyUI folder you just cloned.
- A server should be loaded locally at http://127.0.0.1:8188/
Export ComfyUI Workflow
In our example GitHub repository, we have a workflow.json file. You can click the “Load” button on the right to load in our workflow. You then should see the workflow populated

ComfyUI Application
The main code inmain.py
uses the exported ComfyUI API workflow to create an endpoint. The code:
- Initializes the ComfyUI server
- Loads the workflow API template
- Processes inputs (prompts, images) through the
run
function - Generates base64-encoded image outputs
run
function only during initialization, while the run
function handles all subsequent requests.
We need to alter our workflow_api.json file to have placeholders so that we can replace user values on inference. You can alter the file as follows.
- Replace line 4, the seed input, with: “{{seed}}”
- Replace line 45, the input text of node 6 with: “{{positive_prompt}}”
- Replace line 58, the input text of node 7 with: “{{negative_prompt}}”
- Replace line 108, the image of node 11 with: “{{controlnet_image}}“
FastAPI app
In addition to themain.py
file below (Which runs our fastAPI server and initializes ComfyUI on application start), We created a file that contains utility functions to make it easier to work with ComfyUI. You can find the code for that helper here. Create a file named helpers.py
and copy the code into there.
Deploy ComfyUI Application
Before deploying our application, we must first define the deployment configuration, which installs all our dependencies, contains our hardware and scaling configurations, as well as any scripts which must be executed before running our app. The following should be added to ourcerebrium.toml
file:
cerebrium deploy
uploads both the ComfyUI directory and ~10GB of model weights. For slower connections, use our helper file to download model weights directly to the appropriate folders. Create a file called model.json
with the following contents:

cerebrium deploy
Once your ComfyUI application has been deployed successfully, you should be able to make a request to the endpoint using the following JSON payload:
- The base64 encoded image of the outline of your original ControlNet image. This is what it uses as a input in your flow.
- A base64 encoded image of the final result.
