Ruby
The official Ruby library for the Superslice API.
Source, issues and releases: superslice-co/superslice-ruby.
Install
The gem is not yet published to RubyGems. Until it is, install from GitHub. In your Gemfile:
gem "superslice", git: "https://github.com/superslice-co/superslice-ruby.git"Once published: gem install superslice. Needs Ruby 3.0 or newer. No runtime dependencies.
Quickstart
require "superslice"
client = Superslice::Client.new(api_key: ENV["SUPERSLICE_API_KEY"])
slice = 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",
metadata: { order_id: "1042" } },
file: "cube.stl"
)
slice = client.slices.wait(slice[:id])
if slice[:status] == "succeeded"
puts slice[:result][:print_time_s]
puts slice[:result][:filament_g]
puts slice[:price][:total_minor] if slice[:price]
else
puts slice[:error][:code]
endEvery response is a plain Hash with symbol keys, exactly as the API returns it (see the API reference for the shapes). Fields the library does not know about are kept, so new API fields never break parsing. Money is an integer in price[:total_minor].
To slice a model you already uploaded, or one at a URL, send JSON instead of a file:
client.slices.create({ slicer: "prusa-slicer", printer: "...", process: "...", filament: "...",
model: { id: "mdl_..." } })
client.slices.create({ slicer: "prusa-slicer", printer: "...", process: "...", filament: "...",
model: { source_url: "https://example.com/part.stl" } })file: takes a path, a Pathname or an IO. For an IO without a path, add filename: "part.stl".
Other calls: slices.get(id), slices.list(limit:, cursor:) (returns { data:, next_cursor: }), slices.list_all (an Enumerator over every slice), slices.wait(id, interval: 2, timeout: 600), models.get(id), models.delete(id).
Catalogue lookup
Preset names come from the catalogue. Releases contain @ and +; the library encodes them for you.
release = client.engines.list[:data].find { |e| e[:slicer] == "bambu-studio" && e[:status] == "active" }[:id]
printers = client.engines.printers(release)[:data]
name = printers.first[:name]
client.engines.processes(release, name)
client.engines.filaments(release, name)
client.engines.plate_types(release, name)Presets, pricing and slicer settings:
client.presets.create("shop-pla.json", "bambu-studio")
client.presets.list
client.presets.update("pst_...", name: "Shop PLA")
client.presets.delete("pst_...")
client.pricing.get
client.pricing.set({ enabled: true, currency: "USD", minimum: "2.00", lines: [] })
client.pricing.delete
client.slicers.list
client.slicers.update("bambu-studio", mode: "pick", engine_releases: ["bambu-studio@02.09.02.62+02.09.00.89"])Idempotency
Pass an idempotency key so a repeated request returns the same slice instead of slicing twice:
client.slices.create(params, file: "cube.stl", idempotency_key: "order-1042")Errors
Every error is a Superslice::SupersliceError with status, code, title, errors, retry_after and the raw body.
| Class | When |
|---|---|
AuthenticationError | 401 |
PlanLimitError | 402 |
PermissionError | 403 |
NotFoundError | 404 |
ConflictError | 409 |
InvalidRequestError | 400, 413 |
RateLimitError | 429 |
ServerError | 5xx |
ConnectionError, TimeoutError | transport problems, or slices.wait running out of time |
SignatureError | webhook verification failed |
begin
client.slices.get("job_missing")
rescue Superslice::NotFoundError => e
puts e.code # "not_found"
rescue Superslice::SupersliceError => e
puts "#{e.status} #{e.code}: #{e.title}"
endA slice that fails after it was accepted is not an exception. slices.wait returns it with status: "failed" and error: { code:, message: }.
Retries
On 429 the client waits Retry-After seconds (1 second if absent). On 5xx and network errors it waits 0.5, 1 and 2 seconds. GET, PUT, PATCH and DELETE retry freely. POST /v1/slices retries only when you pass idempotency_key, because a retry without one could slice twice. The default is 2 retries; set max_retries: to change it.
Webhooks
Verify the Superslice-Signature header against the raw request body, before parsing. verify returns the parsed event or raises Superslice::SignatureError.
# Rack or Sinatra style
post "/superslice" do
raw = request.body.read
event = Superslice::Webhooks.verify(ENV["SUPERSLICE_WEBHOOK_SECRET"],
request.env["HTTP_SUPERSLICE_SIGNATURE"], raw)
if event[:event] == "slice.completed"
puts event[:data][:result][:print_time_s]
end
"ok"
rescue Superslice::SignatureError
halt 400, "invalid signature"
endTimestamps more than 300 seconds from now are rejected; change it with tolerance_seconds:. The same event can arrive twice, so ignore event_id values you have seen. client.webhooks.verify is the same function. Webhooks.unwrap is an alias.
Configuration
Superslice::Client.new(
api_key: "...", # default: ENV["SUPERSLICE_API_KEY"]; missing key raises ArgumentError
base_url: "https://api.superslice.co", # default
timeout: 60, # seconds, per request
max_retries: 2
)