Example gallery
The TypeScript SDK repo ships runnable, CI-typechecked examples in
examples/
covering the jobs the API plan is bought for —
ephemeral compute, webhook consumption, and CI-driven configuration. They are
written to be copied whole: the cost-safety idioms (idempotent launch,
guaranteed terminate) are baked into the code around the happy path, and
that scaffolding is the point, not boilerplate.
Every example runs with just SLOTHBOX_API_KEY set, plus the obvious
resource ids; each file documents its full environment contract in its header
comment, summarized in the
examples README.
The examples are TypeScript today. Python equivalents are planned for the
slothbox package — until then, the patterns translate directly (the
launch walkthrough and
SDK webhooks page show each idiom in both languages).
ephemeral-worker.ts
Launch a box per task, do the work, always terminate — the scale-to-zero
pattern, where a box exists only for the duration of one unit of work and you
pay for compute only while the task runs. Three idioms make it safe to run
unattended: the launch carries an Idempotency-Key derived from the task id,
so a retried task re-attaches to the original box instead of paying for a
second one; everything after the launch runs inside try/finally, so
termination happens on every failure path (failed waiter, failed work,
timeout); and termination is confirmed by polling until the box reports
terminated, exiting non-zero if it can't confirm — a box you cannot confirm
dead is a box you must assume is still billing.
webhook-receiver-express.ts
A verified webhook receiver on Node/Express, enforcing the three rules of
receiving: verify the raw body (express.raw(), never
parse-then-restringify), ack fast and do the work after responding, and
handle events idempotently by deduping on the event id. The event switch
includes webhook.endpoint.disabled — the one event to treat as an incident,
because it means deliveries to you have stopped — and a default branch for
event types newer than your SDK version.
webhook-receiver-cloudflare-worker.ts
The same receiver as a Cloudflare Worker: the SDK's webhook toolkit is
WebCrypto-only, so it runs on Workers as-is. The Workers-specific moves are
reading the body with request.text() (which preserves the exact signed
bytes), acking immediately and finishing work via ctx.waitUntil(), and
deduping in a shared store (KV, a Durable Object, D1) because isolates
are ephemeral and you'll run in many at once.
env-config-sync.ts
Push secrets and variables from your CI provider's secret store into an org's
env-config, from where they're injected into boxes at launch — run it as a
deploy-pipeline step on every release. It's idempotent by construction (the
API's PUT is an upsert, so re-running is always safe) and the header
comment carries the scope warnings that matter before you copy it: org scope
reaches every box in the org, narrower scopes override it, secrets are
write-only through the API, and the upserts need an owner-role key.
examples/README.md
The gallery's index: a run-command table, each example's environment contract, and how CI keeps the examples honest — they're typechecked against the built package on every commit, resolved exactly the way an npm consumer resolves it, so a breaking change to the SDK surface fails the build until the examples are updated.