comfy-api-helper
v1.1.1
Published
A Typescript helper library to make using the ComfyUI API easier
Downloads
8
Readme
Comfy API Helper
A lightweight Typescript helper library to make using the ComfyUI API easier.
Installation
You can install the package using your favorite package manager:
npm install comfy-api-helperbun add comfy-api-helperUsage
This library provides simple functions to generate images using your ComfyUI instance.
First, you need a workflow. You can create one in the ComfyUI interface and then export it using the File -> Save (API Format) button. This will give you a JSON file representing the workflow. You can then load this JSON file into your application and modify it as needed.
If using programatically, you may want to wrap the JSON in a Typescript function to replace common inputs like prompt and seed. See example/workflow.ts for an example.
Example
Here's a basic example of how to generate an image and save it to a file:
import fs from 'fs';
import { generateImage } from 'comfy-api-helper';
// 1. Load your workflow API JSON file.
// This is the file you get from ComfyUI's "Save (API Format)" feature.
const workflow = require('./workflow_api.json');
// 2. Set your ComfyUI server endpoint.
const COMFY_ENDPOINT = 'http://127.0.0.1:8188';
// 3. (Optional) Modify the workflow.
// For example, to change the prompt, you need to know the ID of the node you want to change.
// In this example, we're changing the text of a node with ID "6".
workflow['6'].inputs.text = 'a beautiful landscape painting';
async function main() {
try {
// 4. Generate the image.
const imageBuffer = await generateImage(workflow, COMFY_ENDPOINT);
// 5. Save the image to a file.
fs.writeFileSync('output.png', imageBuffer);
console.log('Image saved as output.png');
} catch (error) {
console.error('Error generating image:', error);
}
}
main();See example/example.ts for a full example.
API
generateImage(workflow, comfyEndpoint, outputFormat)
Generates an image and returns it as a Buffer.
workflow(any): A workflow object from ComfyUI's "Save (API Format)" feature. You can modify this object to change parameters like prompts, seeds, etc.comfyEndpoint(string): The URL of your ComfyUI instance (e.g.,'http://127.0.0.1:8188').outputFormat('png' | 'webp'): The desired output format. Defaults to'png'.
Returns: Promise<Buffer> - A promise that resolves to the image buffer.
generateImageToDataUri(workflow, comfyEndpoint, outputFormat)
A convenience function that calls generateImage and returns the image as a Data URI, ready to be used in <img> tags or other web contexts.
workflow(any): A workflow object.comfyEndpoint(string): The URL of your ComfyUI instance.outputFormat('png' | 'webp'): The desired output format. Defaults to'png'.
Returns: Promise<string> - A promise that resolves to the Data URI.
generateImageToBase64String(workflow, comfyEndpoint, outputFormat)
A convenience function that calls generateImage and returns the image as a raw base64-encoded string.
workflow(any): A workflow object.comfyEndpoint(string): The URL of your ComfyUI instance.outputFormat('png' | 'webp'): The desired output format. Defaults to'png'.
Returns: Promise<string> - A promise that resolves to the base64 string.
License
This project is licensed under the MIT License.
