Skip to main content

Official Python SDK for the Slothbox API

Project description

slothbox (Python SDK)

Official Python SDK for the Slothbox API — typed, sync and async, with retries, cursor pagination, lifecycle waiters, and a webhook verification toolkit. Feature parity with @slothbox/sdk is tracked in PARITY_CHECKLIST.md.

pip install slothbox

Requires Python 3.10+ and an org service-account API key (sk_…, API plan) from the Slothbox dashboard — the SDK identifies itself on every request and the API rejects SDK traffic on other credentials (see SDK identification).

Quickstart

from slothbox import Slothbox

client = Slothbox()  # reads SLOTHBOX_API_KEY; or Slothbox(api_key="sk_…")

orgs = client.organizations.list()
envs = client.environments.list(org_id)

# Safe launch: always sends an Idempotency-Key, then polls until `running`.
box = client.environments.launch_and_wait(org_id, {"templateId": template_id})
print(box.env_id, box.status)

Async is the same surface with await:

from slothbox import AsyncSlothbox

async with AsyncSlothbox() as client:
    box = await client.environments.launch_and_wait(org_id, {"templateId": template_id})

Request bodies accept either the typed models (slothbox.models) or plain dicts keyed by the wire (JSON) names, as above.

SDK identification

Every request carries an always-on identification header:

x-slothbox-sdk: slothbox-sdk-python/<version>

The API uses it to identify SDK traffic, which must authenticate with an org service-account key (API plan). SDK-identified requests on any other credential are rejected 403 with code sdk_requires_service_key, raised as PermissionDeniedError. Identification wins: the header is set as a client-level default (so it also covers direct calls through the client.generated escape hatch) and re-asserted on every request the wrapper sends, so it cannot be removed or overridden — not via RequestOptions(headers=…), not via httpx_args, and not via the generated with_headers().

Errors

Every non-2xx response raises a typed exception with the API's machine-readable code, the gateway request id, and validation issues:

from slothbox import ConflictError, RateLimitError

try:
    client.environments.launch(org_id, {"templateId": template_id})
except ConflictError as err:
    if err.code == "template_not_baked":
        client.templates.wait_until_baked(org_id, template_id)
except RateLimitError as err:
    print("retry after", err.retry_after, "s — request", err.request_id)

Retries

GET/HEAD/PUT/DELETE are retried on 429/5xx/network errors (3 retries by default) with capped full-jitter backoff; Retry-After is honored. POST and PATCH are never blind-retried — a duplicated launch provisions a second EC2 box — unless the request carries an Idempotency-Key (environments.launch(..., idempotency_key=…); launch_and_wait always sends one). Tune with Slothbox(max_retries=…) or per request via RequestOptions(max_retries=…).

Pagination

The audit and webhook-delivery lists are cursor-paginated; iterating a page walks every page:

for event in client.audit.list_org_events(org_id, limit=100):
    ...

page = client.webhooks.list_deliveries(org_id, endpoint_id)
while page.has_next_page():
    page = page.get_next_page()

(async for on the async client.)

Webhooks

Verify deliveries with the exact raw bytes you received:

from slothbox import parse_webhook_event, WebhookVerificationError

try:
    event = parse_webhook_event(raw_body, request_headers, endpoint_secret)
except WebhookVerificationError as err:
    return 400, err.code

if event["type"] == "environment.stopped":
    ...

The verifier implements Standard Webhooks v1 (HMAC-SHA256, secret rotation, ±300s replay window) and is tested against the same production-signed vector file as the API's signer and the TS SDK.

Architecture

Per ADR 0001: a pinned openapi-python-client generates the typed core (slothbox._generated — committed, httpx + attrs, regenerated deterministically by scripts/generate.sh and diff-checked in CI), and the hand-written slothbox package on top is the public compatibility surface. client.generated exposes the generated AuthenticatedClient as an escape hatch — note that the generated set_httpx_client() bypasses auth; pass transports via Slothbox(httpx_args=…) instead.

Development

python -m venv .venv && .venv/bin/pip install -e '.[dev]'
ruff check . && ruff format --check .   # lint
mypy                                     # strict on the wrapper
pytest                                   # 3.10+ supported
scripts/generate.sh                      # regen the core from openapi.json

Releases are automated — see RELEASING.md.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

slothbox-0.1.0.tar.gz (121.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

slothbox-0.1.0-py3-none-any.whl (308.8 kB view details)

Uploaded Python 3

File details

Details for the file slothbox-0.1.0.tar.gz.

File metadata

  • Download URL: slothbox-0.1.0.tar.gz
  • Upload date:
  • Size: 121.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for slothbox-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d5b234270ecf42c1762f15aaca84e3cf961d4d9a57874e9d1bce072caaa042ae
MD5 9443de00796c0b2961e137c4aa82614f
BLAKE2b-256 65a32ab99f4caed971f085c197f3463faf824184f2daada10c2da759a33e7c71

See more details on using hashes here.

Provenance

The following attestation bundles were made for slothbox-0.1.0.tar.gz:

Publisher: release.yml on sloth-box/sdk-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file slothbox-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: slothbox-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 308.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for slothbox-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5a6302b908b2ed51b3b2439f1b2daf7d6b6d10c77da8834ad4849a91c11019e6
MD5 bef731efac3406a931388393acb3bd04
BLAKE2b-256 728651ed32aa153fdef7c57f5e279c45e30024fe891a47aae472452299f6da5b

See more details on using hashes here.

Provenance

The following attestation bundles were made for slothbox-0.1.0-py3-none-any.whl:

Publisher: release.yml on sloth-box/sdk-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page