ComfySharpSDK is a lightweight C# SDK for interacting with ComfyUI, a popular graphical interface for generating images using Stable Diffusion models. The library allows you to programmatically run workflows, track progress, and receive generated images directly in your .The NET application.
Did you like it? Put a star on it.
Make sure that your ComfyUI instance is running and accessible via HTTP (by default http://127.0.0.1:8188 ).
If you are using an API key (for example, when running with the --listen --enable-cors-header --extra-model-paths-config flag, etc.), specify it when creating the client.
The main client for interacting with ComfyUI.
Basic constructor
Constructor with custom HttpClient (for DI or setting timeouts)
baseUrlis the ComfyUI server address (without the ending/).apiKeyis an optional Bearer token for authorization (if ComfyUI is protected).maxAttempts— the maximum number of attempts to wait for a result (120 by default, i.e. ~2 minutes with a delay of 1 sec).
| Method | Description |
|---|---|
RunWorkflowAsync(Workflow) |
Sends workflow to ComfyUI and returns `prompt_id'. |
GetHistoryAsync(string promptId) |
Gets the execution history by `prompt_id'. |
RunWorkflowAndGetImagesAsync(...) |
Full cycle: start → wait → load images. |
Represents the ComfyUI workflow in JSON format.
// From a JSON string
var workflow = Workflow.Load(jsonString);
// From the file
var workflow = Workflow.LoadFromFile("path/to/workflow.json");workflow.SetNodeValue("6", "text", "a dog in space");Important:
node_idis a string identifier of the node in the workflow JSON structure (for example,"6").The names of inputs (
input_name) depend on the node type (for example,"text","seed","steps", etc.).
You can freely change the node parameters by specifying the key and value for them
ComfySharpSDK/ComfySharpSDKExemple/FluentWorkflow.cs
Lines 9 to 22 in abadac3
This is a more elegant way, but it also requires specifying the key and value types
ComfySharpSDK/ComfySharpSDKExemple/DslWorkflow.cs
Lines 9 to 20 in abadac3
The most advanced way to edit a Workflow, the disadvantages are the need to download information about all nodes from ComfyUI, after which the system automatically collects objects from them
ComfySharpSDK/ComfySharpSDKExemple/DynamicDslWorkflow.cs
Lines 10 to 25 in abadac3
GetNodes()— returns the dictionaryNode ID → name(if specified in_meta.title).toJSON()— serializes workflow back to readable JSON.
A class representing the generated image.
| Property | Description |
|---|---|
NodeId |
ID of the node that generated the image |
Filename |
File name (for example, "ComfyUI_00001_.png") |
Subfolder |
Subfolder (if used) |
Type |
Output type (usually "output") |
Data |
Bytes of the image (byte[]) |
If your ComfyUI is running with an API key (for example, via `--api-key your_secret_key'), pass it when creating the client.:
var client = new ComfyClient(
baseUrl: "http://your-server.com:8188",
apiKey: "your_secret_key"
);The client will automatically add the header:
Authorization: Bearer your_secret_key
The RunWorkflowAndGetImagesAsync method supports the CancellationToken, which allows you to cancel the wait correctly.:
var cts = new CancellationTokenSource(TimeSpan.FromMinutes(5));
var images = await client.RunWorkflowAndGetImagesAsync(workflow, cancellationToken: cts.Token);If the result does not appear in maxAttempts (by default, 120 attempts × 1 sec = 2 min), a TimeoutException will be thrown.
ComfyUI expects workflow in JSON format, where each node has a unique string ID and inputs (inputs). Example of a node:
{
"6": {
"_meta": { "title": "CLIP Text Encode" },
"class_type": "CLIPTextEncode",
"inputs": {
"text": "a cat",
"clip": ["4", 1]
}
}
}
You can change the "text" via:
workflow.SetNodeValue("6", "text", "a dog in space");- Make sure that ComfyUI is running with the
--listenflag if you are not using localhost. - For CORS requests,
--enable-cors-headermay be required. - Use
Workflow.GetNodes()for debugging — it will show all available nodes and their names. - Images are stored temporarily on the ComfyUI server. Do not delay the download for a long time.
MIT License — use it freely in personal and commercial projects.
Note: ComfySharpSDK is not an official ComfyUI product. This is a community-driven SDK.
