.NET
The official .NET library for the Superslice API.
Source, issues and releases: superslice-co/superslice-dotnet.
Install
The package is not published to NuGet yet. Until it is, install from GitHub:
git clone https://github.com/superslice-co/superslice-dotnet.git
dotnet add <your-project> reference superslice-dotnet/src/Superslice/Superslice.csprojOnce it is on NuGet: dotnet add package Superslice.
It targets net8.0 and netstandard2.0 (so .NET Framework 4.6.1+ and .NET Core 2.0+ work), and depends only on System.Text.Json on netstandard2.0.
Quickstart
using Superslice;
var client = new SupersliceClient("sk_live_..."); // or set SUPERSLICE_API_KEY and pass nothing
var slice = await client.Slices.CreateAsync(new SliceCreateParams
{
Slicer = Slicers.BambuStudio,
Printer = "Bambu Lab A1 0.4 nozzle",
Process = "0.20mm Standard @BBL A1",
Filament = "Bambu PLA Basic @BBL A1",
PlateType = "Textured PEI Plate",
File = SupersliceFile.FromPath("cube.stl"),
});
var result = await client.Slices.WaitAsync(slice.Id); // polls every 2 s, up to 10 min
if (result.Status == SliceStatus.Succeeded)
{
Console.WriteLine($"{result.Result!.PrintTimeS} s, {result.Result.FilamentG} g");
if (result.Price is { } price)
Console.WriteLine($"{price.TotalMinor} {price.Currency} (minor units)");
}
else
{
Console.WriteLine($"failed: {result.Error!.Code}");
}WaitAsync returns a failed slice instead of throwing, so read Error. To slice a model you already uploaded, set Model = ModelRef.FromId("mdl_..."). To have the API download a file, set Model = ModelRef.FromUrl("https://..."). Set exactly one of File and Model. SupersliceFile can also be made from bytes or a stream.
Prices are TotalMinor, a long in the currency's minor unit. There are no floats for money.
Catalogue lookup
Preset names come from the catalogue:
var engines = await client.Engines.ListAsync();
var release = engines.First(e => e.Slicer == Slicers.BambuStudio && e.Status == "active").Id;
var printers = await client.Engines.PrintersAsync(release);
var process = await client.Engines.ProcessesAsync(release, printers[0].Name);
var filament = await client.Engines.FilamentsAsync(release, printers[0].Name);
var plates = await client.Engines.PlateTypesAsync(release, printers[0].Name);Release ids contain @ and +. The library encodes them for you.
A partner preset is passed as PresetRef.FromId("pst_..."). A plain string is a system preset name.
Listing slices
var page = await client.Slices.ListAsync(new SliceListParams { Limit = 50 });
// page.Data, page.NextCursor
await foreach (var s in client.Slices.ListAllAsync())
Console.WriteLine(s.Id);Idempotency
var slice = await client.Slices.CreateAsync(request, idempotencyKey: "order-1042");Repeating a key returns the same slice. A POST /v1/slices is retried only when you pass a key, because a retry without one could slice twice.
Errors
Every error extends SupersliceException and carries Status, Code, Title, Errors, RetryAfter and the raw Body.
| Class | When |
|---|---|
InvalidRequestException | 400, 413 |
AuthenticationException | 401 |
PlanLimitException | 402 |
PermissionException | 403 |
NotFoundException | 404 |
ConflictException | 409 |
RateLimitException | 429 |
ServerException | 5xx |
ConnectionException | no answer from the API |
SupersliceTimeoutException | a request or WaitAsync ran out of time |
WebhookSignatureException | a callback did not verify |
try
{
await client.Slices.GetAsync("job_missing");
}
catch (NotFoundException e)
{
Console.WriteLine($"{e.Code}: {e.Title}");
}
catch (SupersliceException e) when (e.Code == "slice_limit_reached")
{
// ...
}Retries
On 429 the client waits for Retry-After (1 s if absent). On 5xx and network errors it backs off 0.5 s, 1 s, 2 s. It retries up to MaxRetries times (default 2). GET, PUT, PATCH and DELETE retry freely. POST /v1/slices retries only with an idempotency key. POST /v1/presets is not retried.
Webhooks
Verify the raw request body before parsing it:
// ASP.NET Core minimal API
app.MapPost("/superslice", async (HttpRequest req) =>
{
using var ms = new MemoryStream();
await req.Body.CopyToAsync(ms);
try
{
var evt = client.Webhooks.Verify(secret, req.Headers["Superslice-Signature"], ms.ToArray());
if (evt.Event == "slice.completed")
Console.WriteLine(evt.GetSlice()!.Result!.PrintTimeS);
return Results.Ok();
}
catch (WebhookSignatureException)
{
return Results.BadRequest();
}
});Verify checks the HMAC-SHA256 of {t}.{body} in constant time, accepts any matching v1 value, and rejects a timestamp more than 300 seconds from now (change it with toleranceSeconds). It returns the event or throws WebhookSignatureException. Webhook.Verify does the same without a client. The same event can arrive twice: ignore an EventId you have seen.
Configuration
var client = new SupersliceClient("sk_live_...", new SupersliceClientOptions
{
BaseUrl = "https://api.superslice.co", // default
Timeout = TimeSpan.FromSeconds(60), // default, per attempt
MaxRetries = 2, // default
HttpClient = myHttpClient, // optional
});Without an API key argument the client reads SUPERSLICE_API_KEY and throws ArgumentException at construction if that is missing. Create one client and reuse it.
Other resources: client.Models (GetAsync, DeleteAsync), client.Presets (CreateAsync, ListAsync, GetAsync, UpdateAsync, DeleteAsync), client.Pricing (GetAsync, SetAsync, DeleteAsync), client.Slicers (ListAsync, UpdateAsync). Every async method takes a CancellationToken.