box for the client and sandbox for a sandbox returned by
box.sandboxes.create() or box.sandboxes.get().
Configure the client
lithosbox python to use the key you saved during installation.
For other Python environments, provide LITHOSBOX_TOKEN through the environment
or pass token= to the client. Keep credentials outside your source code.
Keep a client open while you use its sandbox handles. Call
box.close() when
finished, or use with LithosBox() as box:. Closing the client does not delete its
sandboxes.
For asynchronous code, use AsyncLithosBox, async with, and await:
Create and find sandboxes
create
box.sandboxes.create()
Returns a Sandbox. Choose at most one of image, template, or snapshot_id.
Omit all three to use the default environment.
Template and snapshot creates use the source’s CPU and memory configuration.
The SDK reuses the same sandbox ID during retries within a create call. If a call
fails without a response, inspect existing sandboxes before making a new create call.
A handle exposes
sandbox.id and sandbox.info, including its state, image, CPU,
and memory configuration. with box.sandboxes.create() as sandbox: deletes the
sandbox when the block ends.
Commands, sessions, and files
run
sandbox.run(command, *, background=False, stdin=None)
Returns an ExecResult with stdout, stderr, and exit_code. Call .check() to
raise on a nonzero exit code. stdout_truncated, stderr_truncated, and truncated
indicate output limits were reached.
With
background=True, the result reports startup status. No process ID is
returned. Redirect output to a file and check the job’s eventual exit status
separately.
Foreground completion does not guarantee every child process has exited. Use
background mode explicitly for servers and workers. Synchronous commands allow
about 110 seconds; each output stream is limited to 16 MiB. See Run commands.
session
sandbox.session(session_id=None)
Returns a Session that keeps its working directory, environment variables, and
shell state between commands.
Use
with sandbox.session() as session: to close the shell when the block ends.
Handles using the same session ID share that shell; closing one ends it for all
handles. An individual session command has the same timeout as sandbox.run().
A sandbox supports up to 256 concurrent sessions. See
Keep shell state for an example.
files
sandbox.files
Writes allow up to 4 MiB per call. Reads allow approximately 16 MiB of text or
12 MiB of binary data. Use larger-file transfers
for files beyond those limits.
write() does not append.
Public endpoints
sandbox.expose(port, public_port=None) returns IngressInfo. Its id identifies
the endpoint and its url is the public HTTPS address. Use the assigned public
port by leaving public_port unset.
Start an HTTP server on the specified sandbox port before using the URL. Remove
an endpoint with box.ingress.delete(endpoint.id). List endpoints in the console
or with GET /vms/{id}/ingress.
See Expose a web service for a complete example.
Lifecycle and checkpoints
See Manage sandbox lifecycle for when to use each action.
snapshot
sandbox.snapshot(*, leave_paused=False, wait_durable=False, timeout=120)
Returns SnapshotInfo, including id, vm_id, state, and durable. The
sandbox keeps running by default. leave_paused=True lets it rest after the
checkpoint; the next command or file operation wakes it automatically.
Use wait_durable=True to wait until saving completes, with timeout in seconds.
A timeout does not cancel saving. Restore with box.snapshots.restore(snapshot);
it accepts either a snapshot object or its ID and returns a new Sandbox.
fork
sandbox.fork(n=None)
Without n, return one independent Sandbox. With n, return a list of that many
copies; n=1 returns a one-element list. Up to four copies can be requested at once.
Copies include files and running processes. Fork between commands and reconnect
external services independently in each copy. See Save, restore, fork.
Activity, logs, and metrics
Recent metrics cover approximately two hours at 30-second intervals. Historical
metrics use minute intervals and remain available for archived and deleted
sandboxes.
measured=false identifies a missing measurement. System logs and
recent metrics are unavailable while archived.
Recent CPU samples report milliseconds used over interval_s. Divide cpu_ms by
interval_s * 1000 * cpus for a fraction of allocated CPU capacity. Historical
points use util_seconds for the measured duration; do not assume every point
contains a full minute of measurements.
See Monitor and troubleshoot for examples.
Templates, registries, and exports
Registry credentials can also use
secret= or ECR role settings: kind="ecr-assume-role",
role_arn, and external_id. Export file links are temporary; download before their
expiry. See HTTP API for export fields.
Existing aliases include exec, exec_background, stop/start, branch, and
files.ls. The guides use run, archive/unarchive, fork, and files.list.
Errors
Error classes are available from
lithosbox.errors. API errors expose status
and body where a response is available. SandboxPausedError and
SandboxStoppedError are subclasses of ConflictError.
box.rate.acquire and box.rate.ops expose rate-limit information from the
client’s recent responses. They may be None before the first response. Treat the
values as advisory and follow retry_after on rate-limit errors. See
Limits and HTTP status codes.