Skip to content

kalebbroo/SwarmUI.ApiClient

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

16 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SwarmUI API Client Library

Professional C# client library for SwarmUI API

🚧 v0.5.0-beta 🚧

SwarmUI.ApiClient is a strongly-typed C# wrapper around the SwarmUI API, providing first-class support for text-to-image generation, model management, presets, user data, backends, and admin operations. The core implementation is in place and covered by unit tests; the API surface may still evolve before a 1.0.0 stable release.

Project Structure

SwarmUI.ApiClient/
β”œβ”€β”€ SwarmClient.cs                 # Main client class
β”œβ”€β”€ ISwarmClient.cs                # Main client interface
β”œβ”€β”€ SwarmClientOptions.cs          # Configuration options
β”‚
β”œβ”€β”€ Sessions/                      # Session management
β”‚   β”œβ”€β”€ ISessionManager.cs
β”‚   └── SessionManager.cs
β”‚
β”œβ”€β”€ Http/                          # HTTP communication
β”‚   β”œβ”€β”€ ISwarmHttpClient.cs
β”‚   └── SwarmHttpClient.cs
β”‚
β”œβ”€β”€ WebSockets/                    # WebSocket streaming
β”‚   β”œβ”€β”€ ISwarmWebSocketClient.cs
β”‚   └── SwarmWebSocketClient.cs
β”‚
β”œβ”€β”€ Endpoints/                     # API endpoint groups
β”‚   β”œβ”€β”€ Generation/                # Text-to-image generation
β”‚   β”œβ”€β”€ Models/                    # Model management
β”‚   β”œβ”€β”€ Backends/                  # Backend servers
β”‚   β”œβ”€β”€ Presets/                   # Parameter presets
β”‚   β”œβ”€β”€ User/                      # User settings
β”‚   └── Admin/                     # Admin operations
β”‚
β”œβ”€β”€ Models/                        # Data models
β”‚   β”œβ”€β”€ Requests/                  # Request models
β”‚   β”œβ”€β”€ Responses/                 # Response models
β”‚   └── Common/                    # Shared models
β”‚
β”œβ”€β”€ Exceptions/                    # Custom exceptions
β”‚   β”œβ”€β”€ SwarmException.cs
β”‚   β”œβ”€β”€ SwarmSessionException.cs
β”‚   β”œβ”€β”€ SwarmAuthenticationException.cs
β”‚   └── SwarmWebSocketException.cs
β”‚
└── Extensions/                    # DI extensions
    └── ServiceCollectionExtensions.cs

Changelog

This README gives a high level snapshot. For detailed release notes, see:

Highlights for the current beta:

  • First beta of SwarmUI.ApiClient: typed wrapper around SwarmUI HTTP + WebSocket APIs.
  • Core infrastructure implemented: SwarmClientOptions, SessionManager, SwarmHttpClient, SwarmWebSocketClient, and the SwarmClient facade.
  • Endpoint coverage for generation, models, backends, presets, user, and admin operations.
  • Unit tests in the SwarmTests project cover HTTP behavior, sessions, streaming generation, model management, presets, and client wiring.

Upcoming Features

Planned improvements for future releases include:

  • Retry and resilience policies using Polly (configurable via SwarmClientOptions).
  • Integration tests against a real SwarmUI instance.
  • Optional examples project / samples that mirror the docs.
  • CI/CD pipeline for automated build, test, pack, and publish to NuGet.
  • Potential multi targeting support for additional .NET versions.

Usage

Standalone Usage

SwarmClientOptions options = new SwarmClientOptions
{
    BaseUrl = "https://hartsy.ai",
    Authorization = "your-api-key"
};
using SwarmClient client = new SwarmClient(options);
GenerationRequest request = new GenerationRequest
{
    Prompt = "A beautiful sunset over mountains",
    Model = "flux-dev",
    Width = 1024,
    Height = 768
};
await foreach (GenerationUpdate update in client.Generation.StreamGenerationAsync(request))
{
    if (update.Type == "progress")
        Console.WriteLine($"Progress: {update.Progress.CurrentPercent}%");
    else if (update.Type == "image")
        SaveImage(update.Image.Image);
}

Dependency Injection Usage

// Program.cs
builder.Services.AddSwarmClient(options =>
{
    options.BaseUrl = "https://hartsy.ai";
    options.Authorization = builder.Configuration["SwarmAuth"];
});

// YourService.cs
public class ImageService(ISwarmClient swarm)
{
    public async Task GenerateAsync()
    {
        // Use swarm...
    }
}

Contributing

This library follows strict coding guidelines:

  • No var keyword - always use explicit types
  • No private fields - use public Impl struct pattern
  • All public members must have XML documentation
  • Follow .NET naming conventions
  • Use ConfigureAwait(false) in library code

See detailed guidelines in Docs/CodingGuidelines.md.

Real-World Usage Examples

The HartsyWeb application uses SwarmUI.ApiClient in production for both internal and external APIs. For example, an ASP.NET Core controller can stream generation updates to the client using Server-Sent Events (SSE):

[ApiController]
[Route("api/swarm")]
public class GenerateController(ISwarmClient swarmClient) : ControllerBase
{
    [HttpPost("generate")]
    public async Task Generate([FromBody] GenerationRequest request, CancellationToken cancellationToken)
    {
        Response.Headers.Append("Content-Type", "text/event-stream");

        await foreach (GenerationUpdate update in swarmClient.Generation.StreamGenerationAsync(request, cancellationToken))
        {
            // Write SSE event data and flush the response stream here.
        }
    }
}

See the HartsyWeb repository for full controller implementations and additional end-to-end examples.

License

MIT License. See the LICENSE file in this folder.

Links

About

A C# wrapper for the SwarmUI API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages