Choose authentication
These examples run on serverless capacity unless a section specifies CoreWeave Kubernetes Service (CKS). Choose the credential for the account that owns your sandboxes. See Choose and get credentials to obtain a key or token. Save the configuration for your language beside the scripts you run from this page. Python examples importAUTH from sandbox_auth.py. TypeScript examples import client from sandbox-client.mts. These files select authentication without storing your credential. For Python, use an active virtual environment. For TypeScript, first install the client with npm install @coreweave/cwsandbox.
- Forge API key
- CoreWeave API access token
Use your Forge API key with the SDK’s
wandb integration. Replace [WANDB-API-KEY] with your API key:- Python
- TypeScript
sandbox_auth.py
placement_mode="cks" on sandbox creation or in SandboxDefaults. In TypeScript, choose the CoreWeave token and set a nonempty runnerIds array to select CKS. Creating a Python session with auth=AUTH also authenticates the sandboxes it owns. Keep the same credential when reconnecting, listing, or cleaning up sandboxes.
Choose a volume type
Use a scratch volume for working space inside one run. Use a registered Volume for data that outlives a sandbox or that several sandboxes read at once, such as a shared dataset or model weights.
Scratch volumes
A scratch volume is created with the sandbox and destroyed with it. Declare one withScratchVolumeOptions in the volumes list.
- Python
- TypeScript
ScratchVolumeOptions accepts the following fields:
Choose a storage medium
StorageMedium.DISK backs the volume with local disk. StorageMedium.MEMORY backs it with tmpfs, which is faster but counts against the mounting container’s memory request and against the policy’s memory ceiling.
Because an unbounded RAM-backed volume could exhaust the node, a MEMORY volume must declare a size. It may do so as an absolute quantity, or as an integer percentage of the sandbox’s resolved memory request, such as "50%". The percentage form is valid only for MEMORY. A DISK volume must use an absolute size.
Mount a scratch volume on several containers
mount_path is a convenience that mounts the volume on the primary container. In a multi-container sandbox, omit mount_path and declare the mounts on each container instead, so that more than one container can reach the same volume.
Registered Volumes
The PVC-backed registered Volumes in this guide require a CoreWeave API access token and registered Volumes enabled for your organization. The Forge API key tab in Choose authentication applies to scratch volumes. The following examples use a PVC in your CKS cluster and a CoreWeave API access token inCWSANDBOX_API_KEY. Complete Use your own compute before registering or mounting a PVC-backed Volume.
A registered Volume points at a PersistentVolumeClaim (PVC) that already exists in one of your clusters. Registering it makes the claim mountable by sandboxes without granting them access to the cluster itself. The Volume is a metadata pointer: CoreWeave never copies the data, and deleting the Volume never touches the claim.
Every attach is a live view of the same claim, so a write from one sandbox is visible to every other sandbox mounting it.
The TypeScript client doesn’t support registered Volumes. Use the Python client to register, mount, and manage them.
Register a Volume
Registering, updating, or deleting a Volume requires Sandbox Admin. Sandbox User can list, read, validate, and mount registered Volumes. Sandbox Admin includes Sandbox User permissions. See Sandbox roles.Volume.create() returns immediately with the Volume in the VALIDATING state, then the control plane checks that the claim exists and is usable. Poll with wait_until_ready() before you mount it.
The source and the
read_only flag are also immutable after creation. Only description can change later.PvcVolumeSource takes these fields:
Set
read_only=True on Volume.create() to make read-only a property of the Volume itself. That setting is a floor, not a default: a mount can be read-only on a writable Volume, but no mount can write to a read-only Volume.
Mount a registered Volume
Mount the Volume by ID withRegisteredVolumeOptions. The Volume must be READY at start, or the sandbox fails with CWSANDBOX_VOLUME_NOT_READY.
RegisteredVolumeOptions requires name, volume_id, and an absolute mount_path other than /. It also accepts sub_path, which combines with any sub_path set at registration, and read_only.
A sandbox echoes the Volumes it mounted in attached_volume_ids.
Locality and placement
A Volume’slocality reports whether it constrains placement:
cluster_local: the Volume lives in one cluster, so sandboxes mounting it are scheduled onto that cluster’s runner. PVC-backed Volumes are cluster-local.global: the Volume is reachable from any cluster and does not constrain placement.
CWSANDBOX_VOLUME_PLACEMENT_CONFLICT.
Manage registered Volumes
Use the following operations to list, inspect, update, and deregister the Volumes in your organization.Volume.list() paginates automatically and accepts states and runner_ids filters. delete() accepts allow_missing=True, which makes deleting an already-deleted Volume succeed instead of raising.
Deleting a Volume that non-terminal sandboxes still mount fails with CWSANDBOX_VOLUME_IN_USE, which the Python client raises as VolumeInUseError. Stop the attached sandboxes first.
Volume states
A registered Volume moves through the following states over its lifecycle:wait_until_ready() polls until READY. It raises VolumeError if the Volume reaches ERROR or DELETING, and VolumeWaitTimeoutError if the timeout elapses first.
Failure reasons
Use this table to interpret the failure reasons that sandbox and Volume operations can return.Related resources
For more on related sandbox features, see the following resources:- File system snapshots: persist and restore a sandbox’s filesystem.
- Grant sandboxes access to AI Object Storage: read and write buckets from inside a sandbox.
- Sandbox configuration: the rest of the sandbox create options
- Distributed File Storage: create the PersistentVolumeClaims that registered Volumes point at.