The ComfyUIApiClient class provides a high-level interface for interacting with the ComfyUI API.

Example

const client = new ComfyUIApiClient({
api_host: "YOUR_API_HOST",
clientId: "YOUR_CLIENT_ID",
});

const extensions = await client.getEmbeddings();
console.log(extensions);

Hierarchy (view full)

Constructors

Properties

WebSocket: {
    CLOSED: 3;
    CLOSING: 2;
    CONNECTING: 0;
    OPEN: 1;
    prototype: WebSocket;
    new (url, protocols?): WebSocket;
}

Type declaration

    • new (url, protocols?): WebSocket
    • Parameters

      • url: string | URL
      • Optional protocols: string | string[]

      Returns WebSocket

  • Readonly CLOSED: 3
  • Readonly CLOSING: 2
  • Readonly CONNECTING: 0
  • Readonly OPEN: 1
  • prototype: WebSocket
_cached_fn: CachedFn
_plugins: ClientPlugin[] = ...
_polling_interval: number = 1000
_polling_timer: any = null
api_base: string
api_host: string
clientId?: string
closed: boolean = false
events: EventEmitter<ComfyUIClientEvents & Record<string & {}, any>, any> = ...
fetch: {
    (input, init?): Promise<Response>;
    (input, init?): Promise<Response>;
}

Type declaration

    • (input, init?): Promise<Response>
    • Parameters

      • input: URL | RequestInfo
      • Optional init: RequestInit

      Returns Promise<Response>

    • (input, init?): Promise<Response>
    • Parameters

      • input: string | URL | Request
      • Optional init: RequestInit

      Returns Promise<Response>

socket?: null | WebSocket
socket_callbacks: Record<string, any> = {}
ssl: boolean
user: string
DEFAULT_API_BASE: string = ""
DEFAULT_API_HOST: string = "127.0.0.1:8188"
DEFAULT_USER: string = ""
IS_BROWSER: boolean = ...

Accessors

  • get registered(): (string & {} | (keyof ComfyUIClientEvents))[]
  • Returns (string & {} | (keyof ComfyUIClientEvents))[]

