docs
SupportConsole

PHP

The official PHP library for the Superslice API.

Requires PHP 8.1 or newer with the curl and json extensions. It has no other dependencies, so it drops into WordPress and WooCommerce plugins without version conflicts.

Source, issues and releases: superslice-co/superslice-php.

Install

The package is not yet published to Packagist. Until it is, install from GitHub by adding the repository to your composer.json:

json
{
    "repositories": [
        { "type": "vcs", "url": "https://github.com/superslice-co/superslice-php" }
    ],
    "require": {
        "superslice/superslice-php": "dev-main"
    }
}

Then run composer install. Once it is on Packagist, composer require superslice/superslice-php is enough.

Quickstart

php
require 'vendor/autoload.php';

$client = new Superslice\Client(['api_key' => getenv('SUPERSLICE_API_KEY')]);

$slice = $client->slices->create([
    'file' => 'bracket.stl',
    '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',
]);

$slice = $client->slices->wait($slice['id']);

if ($slice['status'] === 'failed') {
    echo $slice['error']['code'], "\n";
} else {
    echo $slice['result']['print_time_s'], " s\n";
    echo $slice['result']['filament_g'], " g\n";
    if ($slice['price'] !== null) {
        printf("%s %.2f\n", $slice['price']['currency'], $slice['price']['total_minor'] / 100);
    }
}

file can be a path, an open stream, or ['contents' => $bytes, 'filename' => 'part.stl']. To slice a model you already uploaded, pass 'model' => ['id' => 'mdl_...'] instead of file. To have us download one, pass 'model' => ['source_url' => 'https://...', 'filename' => 'part.stl'].

Responses are plain associative arrays. price.total_minor is an integer in the currency's minor unit; never store money as a float. The array shapes are documented with PHPDoc in src/Types.php, and fields the API adds later appear in the arrays without breaking anything.

wait() polls every 2 seconds for up to 10 minutes and returns the slice when it is succeeded or failed. A failed slice is returned, not thrown. It throws Superslice\TimeoutError if time runs out:

php
$client->slices->wait($id, ['interval' => 1, 'timeout' => 120]);

Other slice methods:

php
$client->slices->get($id);
$page = $client->slices->list(['limit' => 50]);   // ['data' => [...], 'next_cursor' => ...]
foreach ($client->slices->listAll() as $slice) {} // follows next_cursor for you

Catalogue lookup

php
$engines = $client->engines->list()['data'];
$release = 'bambu-studio@02.09.02.62+02.09.00.89';

$client->engines->printers($release);
$client->engines->processes($release, 'Bambu Lab A1 0.4 nozzle');
$client->engines->filaments($release, 'Bambu Lab A1 0.4 nozzle');
$client->engines->plateTypes($release, 'Bambu Lab A1 0.4 nozzle');

Release names contain @ and +. The library URL-encodes them.

Other resources:

php
$client->models->get('mdl_...');
$client->models->delete('mdl_...');

$client->presets->create('my-process.json', 'bambu-studio');
$client->presets->list();
$client->presets->get('pst_...');
$client->presets->update('pst_...', ['name' => 'Fine detail']);
$client->presets->delete('pst_...');

$client->pricing->get();
$client->pricing->set(['enabled' => true, 'currency' => 'USD', 'minimum' => '2.00', 'lines' => [
    ['name' => 'Filament', 'basis' => 'gram', 'amount' => '0.05'],
]]);
$client->pricing->delete();

$client->slicers->list();
$client->slicers->update('bambu-studio', ['mode' => 'pick', 'engine_releases' => [$release]]);

Idempotency

Pass an idempotency key so a repeated request returns the same slice instead of slicing twice:

php
$client->slices->create($params, ['idempotency_key' => 'order-1042']);

Use one key per order. Repeating a key with a different body raises ConflictError (idempotency_conflict).

Errors

The API answers errors as application/problem+json. Each family has its own class, all extending Superslice\SupersliceError:

ClassWhen
AuthenticationError401
PermissionError403
NotFoundError404
ConflictError409
RateLimitError429
InvalidRequestError400, 413
PlanLimitError402
ServerError5xx
ConnectionError, TimeoutErrorthe request never got an answer
SignatureErrora callback failed verification
php
try {
    $client->slices->get('job_missing');
} catch (Superslice\NotFoundError $e) {
    echo $e->status;      // 404
    echo $e->errorCode;   // not_found
    echo $e->title;
    print_r($e->errors);  // field errors of an invalid_request: [['path' => ..., 'message' => ...]]
    echo $e->retryAfter;  // seconds, when the API sent Retry-After
    print_r($e->body);    // the decoded problem body
}

PHP reserves getCode() for an integer, so it returns the HTTP status. The API's string code is $e->errorCode. Every code is listed at https://docs.superslice.co/errors.

Retries

On 429 the client waits for Retry-After seconds (1 second if the header is missing). On 5xx and network errors it backs off 0.5, 1 and 2 seconds. It makes up to max_retries retries (default 2). GET, PUT, PATCH and DELETE retry freely. POST /v1/slices retries only when you pass an idempotency key, because a blind retry could slice twice. Preset uploads are not retried.

Webhooks

Superslice sends slice.completed and slice.failed callbacks signed in the Superslice-Signature header. Verify the raw body before you parse it:

php
$raw = file_get_contents('php://input');
$header = $_SERVER['HTTP_SUPERSLICE_SIGNATURE'] ?? null;

try {
    $event = $client->webhooks->verify(getenv('SUPERSLICE_WEBHOOK_SECRET'), $header, $raw);
} catch (Superslice\SignatureError $e) {
    http_response_code(400);
    exit('invalid signature');
}

if ($event['event'] === 'slice.completed') {
    echo $event['data']['id'], ' ', $event['data']['result']['print_time_s'], " s\n";
}

verify checks the HMAC-SHA256 of {t}.{rawBody} against every v1 value in constant time and rejects timestamps more than 300 seconds old. Change that with ['toleranceSeconds' => 600]. An event can arrive more than once, so store event_id and skip repeats. verify makes no network calls. Without an API key, use (new Superslice\Resources\Webhooks())->verify(...).

Configuration

php
$client = new Superslice\Client([
    'api_key' => 'sk_...',                       // default: SUPERSLICE_API_KEY
    'base_url' => 'https://api.superslice.co',   // default
    'timeout' => 60,                             // seconds
    'max_retries' => 2,
]);

A missing key throws InvalidArgumentException at construction. Every request sends Authorization: Bearer <key> and User-Agent: superslice-php/0.1.0.