> ## Documentation Index
> Fetch the complete documentation index at: https://docs.beam.cloud/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> Install the Beam CLI, sign in, and connect coding agents to the Beam MCP server with `curl -fsSL https://www.beam.cloud/install | sh`. To also create a VM, run `curl -fsSL https://www.beam.cloud/install | sh -s -- --vm`.
> VMs are persistent Linux machines without GPUs. Defaults: 4 CPUs, 8 GiB of RAM, a 50 GiB disk, Ubuntu 24.04, and a browser desktop. A running VM accrues usage until you stop, pause, or remove it, or until an idle timeout puts it to sleep.
> Stopping a VM keeps its disk and discards RAM and processes. Pausing keeps both, and resuming restores them. To pause after 5 idle minutes and wake on access, run `beam vm update NAME --ttl 300 --idle-action pause --auto-resume`.
> Core VM commands: `beam vm new NAME`, `beam vm ssh NAME`, `beam vm sync NAME ./dir`, `beam vm exec NAME -- CMD`, `beam vm expose NAME PORT --protected`, `beam vm fork NAME NEW_NAME`, `beam vm pause NAME`, `beam vm stop NAME`, `beam vm rm NAME`.
> For GPUs, use a sandbox or a deployed endpoint. Sandboxes are disposable environments that boot in seconds and fork from snapshots.
> Current rates are at https://www.beam.cloud/pricing.

# Serverless ComfyUI