Methods

  • Asynchronously enqueues a prompt with optional workflow and random seed.

    Parameters

    • prompt: Record<string, unknown>

      The prompt to enqueue.

    • Optional options: {
          workflow?: Record<string, unknown>;
      }

      The options for enqueueing the prompt.

      • Optional workflow?: Record<string, unknown>

        The workflow for the prompt.

    Returns Promise<{
        node_errors: any;
        number: number;
        prompt_id: string;
    }>

    A promise that resolves with the enqueued prompt response.

    Throws

    If there is an error in the response.

  • Adds an event listener for the specified event type.

    Type Parameters

    • T extends keyof ComfyUIClientEvents

    Parameters

    • type: T

      The type of event to listen for.

    • callback: ((...args) => void)

      The callback function to be executed when the event is triggered.

        • (...args): void
        • Parameters

          • Rest ...args: ArgumentMap<ComfyUIClientEvents>[Extract<T, keyof ComfyUIClientEvents>]

          Returns void

    • Optional options: any

      (Optional) Additional options for the event listener.

    Returns (() => void)

    A function that removes the event listener when called.

      • (): void
      • Returns void

  • Type Parameters

    • K extends keyof WebSocketEventMap

    Parameters

    • socket: WebSocket
    • type: K
    • listener: ((this, ev) => any)
        • (this, ev): any
        • Parameters

          • this: WebSocket
          • ev: WebSocketEventMap[K]

          Returns any

    • Optional options: boolean | AddEventListenerOptions

    Returns (() => void)

      • (): void
      • Returns void

  • Returns the headers for the API request.

    Parameters

    • Optional options: RequestInit

      (Optional) Additional options for the request.

    Returns HeadersInit

    The headers for the API request.

  • Generates the URL for the API endpoint based on the provided route.

    Parameters

    • route: string

      The route for the API endpoint.

    Returns string

    The generated URL for the API endpoint.

  • Clears the specified list

    Parameters

    • type: "history" | "queue"

      The type of list to clear, queue or history

    Returns Promise<void>

  • Connects to the WebSocket server by creating a new socket connection.

    Parameters

    • options: {
          polling?: {
              enabled: boolean;
              interval?: number;
          };
          websocket?: {
              enabled: boolean;
          };
      } = {}

      The options for connecting to the server.

      • Optional polling?: {
            enabled: boolean;
            interval?: number;
        }

        The options for polling.

        • enabled: boolean
        • Optional interval?: number
      • Optional websocket?: {
            enabled: boolean;
        }

        The options for the WebSocket connection.

        • enabled: boolean

    Returns ComfyUIApiClient

    • The instance of the class.
  • Creates a new user

    Parameters

    • username: string

    Returns Promise<Response>

    The fetch response

  • Deletes an item from the specified list

    Parameters

    • type: "history" | "queue"

      The type of item to delete, queue or history

    • id: any

      The id of the item to delete

    Returns Promise<void>

  • Enqueues a prompt and waits for the corresponding prompt websocket.

    Type Parameters

    • T

    Parameters

    • prompt: Record<string, unknown>

      The prompt to enqueue.

    • Optional options: EnqueueOptions<T>

      The options for enqueueing the prompt.

    Returns Promise<WorkflowOutput<T>>

    A promise that resolves with the prompt result.

  • Parameters

    • prompt: Record<string, unknown>
    • Optional options: EnqueueOptions

    Returns Promise<WorkflowOutput>

  • Asynchronously enqueues a prompt and waits for the corresponding prompt websocket.

    This function does not use WebSocket, but uses polling to get the result So if your workflow contains custom ws events, this function will not be able to get these events

    Type Parameters

    • T

    Parameters

    • prompt: Record<string, unknown>

      The prompt to enqueue.

    • Optional options: EnqueueOptions<T>

      The options for enqueueing the prompt.

    Returns Promise<WorkflowOutput<T>>

    A promise that resolves with the prompt result.

  • Parameters

    • prompt: Record<string, unknown>
    • Optional options: EnqueueOptions

    Returns Promise<WorkflowOutput>

  • Fetches API data based on the provided route and options.

    NOTE: CORS policy: Request header field comfy-user is not allowed by Access-Control-Allow-Headers in preflight response. Please use empty string in browser.

    Parameters

    • route: string

      The route for the API request.

    • Optional options: RequestInit

      (Optional) Additional options for the request.

    Returns Promise<Response>

    A promise that resolves to the API response.

  • Free up memory by unloading models and freeing memory

    Parameters

    • Optional params: {
          free_memory?: boolean;
          unload_models?: boolean;
      }
      • Optional free_memory?: boolean
      • Optional unload_models?: boolean

    Returns Promise<void>

  • Retrieves the list of model names from the node definitions.

    Returns Promise<string[]>

    A promise that resolves to an array of strings representing the model names.

  • Gets a list of embedding names

    Returns Promise<string[]>

    An array of script urls to import

  • Gets a list of extension urls

    Returns Promise<string[]>

    An array of script urls to import

  • Gets the prompt execution history

    Parameters

    • max_items: number = 200

    Returns Promise<{
        History: {
            outputs: Record<string, unknown>;
            prompt: [number, string, any, any, any];
            status: {
                completed: boolean;
                messages: any[];
                status_str: string;
            };
        }[];
    }>

    Prompt history including node outputs

  • Retrieves the list of hypernetwork names from the node definitions.

    Returns Promise<string[]>

    A promise that resolves to an array of strings representing the hypernetwork names.

  • Loads a list of items (queue or history)

    Parameters

    • type: "history"

      The type of items to load, queue or history

    Returns Promise<{
        History: {
            outputs: Record<string, unknown>;
            prompt: [number, string, any, any, any];
            status: {
                completed: boolean;
                messages: any[];
                status_str: string;
            };
        }[];
    }>

    The items of the specified type grouped by their status

  • Parameters

    • type: "queue"

    Returns Promise<{
        Pending: Record<string, unknown>[];
        Running: Record<string, unknown>[];
    }>

  • Retrieves the list of LoRAs from the node definitions.

    Returns Promise<string[]>

    A promise that resolves to an array of strings representing the LoRAs.

  • Loads node object definitions for the graph

    Returns Promise<ObjectInfo>

    The object info for the graph

  • Retrieves the outputs of a prompt with the given ID from the history.

    Parameters

    • prompt_id: string

      The ID of the prompt to retrieve the outputs for.

    Returns Promise<Record<string, unknown>>

    A promise that resolves to the outputs of the prompt.

    Throws

    If the prompt with the given ID is not found in the history or if it failed with a non-"success" status.

  • Retrieves the result of a prompt with the given ID, resolved using the provided resolver.

    Type Parameters

    • T

    Parameters

    • prompt_id: string

      The ID of the prompt to retrieve the result for.

    • resolver: WorkflowOutputResolver<T>

      The resolver to use when resolving the prompt result.

    Returns Promise<WorkflowOutput<T>>

    A promise that resolves to the result of the prompt.

  • Parameters

    • prompt_id: string

    Returns Promise<WorkflowOutput>

  • Retrieves the status of a prompt based on the provided prompt ID.

    Parameters

    • prompt_id: string

      The ID of the prompt to check status for.

    Returns Promise<{
        done: boolean;
        pending: boolean;
        running: boolean;
    }>

    Object containing the running, pending, and done status of the prompt.

  • Gets the current state of the queue

    Returns Promise<{
        Pending: Record<string, unknown>[];
        Running: Record<string, unknown>[];
    }>

    The currently running and queued items

  • Retrieves the list of model names from the node definitions.

    Returns Promise<string[]>

    A promise that resolves to an array of strings representing the model names.

  • Retrieves the list of samplers from the node definitions.

    Returns Promise<string[]>

    A promise that resolves to an array of strings representing the sampler names.

  • Retrieves the list of schedulers from the node definitions.

    Returns Promise<string[]>

    A promise that resolves to an array of strings representing the scheduler names.

  • Gets a setting for the current user

    Parameters

    • id: string

      The id of the setting to fetch

    Returns Promise<unknown>

    The setting value

  • Gets all setting values for the current user

    Returns Promise<Record<string, unknown>>

    A dictionary of id -> value

  • Gets system & device stats

    Returns Promise<SystemStatsRoot>

    System stats such as python version, OS, per device info

  • Retrieves the list of model names from the node definitions for the UpscaleModelLoader node.

    Returns Promise<string[]>

    A promise that resolves to an array of strings representing the model names.

  • Gets user configuration data and where data should be stored

    Returns Promise<any>

  • Gets a user data file for the current user

    Parameters

    • file: string

      The name of the userdata file to load

    • Optional options: RequestInit

    Returns Promise<Response>

    The fetch response object

  • Retrieves the list of VAE names from the node definitions.

    Returns Promise<string[]>

    A promise that resolves to an array of strings representing the VAE names.

  • Adds an event listener for the specified event type.

    Type Parameters

    • T extends keyof ComfyUIClientEvents

    Parameters

    • type: T

      The type of event to listen for.

    • callback: ((...args) => void)

      The callback function to be executed when the event is triggered.

        • (...args): void
        • Parameters

          • Rest ...args: ArgumentMap<ComfyUIClientEvents>[Extract<T, keyof ComfyUIClientEvents>]

          Returns void

    • Optional options: any

      (Optional) Additional options for the event listener.

    Returns (() => void)

    A function that removes the event listener when called.

      • (): void
      • Returns void

  • Listens for progress updates for a specific task.

    Parameters

    • fn: undefined | ((p) => void)

      The progress callback function.

    • task_id: string

      The ID of the task to listen for progress updates.

    Returns (() => void)

    A function that can be used to remove the progress listener.

      • (): void
      • Returns void

  • Adds an event listener for the specified event type.

    Type Parameters

    • T extends keyof ComfyUIClientEvents

    Parameters

    • type: T

      The type of event to listen for.

    • callback: ((...args) => void)

      The callback function to be executed when the event is triggered.

        • (...args): void
        • Parameters

          • Rest ...args: ArgumentMap<ComfyUIClientEvents>[Extract<T, keyof ComfyUIClientEvents>]

          Returns void

    • Optional options: any

      (Optional) Additional options for the event listener.

    Returns (() => void)

    A function that removes the event listener when called.

      • (): void
      • Returns void

  • Sends a POST request to the API

    Parameters

    • type: string

      The endpoint to post to

    • body: any

      Optional POST data

    Returns Promise<void>

  • Parameters

    • queue_index: number

      The index at which to queue the prompt, passing -1 will insert the prompt at the front of the queue

    • options: {
          prompt: any;
          workflow: any;
      }
      • prompt: any

        The prompt to queue

      • workflow: any

        This png info to be added to resulting image

    Returns Promise<QueuePrompt>

    The response from the server

  • Asynchronously runs a prompt with the provided options.

    This function does not use WebSocket, but uses polling to get the result So if your workflow contains custom ws events, this function will not be able to get these events

    Parameters

    • prompt: Record<string, unknown>

      The prompt to run.

    • Optional options: {
          polling_ms?: number;
          workflow?: Record<string, unknown>;
      }

      The options for running the prompt.

      • Optional polling_ms?: number

        The number of milliseconds to polling query prompt result.

      • Optional workflow?: Record<string, unknown>

        The workflow for the prompt, It will be added to the png info of the generated image.

    Returns Promise<WorkflowOutput<WorkflowOutput>>

    A promise that resolves with the prompt result.

    Deprecated

    Use enqueue_polling instead

  • Stores a setting for the current user

    Parameters

    • id: string

      The id of the setting to update

    • value: unknown

      The value of the setting

    Returns Promise<Response>

  • Stores a dictionary of settings for the current user

    Parameters

    • settings: Record<string, unknown>

      Dictionary of setting id -> value to save

    Returns Promise<Response>

  • Stores a user data file for the current user

    Parameters

    • file: string

      The name of the userdata file to save

    • data: any

      The data to save to the file

    • Optional options: RequestInit & {
          stringify?: boolean;
          throwOnError?: boolean;
      }

    Returns Promise<void>

  • Generates a URL for viewing a specific file with the given filename, subfolder, and type.

    Parameters

    • filename: string

      The name of the file to view.

    • subfolder: string

      The subfolder where the file is located.

    • type: string

      The type of the file.

    Returns string

    The URL for viewing the file.

  • Asynchronously waits for the prompt with the provided ID to be done.

    Parameters

    • prompt_id: string

      The ID of the prompt to wait for.

    • Optional polling_ms: number = 1000

      The number of milliseconds to wait between checks.

    Returns Promise<void>

  • Asynchronously waits for the prompt with the provided ID to be done, using a WebSocket connection to receive updates.

    Type Parameters

    • T

    Parameters

    • prompt_id: string

      The ID of the prompt to wait for.

    • resolver: WorkflowOutputResolver<T>

      A function to resolve the output of the prompt.

    Returns Promise<WorkflowOutput<T>>

    A promise that resolves with the output of the prompt.