> ## Documentation Index
> Fetch the complete documentation index at: https://docs.baseten.co/llms.txt
> Use this file to discover all available pages before exploring further.

# Mount a drive

> Mount a drive in a running sandbox, as the whole drive or one folder, read-write or read-only.

Mount a [drive](https://docs.baseten.co/drives/overview.md) in a running sandbox to read and write its
files at a path you choose. The sandbox must be in the drive's region. You
can't attach a drive when you create a sandbox; mount it once the sandbox is
running.

[Set up your SDK or the CLI](https://docs.baseten.co/drives/create-drives.md#set-up) before running these
examples. They use a drive named `shared-notes`, created as in
[Create a drive](https://docs.baseten.co/drives/create-drives.md), and a running sandbox named
`hello-world` in the same region, created as in
[Create a sandbox](https://docs.baseten.co/sandboxes/manage.md#create-a-sandbox).

> [!NOTE]
>
> Drives are in pre-release in the Baseten CLI (`baseten drive` and `baseten sandbox drive`) and in the Python, TypeScript, and Go SDKs (`baseten`, `@basetenlabs/client`, `@basetenlabs/sandbox`, and `baseten-go`). Commands, methods, and responses can change. The [Management API drive operations](https://docs.baseten.co/reference/management-api/overview.md#drives) are experimental.

## Mount a drive

The mount path must be an absolute path that no other mount uses, such as
`/mnt/notes`. A mount lasts until you [unmount it](https://docs.baseten.co/drives/unmount-drives.md).

**To mount `shared-notes` at `/mnt/notes`**:

> [tab] TypeScript
>
> Run the following code:
>
> ```typescript theme={"system"}
> import { SandboxClient } from "@basetenlabs/sandbox";
>
> const client = new SandboxClient({ apiKey: process.env.BASETEN_API_KEY! });
> const sandbox = await client.get({ name: "hello-world" });
> const mount = await sandbox.drives.mount({
>   name: "shared-notes",
>   mountPath: "/mnt/notes",
> });
> console.log(mount.mountPath, mount.readOnly);
> ```

> [tab] Python
>
> Run the following code:
>
> ```python theme={"system"}
> import os
>
> from baseten.sandbox import SandboxClient
>
> with SandboxClient(api_key=os.environ["BASETEN_API_KEY"]) as client:
>     sandbox = client.get(name="hello-world")
>     mount = sandbox.drives.mount(name="shared-notes", mount_path="/mnt/notes")
>     print(mount.mount_path, mount.read_only)
> ```

> [tab] Go
>
> Run the following code:
>
> ```go theme={"system"}
> package main
>
> import (
>     "context"
>     "fmt"
>     "log"
>     "os"
>
>     "github.com/basetenlabs/baseten-go/sandbox"
> )
>
> func main() {
>     ctx := context.Background()
>     client, err := sandbox.NewClient(sandbox.ClientOptions{
>         APIKey: os.Getenv("BASETEN_API_KEY"),
>     })
>     if err != nil {
>         log.Fatal(err)
>     }
>     sb, err := client.Get(ctx, sandbox.GetOptions{Name: "hello-world"})
>     if err != nil {
>         log.Fatal(err)
>     }
>     mount, err := sb.Drives().Mount(ctx, sandbox.DriveMountOptions{
>         Name:      "shared-notes",
>         MountPath: "/mnt/notes",
>     })
>     if err != nil {
>         log.Fatal(err)
>     }
>     fmt.Println(mount.MountPath, mount.ReadOnly)
> }
> ```

> [tab] API
>
> You mount a drive with a call to the
> [sandbox runtime API](https://docs.baseten.co/sandboxes/run-commands.md#call-the-sandbox-runtime-api-over-http). Use
> a token with both the `sandboxes` and `drives` scopes, and set
> `SANDBOX_URL` to the sandbox's URL; see
> [Get the sandbox URL](https://docs.baseten.co/sandboxes/run-commands.md#get-the-sandbox-url).
>
> Run the following command to
> [mount the drive](https://docs.baseten.co/reference/sandbox-api/drive/attach-a-drive-to-a-local-path.md):
>
> ```bash theme={"system"}
> curl --request POST \
>   --url "${SANDBOX_URL%/}/drives/mount" \
>   --header "Authorization: Bearer $SANDBOX_TOKEN" \
>   --header "Content-Type: application/json" \
>   --data '{"driveName": "shared-notes", "mountPath": "/mnt/notes"}'
> ```
>
> The response's `success` field is `true`.

> [tab] CLI
>
> Run the following command:
>
> ```bash theme={"system"}
> baseten sandbox drive mount --name hello-world --drive-name shared-notes --mount-path /mnt/notes
> ```

**To confirm the mount**:

Run the following command to write a file to the drive and read it back:

```bash theme={"system"}
baseten sandbox exec --name hello-world -- 'echo hello > /mnt/notes/hello.txt && cat /mnt/notes/hello.txt'
```

The command prints `hello`. The file stays on the drive after you unmount it
or delete the sandbox.

## Mount one folder of a drive

Mount a folder of the drive instead of the whole drive. Give the folder's path
from the drive's root, starting with `/`.

**To mount the drive's `/projects/alpha` folder at `/mnt/alpha`**:

> [tab] TypeScript
>
> Run the following code with the same sandbox:
>
> ```typescript theme={"system"}
> await sandbox.drives.mount({
>   name: "shared-notes",
>   mountPath: "/mnt/alpha",
>   drivePath: "/projects/alpha",
> });
> ```

> [tab] Python
>
> Run the following code with the same sandbox:
>
> ```python theme={"system"}
> sandbox.drives.mount(
>     name="shared-notes", mount_path="/mnt/alpha", drive_path="/projects/alpha"
> )
> ```

> [tab] Go
>
> Run the following code with the same sandbox:
>
> ```go theme={"system"}
> _, err = sb.Drives().Mount(ctx, sandbox.DriveMountOptions{
>     Name:      "shared-notes",
>     MountPath: "/mnt/alpha",
>     DrivePath: "/projects/alpha",
> })
> if err != nil {
>     log.Fatal(err)
> }
> ```

> [tab] API
>
> Run the following command:
>
> ```bash theme={"system"}
> curl --request POST \
>   --url "${SANDBOX_URL%/}/drives/mount" \
>   --header "Authorization: Bearer $SANDBOX_TOKEN" \
>   --header "Content-Type: application/json" \
>   --data '{"driveName": "shared-notes", "mountPath": "/mnt/alpha", "drivePath": "/projects/alpha"}'
> ```

> [tab] CLI
>
> Run the following command:
>
> ```bash theme={"system"}
> baseten sandbox drive mount --name hello-world --drive-name shared-notes --mount-path /mnt/alpha --drive-path /projects/alpha
> ```

## Mount a drive read-only

A read-only mount lets the sandbox read the drive's files but not change them.
Use one for a sandbox that only reads shared inputs.

**To mount `shared-notes` read-only at `/mnt/notes-ro`**:

> [tab] TypeScript
>
> Run the following code with the same sandbox:
>
> ```typescript theme={"system"}
> await sandbox.drives.mount({
>   name: "shared-notes",
>   mountPath: "/mnt/notes-ro",
>   readOnly: true,
> });
> ```

> [tab] Python
>
> Run the following code with the same sandbox:
>
> ```python theme={"system"}
> sandbox.drives.mount(name="shared-notes", mount_path="/mnt/notes-ro", read_only=True)
> ```

> [tab] Go
>
> Run the following code with the same sandbox:
>
> ```go theme={"system"}
> _, err = sb.Drives().Mount(ctx, sandbox.DriveMountOptions{
>     Name:      "shared-notes",
>     MountPath: "/mnt/notes-ro",
>     ReadOnly:  true,
> })
> if err != nil {
>     log.Fatal(err)
> }
> ```

> [tab] API
>
> Run the following command:
>
> ```bash theme={"system"}
> curl --request POST \
>   --url "${SANDBOX_URL%/}/drives/mount" \
>   --header "Authorization: Bearer $SANDBOX_TOKEN" \
>   --header "Content-Type: application/json" \
>   --data '{"driveName": "shared-notes", "mountPath": "/mnt/notes-ro", "readOnly": true}'
> ```

> [tab] CLI
>
> Run the following command:
>
> ```bash theme={"system"}
> baseten sandbox drive mount --name hello-world --drive-name shared-notes --mount-path /mnt/notes-ro --read-only
> ```

## Share a drive between sandboxes

Mount the same drive in each sandbox that needs its files. Each sandbox mounts
it separately, at its own mount path, and can mount it read-write or
read-only.

**To pass a file from one sandbox to another**:

This procedure uses a second running sandbox named `reader` in the drive's
region.

1. Run the following command to write a file from `hello-world`, where the
   drive is mounted read-write at `/mnt/notes`:

   ```bash theme={"system"}
   baseten sandbox exec --name hello-world -- 'echo "step 1 done" > /mnt/notes/status.txt'
   ```

2. Run the following command to mount the drive read-only in `reader`:

   ```bash theme={"system"}
   baseten sandbox drive mount --name reader --drive-name shared-notes --mount-path /mnt/notes --read-only
   ```

3. Run the following command to read the file from `reader`:

   ```bash theme={"system"}
   baseten sandbox exec --name reader -- cat /mnt/notes/status.txt
   ```

   The command prints `step 1 done`.

## Troubleshoot mounting

| Response | Cause and fix |
| - | - |
| `400` | The request is invalid, such as a relative mount path or a drive path that doesn't start with `/`. Fix the path. |
| `403` | The sandbox can't access the drive. Check that drives are turned on for your organization and that the drive is in the sandbox's region. [Contact us](mailto:support@baseten.co) to request access. |
| `409` | Another mount already uses the mount path. Choose another path, or [unmount](https://docs.baseten.co/drives/unmount-drives.md) what's there. |

## Next steps

> [card] [List drive mounts](https://docs.baseten.co/drives/list-drive-mounts.md)
>
> See which drives a sandbox has mounted.

> [card] [Unmount a drive](https://docs.baseten.co/drives/unmount-drives.md)
>
> Detach a drive from a sandbox and keep its files.
