CoreWeave Sandbox Python Client
A Python client library for CoreWeave Sandboxes (CWSandboxes), offering a programmatic interface to manage sandboxed environments for secure execution of workloads on the CoreWeave platform. It is currently at version 0.16.0 and maintains an active release cadence with frequent updates.
Warnings
- gotcha Authentication to the CoreWeave platform is required. The `cwsandbox` client relies on proper configuration, often via environment variables (e.g., `COREWEAVE_API_KEY`, `COREWEAVE_PROJECT_ID`) or a configured CoreWeave CLI/SDK context. The provided quickstart assumes this setup.
- gotcha The term 'cwsandbox' in this context refers to a client for CoreWeave's remote sandboxing service, not a local Python-level sandbox for executing untrusted Python code. Misunderstanding this distinction can lead to incorrect security assumptions if you intend to sandbox local Python execution.
- gotcha Failing to explicitly stop sandboxes (`sb.stop()`) can lead to resource leakage and unexpected billing, as sandbox instances on the CoreWeave platform may continue to run if not properly terminated.
- deprecated The `Sandbox.run` factory method provides a hybrid synchronous/asynchronous API (`.result()` blocks). While convenient, mixing sync and async code can be a footgun. Older versions or alternative methods might have different blocking behaviors.
Install
-
pip install cwsandbox
Imports
- Sandbox
from cwsandbox import Sandbox
Quickstart
import os
from cwsandbox import Sandbox
# Ensure COREWEAVE_API_KEY and COREWEAVE_PROJECT_ID (or similar) are set in your environment
# For demonstration, using dummy values. In production, use actual credentials.
# os.environ['COREWEAVE_API_KEY'] = os.environ.get('CWSANDBOX_API_KEY', 'your_api_key_here')
# os.environ['COREWEAVE_PROJECT_ID'] = os.environ.get('CWSANDBOX_PROJECT_ID', 'your_project_id_here')
try:
# Create and run a simple command in a sandbox
print("Starting sandbox...")
sb = Sandbox.run("echo", "Hello, World! from cwsandbox")
print(f"Sandbox ID: {sb.id}")
# Wait for the sandbox to complete and get results (synchronous wait)
result = sb.stop().result()
print("Sandbox output:")
print(result.output)
print(f"Sandbox exited with code: {result.exit_code}")
except Exception as e:
print(f"An error occurred: {e}")
print("Ensure your CoreWeave environment is configured and authenticated.")