Node.js
The official Node.js library for the Superslice API.
It is written in TypeScript, ships ESM and CommonJS builds with types, needs Node 18 or newer, and has no runtime dependencies.
Source, issues and releases: superslice-co/superslice-node.
Install
The package is not yet published to npm. Until it is, install it from GitHub:
npm install github:superslice-co/superslice-nodeOnce it is on npm: npm install @superslice/node.
Quickstart
import Superslice from "@superslice/node";
const client = new Superslice({ apiKey: process.env.SUPERSLICE_API_KEY });
const slice = await client.slices.create({
slicer: "bambu-studio",
printer: "Bambu Lab A1 0.4 nozzle",
process: "0.20mm Standard @BBL A1",
filament: "Bambu PLA Basic @BBL A1",
plate_type: "Textured PEI Plate",
file: "./cube.stl", // a path, bytes, a Blob or a stream
});
const done = await client.slices.wait(slice.id);
if (done.status === "succeeded") {
console.log(done.result!.print_time_s, "s");
console.log(done.result!.filament_g, "g");
if (done.price) console.log(done.price.total_minor, done.price.currency); // integer minor units
} else {
console.log("failed:", done.error?.code);
}slices.wait returns the slice when it is succeeded or failed and throws TimeoutError if it runs out of time. It does not throw for a failed slice: read error.
To slice a model you already uploaded, or one at a URL, leave out file and pass model:
await client.slices.create({ ...settings, model: { id: "mdl_..." } });
await client.slices.create({ ...settings, model: { source_url: "https://example.com/a.stl" } });With CommonJS: const { Superslice } = require("@superslice/node");
Catalogue lookup
Preset names come from the catalogue. Releases contain @ and +; the library encodes them for you.
const { data: engines } = await client.engines.list();
const release = engines.find((e) => e.slicer === "bambu-studio" && e.status === "active")!.id;
const printers = await client.engines.printers(release);
const printer = printers.data[0].name;
const processes = await client.engines.processes(release, printer);
const filaments = await client.engines.filaments(release, printer);
const plates = await client.engines.plateTypes(release, printer);Your organisation may allow only some releases. client.slicers.list() shows which, and the default a request without engine_release runs on.
Idempotency
Pass an idempotency key so a repeated request returns the same slice instead of creating a second one:
await client.slices.create(params, { idempotencyKey: `order-${orderId}` });The same key with different settings throws ConflictError (idempotency_conflict).
Errors
Every API error extends SupersliceError and carries status, code, title, errors (field details for invalid_request), retryAfter (seconds, when sent) and the raw body.
| Class | When |
|---|---|
AuthenticationError | 401 |
PermissionError | 403 |
NotFoundError | 404 |
ConflictError | 409 |
RateLimitError | 429 |
InvalidRequestError | 400, 413 |
PlanLimitError | 402 |
ServerError | 5xx |
ConnectionError, TimeoutError | the request got no answer |
import { NotFoundError, InvalidRequestError } from "@superslice/node";
try {
await client.slices.get("job_missing");
} catch (err) {
if (err instanceof NotFoundError) console.log("no such slice");
else if (err instanceof InvalidRequestError) console.log(err.code, err.errors);
else throw err;
}Retries
On 429 the client waits for Retry-After seconds (1 s if absent). On 5xx and network errors it backs off 0.5 s, 1 s, 2 s. It makes at most maxRetries extra attempts.
POST /v1/slices is retried only when you pass an idempotency key, because a retry without one could slice twice. GET, PUT, PATCH and DELETE are retried freely.
Webhooks
Verify the Superslice-Signature header against the raw request body, before parsing JSON. verify returns the parsed event or throws SignatureVerificationError. It accepts a timestamp within 5 minutes by default.
import express from "express";
app.post("/superslice", express.raw({ type: "application/json" }), (req, res) => {
try {
const event = client.webhooks.verify(
process.env.SUPERSLICE_WEBHOOK_SECRET!,
req.header("superslice-signature"),
req.body, // Buffer
);
if (event.event === "slice.completed") console.log(event.data);
res.sendStatus(200);
} catch {
res.sendStatus(400);
}
});Events can arrive twice. Keep the event_id values you have handled. webhooks.unwrap is an alias of verify.
Configuration
new Superslice({
apiKey: "sk_...", // default: SUPERSLICE_API_KEY
baseUrl: "https://api.superslice.co",
timeout: 60_000, // milliseconds per request
maxRetries: 2,
});Without a key the constructor throws. slices.wait takes { interval, timeout } in milliseconds (defaults 2000 and 600000). slices.listAll() is an async iterator over every slice.
Methods
slices.create(params, { idempotencyKey? }),slices.get(id),slices.list({ limit?, cursor? }),slices.listAll(),slices.wait(id, { interval?, timeout? })models.get(id),models.delete(id)engines.list(),engines.printers(release),engines.processes(release, printer),engines.filaments(release, printer),engines.plateTypes(release, printer)presets.create(file, slicer?),presets.list(),presets.get(id),presets.update(id, { name }),presets.delete(id)pricing.get(),pricing.set(rules),pricing.delete()slicers.list(),slicers.update(slicer, { mode, engine_releases? })webhooks.verify(secret, header, rawBody, { toleranceSeconds? }),webhooks.unwrap(...)
Money is always an integer in minor units (total_minor), never a float. Response objects keep any fields the API adds later.