Python SDK
Use the Python SDK to manage sandboxes from applications, notebooks, and automation. It can reuse gateway registration and authentication state created by the OpenShell CLI. Use the SDK and gateway from the same OpenShell release when possible.
Install the SDK
The package requires Python 3.11 or later. Add it to your project with uv:
The openshell package contains the SDK. It does not install the OpenShell CLI.
Connect to a Registered Gateway
Register and select a gateway with the CLI first. Then construct the client from the active gateway:
from_active_cluster() reads the selected gateway endpoint, TLS files, and
OIDC token state. It refreshes an expiring OIDC token by default and writes the
rotated bundle back for other OpenShell processes.
For a direct local plaintext connection, pass a host:port endpoint:
For service automation against an OIDC gateway, use
ClientCredentialsAuth. Non-loopback endpoints require TLS:
Retrieve Provider Credentials
Connect directly with a trusted OU=operator client certificate after enabling
operator authentication.
Do not supply a bearer token or use an OIDC login for this connection. No sandbox
creation or provider attachment is required.
ProviderClient also accepts an existing grpc.Channel. Its
get_credentials() method requires an explicit workspace and key selection.
It returns a dictionary of ProviderCredentialValue objects with value and
optional expiration_time, a UTC datetime. Pass a timedelta for
minimum_remaining_lifetime; omission or zero uses the gateway default.
The helper uses the connection’s timeout and does not retry or cache exports.
Gateway failures raise GatewayError; invalid local selections raise ValueError.
The value is excluded from the object’s repr, but serialization still exposes
it. Keep exported secrets out of logs and shared output. See
Retrieve Runtime Credentials
for freshness, supported credentials, and the export security boundary.
Create and Use a Sandbox
Python SDK methods take an explicit workspace. This example creates a sandbox, waits for readiness, runs a command, and waits for deletion:
Use create_session() when you want an object that retains the sandbox name and
workspace for repeated exec, stop, start, and delete operations. List methods
return lazy Pager instances; use list_all() only when you want to fetch the
complete collection.
Next Steps
- Review Manage Sandboxes for labels, templates, services, and Python SDK examples.
- Review Gateway Authentication for OIDC and client credentials.
- Review API Errors for structured error handling.