Skip to content

ComfyApi ‐ Comfy client for interact with ComfyUI Web instance

Saintno edited this page Aug 13, 2024 · 1 revision

ComfyApi Class

The ComfyApi class is a TypeScript implementation of a client for interacting with a Comfy API. It provides methods for various API operations and includes WebSocket functionality for real-time updates.

Constructor

constructor(host: string, clientId: string = ComfyApi.generateId(), opts?: { credentials?: BasicCredentials })

Creates a new instance of the ComfyApi class.

Static Methods

generateId()

Generates a random client ID.

Instance Methods

API Interaction

  • queuePrompt(number: number, workflow: object): Queues a prompt for processing.
  • getQueue(): Retrieves the current state of the queue.
  • getHistories(maxItems: number = 200): Retrieves the prompt execution history.
  • getHistory(promptId: string): Retrieves a specific history entry.
  • getSystemStats(): Retrieves system and device stats.
  • getExtensions(): Retrieves a list of extension URLs.
  • getEmbeddings(): Retrieves a list of embedding names.
  • getCheckpoints(): Retrieves the checkpoints from the server.
  • getLoras(): Retrieves the Loras from the node definitions.
  • getSamplerInfo(): Retrieves sampler and scheduler information.
  • getNodeDefs(nodeName?: string): Retrieves node object definitions.
  • getUserConfig(): Retrieves user configuration data.
  • createUser(username: string): Creates a new user.
  • getSettings(): Retrieves all setting values for the current user.
  • getSetting(id: string): Retrieves a specific setting.
  • storeSettings(settings: Record<string, unknown>): Stores multiple settings.
  • storeSetting(id: string, value: unknown): Stores a specific setting.
  • uploadImage(file: Buffer | Blob, fileName: string, config?: object): Uploads an image file.
  • getPathImage(imageInfo: ImageInfo): Returns the path to an image.
  • getImage(imageInfo: ImageInfo): Retrieves the blob of an image.

User Data Management

  • getUserData(file: string): Retrieves a user data file.
  • storeUserData(file: string, data: unknown, options?: object): Stores a user data file.
  • deleteUserData(file: string): Deletes a user data file.
  • moveUserData(source: string, dest: string, options?: object): Moves a user data file.
  • listUserData(dir: string, recurse?: boolean, split?: boolean): Lists user data files.

WebSocket and Connection Management

  • init(): Initializes the WebSocket connection.
  • ping(): Sends a ping request to check server reachability.
  • interrupt(): Interrupts the execution of the running prompt.
  • pollStatus(timeout = 1000): Polls the status for environments that don't support WebSockets.

Event Handling

  • on(type: string, callback: Function, options?: object): Adds an event listener.
  • off(type: string, callback: Function, options?: object): Removes an event listener.

Private Methods

  • createSocket(isReconnect: boolean = false): Creates and connects a WebSocket.
  • pollQueue(): Polls the status queue for updates in non-WebSocket environments.
  • fetchApi(route: string, options?: FetchOptions): Performs API requests with proper headers.
  • testCredentials(): Tests the provided credentials.

Events

The class extends EventTarget and dispatches custom events such as:

  • auth_success
  • auth_error
  • reconnected
  • disconnected
  • reconnecting
  • b_preview
  • all
  • status

These events can be listened to using the on method or standard addEventListener.


This class provides a comprehensive interface for interacting with the Comfy API, handling authentication, WebSocket communication, and various API operations. It's designed to be used in TypeScript projects and includes type definitions for improved development experience.