This guide shows how to deploy a ComfyUI server on Beam using [`Pod`](https://docs.beam.cloud/v2/pod/web-service). We'll set up a server to generate images with [Flux1 Schnell](https://huggingface.co/Comfy-Org/flux1-schnell), but you can easily adapt it to use other models like Stable Diffusion v1.5.

> [card] [View the Code](https://github.com/beam-cloud/examples/blob/main/image_generation/comfy_ui/app.py)
>
> See the code for this example on Github.

![](https://mintcdn.com/slai-beam/cYxAFgZcnH6nQdWb/img/examples/comfyui.png?fit=max\&auto=format\&n=cYxAFgZcnH6nQdWb\&q=85\&s=c9153d8867a96398b3a57cc1a41f0531)

## Setting Up the ComfyUI Server

1. **Create the Deployment Script**

   Create a file named `app.py` with the following code. This script sets up a Beam `Pod` with ComfyUI, installs dependencies, downloads the Flux1 Schnell model, and launches the server.

   ```python theme={null}
   from beam import Image, Pod

   ORG_NAME = "Comfy-Org"
   REPO_NAME = "flux1-schnell"
   WEIGHTS_FILE = "flux1-schnell-fp8.safetensors"
   COMMIT = "f2808ab17fe9ff81dcf89ed0301cf644c281be0a"

   image = (
       Image()
       .add_commands(["apt update && apt install git -y"])
       .add_python_packages(
           [
               "fastapi[standard]==0.115.4",
               "comfy-cli==1.22.0",
               "huggingface_hub==2.2.0",
           ]
       )
       .add_commands(
           [
               "comfy --skip-prompt install --nvidia --version 0.39.0",
               f"hf download {ORG_NAME}/{REPO_NAME} {WEIGHTS_FILE} --revision {COMMIT} --local-dir /root/comfy/ComfyUI/models/checkpoints",
           ]
       )
   )

   comfyui_server = Pod(
       image=image,
       ports=[8000],
       cpu=12,
       memory="32Gi",
       gpu="RTX4090",
       entrypoint=["sh", "-c", "comfy launch -- --listen 0.0.0.0 --port 8000"],
   )

   res = comfyui_server.create()
   print("ComfyUI hosted at:", res.url)
   ```

   `hf download` saves the weights straight into ComfyUI's `checkpoints` folder, and `--revision` pins them to the commit in `COMMIT`. If your workflows need custom nodes, add `comfy node install <node-name>` to the same list, after `comfy install`.

2. **Start ComfyUI**

   ```bash theme={null}
   python app.py
   ```

   This builds the image, starts the ComfyUI server on Beam, and prints its URL, which ends in `-8000.app.beam.cloud`.

   &#x20;

   > [!WARNING]
   >
   > The first run takes about 10 minutes, because Beam builds and uploads a
   > 23 GB image that includes ComfyUI and the model weights. Later runs reuse
   > the cached image, and ComfyUI responds about 30 seconds after the container
   > starts.

3. **Accessing the Server**

   * Open the URL from your terminal in a browser to access the ComfyUI interface.
   * Use the web UI to load workflows or generate images. The first prompt takes a few minutes while ComfyUI loads the model into GPU memory. After that, Flux1 Schnell generates an image in about 2 seconds on an RTX 4090.
   * An idle Pod shuts down after 10 minutes. Set `keep_warm_seconds` on the `Pod` to change this, or stop it sooner as described in [Terminating a Pod](https://docs.beam.cloud/v2/pod/web-service.md#terminating-a-pod).

## Using Different Models

You can swap the Flux1 Schnell model for another, such as Stable Diffusion v1.5, by updating the model variables in `app.py`. Here’s how:

1. **Update the Model Variables**

   Define the organization, repository, weights file, and commit ID for your desired model. For example, to use Stable Diffusion v1.5:

   ```python theme={null}
   ORG_NAME = "Comfy-Org"
   REPO_NAME = "stable-diffusion-v1-5-archive"
   WEIGHTS_FILE = "v1-5-pruned-emaonly-fp16.safetensors"
   COMMIT = "21e044065c0b2d82dafd35397a553847c70c0445"
   ```

2. **Apply to the Image Commands**

   The rest of the script uses these variables, so no further changes are needed to the `image` section:

   ```python theme={null}
   image = (
       Image()
       .add_commands(["apt update && apt install git -y"])
       .add_python_packages(
           [
               "fastapi[standard]==0.115.4",
               "comfy-cli==1.22.0",
               "huggingface_hub==2.2.0",
           ]
       )
       .add_commands(
           [
               "comfy --skip-prompt install --nvidia --version 0.39.0",
               f"hf download {ORG_NAME}/{REPO_NAME} {WEIGHTS_FILE} --revision {COMMIT} --local-dir /root/comfy/ComfyUI/models/checkpoints",
           ]
       )
   )
   ```

3. **Find Model Details**

   To use any other model:

   * Visit [Comfy-Org Hugging Face](https://huggingface.co/Comfy-Org) and find your desired model.
   * Update `ORG_NAME`, `REPO_NAME`, `WEIGHTS_FILE`, and `COMMIT` with values from the model’s repository. Check the "Files and versions" tab for the weights file and commit hash.

## Running Workflows as APIs

You can also expose ComfyUI workflows as APIs using Beam’s ASGI support. This allows you to programmatically generate images by sending requests with prompts. Below is an example of how to set this up:

1. **Create the API Script**

   Create a file named `api.py`:

   ```python theme={null}
   from beam import Image, asgi, Output

   image = (
       Image()
       .add_commands(["apt update && apt install git -y"])
       .add_python_packages(
           [
               "fastapi[standard]==0.115.4",
               "comfy-cli==1.22.0",
               "huggingface_hub==2.2.0",
           ]
       )
       .add_commands(
           [
               "comfy --skip-prompt install --nvidia --version 0.39.0",
               "hf download Comfy-Org/flux1-schnell flux1-schnell-fp8.safetensors --revision f2808ab17fe9ff81dcf89ed0301cf644c281be0a --local-dir /root/comfy/ComfyUI/models/checkpoints",
           ]
       )
   )

   def init_models():
       import subprocess

       cmd = "comfy launch --background"
       subprocess.run(cmd, shell=True, check=True)

   @asgi(
       name="comfy",
       image=image,
       on_start=init_models,
       cpu=8,
       memory="32Gi",
       gpu="RTX4090",
       timeout=600,
   )
   def handler():
       from fastapi import FastAPI, HTTPException
       import subprocess
       import json
       from pathlib import Path
       import uuid
       from typing import Dict

       app = FastAPI()

       # This is where you specify the path to your workflow file.
       # Make sure "workflow_api.json" exists in the same directory as this script.
       WORKFLOW_FILE = Path(__file__).parent / "workflow_api.json"
       OUTPUT_DIR = Path("/root/comfy/ComfyUI/output")

       @app.post("/generate")
       async def generate(item: Dict):
           if not WORKFLOW_FILE.exists():
               raise HTTPException(status_code=500, detail="Workflow file not found.")

           workflow_data = json.loads(WORKFLOW_FILE.read_text())
           workflow_data["6"]["inputs"]["text"] = item["prompt"]
           request_id = uuid.uuid4().hex
           workflow_data["9"]["inputs"]["filename_prefix"] = request_id

           new_workflow_file = Path(f"{request_id}.json")
           new_workflow_file.write_text(json.dumps(workflow_data, indent=4))

           # Run inference
           cmd = f"comfy run --workflow {new_workflow_file} --wait --timeout 1200 --verbose"
           subprocess.run(cmd, shell=True, check=True)

           image_files = list(OUTPUT_DIR.glob("*"))

           # Find the latest image
           latest_image = max(
               (f for f in image_files if f.suffix.lower() in {".png", ".jpg", ".jpeg"}),
               key=lambda f: f.stat().st_mtime,
               default=None
           )

           if not latest_image:
               raise HTTPException(status_code=404, detail="No output image found.")

           output_file = Output(path=latest_image)
           output_file.save()
           public_url = output_file.public_url(expires=3600)
           print(public_url)
           return {"output_url": public_url}

       return app
   ```

2. **Prepare a Workflow File**

   * Create a `workflow_api.json` file in the same directory as `api.py`. This file should contain your ComfyUI workflow in API format, which you can export from the ComfyUI web interface.
   * The handler sets the prompt on node `"6"` and the output filename prefix on node `"9"`. If those node IDs are different in your exported file, update them in the handler.

   &#x20;

   ![](https://mintcdn.com/slai-beam/cYxAFgZcnH6nQdWb/img/examples/comfyui-menu-workflow.png?fit=max\&auto=format\&n=cYxAFgZcnH6nQdWb\&q=85\&s=7ca9e35fc3472d0c71ac947f62ef1e91)

   * You can also store your `workflow_api.json` file in your Volume and use it like `WORKFLOW_FILE = Path("/your_volume/workflow_api.json")`

3. **Deploy the API**

   ```bash theme={null}
   beam deploy api.py:handler
   ```

   The image is the same as the one in the Pod example, so if you ran that first, the deploy reuses the cached image. Otherwise, the first deploy takes about 10 minutes to build it.

4. **Use the API**

   Send a POST request to the `/generate` endpoint with a JSON payload containing a `prompt`. Use the URL that `beam deploy` printed:

   ```bash theme={null}
    curl -X POST https://comfy-[APP-ID].app.beam.cloud/generate \
    -H 'Content-Type: application/json' \
    -H 'Authorization: Bearer [YOUR_AUTH_TOKEN]' \
    -d '{"prompt": "A cat image"}'
   ```

   The first request after the app scales up from zero takes about 90 seconds while ComfyUI starts and loads the model. After that, each request takes a few seconds.

   The response will include a public URL to the generated image:

   ```json theme={null}
   {
     "output_url": "https://app.beam.cloud/output/id/9a003889-8345-4969-bdf8-2808eebc1c4b"
   }
   ```

   ![](https://mintcdn.com/slai-beam/cYxAFgZcnH6nQdWb/img/examples/comfy-output.png?fit=max\&auto=format\&n=cYxAFgZcnH6nQdWb\&q=85\&s=97e37bb45c8894bbff43f174127dee8a)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.