Choose authentication
These examples run on serverless capacity unless a section specifies CKS. Choose the credential for the account that owns your sandboxes. See Choose and get credentials to obtain a key or token. Run the installation commands in an active Python virtual environment. Save the Python code from your credential tab assandbox_auth.py beside the Python scripts you run from this page. It stores the authentication choice, not your credential. The examples import AUTH from that file and pass it explicitly to each new sandbox or session.
- W&B API key
- CoreWeave API access token
Use your Forge API key through the SDK’s W&B authentication strategy:
sandbox_auth.py
placement_mode="cks" on sandbox creation or in SandboxDefaults. Creating a session with auth=AUTH also authenticates the sandboxes it owns. Keep the same credential when reconnecting, listing, or cleaning up sandboxes.
Basic operations
The examples in this section cover the most common file operations: writing data to a path in the sandbox and reading data back. File operations returnOperationRef objects. Use .result() to block for completion.
Write files
Read files
Parallel operations
File operations return immediately, enabling natural parallelism. Running uploads or downloads in parallel can reduce total transfer time when you have multiple independent files.Parallel uploads
Parallel downloads
Upload-process-download pattern
A common workflow: upload input files, run processing, download results.Error handling
File operations can fail if a path doesn’t exist or isn’t writable. CatchSandboxFileError to handle these cases and inspect the affected path.
File not found
Write errors
Binary files
File operations work with any binary content, including images and serialized Python objects:Text encoding
Files are transferred as bytes, so you must encode and decode text explicitly to avoid corruption with non-ASCII characters.Large file considerations
Theread_file and write_file APIs are best suited for small to moderately sized files. For large files, consider the following:
- Account for bandwidth, because files are transferred through the API.
- Use streaming for large datasets when possible.
- Access object storage directly instead of transferring the data through the API.
object_storage_access parameter creates temporary CoreWeave AI Object Storage credentials and provides them to the sandbox’s containers. Your workload then reads and writes buckets directly, so large objects never pass through read_file or write_file. Your organization must first register a Workload Identity Federation configuration with the sandbox control plane; see Object storage access.
The following example uses CoreWeave authentication to start a sandbox with read-write access to one bucket, then confirms the credentials were injected. Set CWSANDBOX_API_KEY to a CoreWeave API access token for the configured organization, and replace [BUCKET-NAME] with the name of your bucket:
object_prefix. For workloads that only read data, use ObjectStoragePermission.READ.
For large temporary data that stays local to the sandbox, attach a volume instead. See Volumes.