> ## 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.

# Create a drive

> Create a drive in a region, optionally with a name and labels, and wait until it's ready to mount.

Create a [drive](https://docs.baseten.co/drives/overview.md) to share files between sandboxes. Create it
in the region where the sandboxes that mount it run.

> [!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.

## Set up

The drive examples use the same SDKs and CLI as sandboxes. The management
client creates, gets, lists, updates, and deletes drives. Mounting goes
through a sandbox.

Every example reads your API key from the `BASETEN_API_KEY` environment
variable, so keep the key out of your code.

**To set your API key**:

Replace `<BASETEN_API_KEY>` with a
[Baseten API key](https://docs.baseten.co/organization/api-keys.md#create-an-api-key), then run the
following command in the shell where you run the examples:

```bash theme={"system"}
export BASETEN_API_KEY="<BASETEN_API_KEY>"
```

**To install and connect your SDK or CLI**:

> [tab] TypeScript
>
> 1. Run the following command to install the SDK:
>
>    ```bash theme={"system"}
>    npm install @basetenlabs/client @basetenlabs/sandbox undici
>    ```
>
> 2. Create the management client, which reads the key from the
>    environment:
>
>    ```typescript theme={"system"}
>    import { ManagementClient } from "@basetenlabs/client";
>
>    const client = new ManagementClient({ apiKey: process.env.BASETEN_API_KEY! });
>    ```

> [tab] Python
>
> 1. Run the following command to install the SDK:
>
>    ```bash theme={"system"}
>    pip install --upgrade baseten "httpx[http2]"
>    ```
>
> 2. Create the management client, which reads the key from the
>    environment:
>
>    ```python theme={"system"}
>    import os
>
>    from baseten.client import ManagementClient
>
>    client = ManagementClient(api_key=os.environ["BASETEN_API_KEY"])
>    ```

> [tab] Go
>
> 1. Run the following command to install the SDK:
>
>    ```bash theme={"system"}
>    go get github.com/basetenlabs/baseten-go@latest
>    ```
>
> 2. Create the management client, which reads the key from the
>    environment:
>
>    ```go theme={"system"}
>    management, err := client.NewManagementClient(client.ManagementClientOptions{
>        APIKey: os.Getenv("BASETEN_API_KEY"),
>    })
>    if err != nil {
>        log.Fatal(err)
>    }
>    ```
>
>    Import `github.com/basetenlabs/baseten-go/client`.

> [tab] API
>
> The [drive API](https://docs.baseten.co/reference/management-api/overview.md#drives) doesn't accept
> your API key directly. Exchange it for an
> [access token](https://docs.baseten.co/sandboxes/authentication.md) with the `drives` scope. Request
> the `sandboxes` scope too, so one token also works for mount requests.
>
> 1. Run the following command to
>    [create a token](https://docs.baseten.co/reference/management-api/sandboxes/create-a-sandbox-access-token.md):
>
>    ```bash theme={"system"}
>    curl --request POST \
>      --url https://api.baseten.co/v1/token \
>      --header "Authorization: Bearer $BASETEN_API_KEY" \
>      --header "Content-Type: application/json" \
>      --data '{"scopes": ["sandboxes", "drives"]}'
>    ```
>
>    If drives aren't turned on for your organization, the request returns
>    HTTP `403`.
>
> 2. Replace `<TOKEN>` with the response's `token` value, then run the
>    following command:
>
>    ```bash theme={"system"}
>    export SANDBOX_TOKEN="<TOKEN>"
>    ```

> [tab] CLI
>
> 1. [Install or update the Baseten CLI](https://docs.baseten.co/reference/cli/baseten/overview.md#install).
>
> 2. Run the following command to sign in:
>
>    ```bash theme={"system"}
>    baseten auth login
>    ```
>
> The drive commands are [`baseten drive`](https://docs.baseten.co/reference/cli/baseten/drive)
> and [`baseten sandbox drive`](https://docs.baseten.co/reference/cli/baseten/sandbox#drive).

## Create a drive

You can't change the name or region later. A name is unique in your team. Use
1 to 49 characters: lowercase letters, digits, and hyphens, and don't start or
end the name with a hyphen. Omit `name` and Baseten generates one. Creation
returns before the drive is ready. The drive is ready when its status is
`DEPLOYED`.

**To create a drive named `shared-notes`**:

> [tab] TypeScript
>
> Run the following code, which waits until the drive is ready:
>
> ```typescript theme={"system"}
> import { ManagementClient } from "@basetenlabs/client";
>
> const client = new ManagementClient({ apiKey: process.env.BASETEN_API_KEY! });
> let drive = await client.drives.create({
>   name: "shared-notes",
>   labels: { project: "alpha" },
> });
> while (drive.status === "DEPLOYING") {
>   await new Promise((resolve) => setTimeout(resolve, 2000));
>   drive = await client.drives.getInfo({ name: "shared-notes" });
> }
> console.log(drive.status, drive.region);
> ```

> [tab] Python
>
> Run the following code, which waits until the drive is ready:
>
> ```python theme={"system"}
> import os
> import time
>
> from baseten.client import ManagementClient
>
> with ManagementClient(api_key=os.environ["BASETEN_API_KEY"]) as client:
>     drive = client.drives.create(name="shared-notes", labels={"project": "alpha"})
>     while drive.status == "DEPLOYING":
>         time.sleep(2)
>         drive = client.drives.get_info(name="shared-notes")
>     print(drive.status, drive.region)
> ```

> [tab] Go
>
> Run the following code, which waits until the drive is ready:
>
> ```go theme={"system"}
> package main
>
> import (
>     "context"
>     "fmt"
>     "log"
>     "os"
>     "time"
>
>     "github.com/basetenlabs/baseten-go/client"
> )
>
> func main() {
>     ctx := context.Background()
>     management, err := client.NewManagementClient(client.ManagementClientOptions{
>         APIKey: os.Getenv("BASETEN_API_KEY"),
>     })
>     if err != nil {
>         log.Fatal(err)
>     }
>     drive, err := management.Drives().Create(ctx, client.DriveCreateOptions{
>         Name:   "shared-notes",
>         Labels: map[string]string{"project": "alpha"},
>     })
>     if err != nil {
>         log.Fatal(err)
>     }
>     for drive.Status == client.DriveStatusDeploying {
>         time.Sleep(2 * time.Second)
>         drive, err = management.Drives().GetInfo(ctx, client.DriveGetInfoOptions{Name: "shared-notes"})
>         if err != nil {
>             log.Fatal(err)
>         }
>     }
>     fmt.Println(drive.Status, drive.Region)
> }
> ```

> [tab] API
>
> 1. Run the following command to
>    [create the drive](https://docs.baseten.co/reference/management-api/drives/create-a-drive.md):
>
>    ```bash theme={"system"}
>    curl --request POST \
>      --url https://api.baseten.co/v1/drives/instances \
>      --header "Authorization: Bearer $SANDBOX_TOKEN" \
>      --header "Content-Type: application/json" \
>      --data '{"name": "shared-notes", "labels": {"project": "alpha"}}'
>    ```
>
>    The request returns HTTP `201` with the drive's record. Its `status`
>    can still be `DEPLOYING`.
>
> 2. Run the following command to [get the drive](https://docs.baseten.co/reference/management-api/drives/get-a-drive.md)
>    until the response's `status` is `DEPLOYED`:
>
>    ```bash theme={"system"}
>    curl --url https://api.baseten.co/v1/drives/instances/shared-notes \
>      --header "Authorization: Bearer $SANDBOX_TOKEN"
>    ```
>
>    The response's `region` field holds the drive's region.
>
> If you belong to more than one team, add the `team_id` query parameter or
> the `X-Team-Id` header to each request.

> [tab] CLI
>
> 1. Run the following command to create the drive:
>
>    ```bash theme={"system"}
>    baseten drive create --name shared-notes --label project=alpha
>    ```
>
> 2. Run the following command until it prints `"DEPLOYED"`:
>
>    ```bash theme={"system"}
>    baseten drive describe --name shared-notes --jq '.status'
>    ```
>
> If you belong to more than one team, add `--team "<TEAM>"` to each
> command.

## Create a drive in a specific region

A drive defaults to your default sandbox region. Only sandboxes in the drive's
region can mount it, so create it in the region that your sandboxes run in.

**To create a drive in a specific region**:

> [tab] TypeScript
>
> Run the following code. It prints the regions available to your account,
> then creates the drive in `us-was-1`. Replace `us-was-1` with a name from
> the list.
>
> ```typescript theme={"system"}
> import { ManagementClient } from "@basetenlabs/client";
> import { SandboxClient } from "@basetenlabs/sandbox";
>
> const apiKey = process.env.BASETEN_API_KEY!;
> const sandboxes = new SandboxClient({ apiKey });
> const { regions } = await sandboxes.rawApi.getSandboxConfiguration();
> for (const region of regions) {
>   console.log(region.name, region.location);
> }
>
> const management = new ManagementClient({ apiKey });
> const drive = await management.drives.create({
>   name: "shared-notes",
>   region: "us-was-1",
> });
> console.log(drive.region);
> ```

> [tab] Python
>
> Run the following code. It prints the regions available to your account,
> then creates the drive in `us-was-1`. Replace `us-was-1` with a name from
> the list.
>
> ```python theme={"system"}
> import os
>
> from baseten.client import ManagementClient
> from baseten.sandbox import SandboxClient
>
> api_key = os.environ["BASETEN_API_KEY"]
> with SandboxClient(api_key=api_key) as sandboxes:
>     for region in sandboxes.raw_api.get_sandbox_configuration().regions:
>         print(region.name, region.location)
>
> with ManagementClient(api_key=api_key) as management:
>     drive = management.drives.create(name="shared-notes", region="us-was-1")
>     print(drive.region)
> ```

> [tab] Go
>
> Run the following code. It prints the regions available to your account,
> then creates the drive in `us-was-1`. Replace `us-was-1` with a name from
> the list.
>
> ```go theme={"system"}
> package main
>
> import (
>     "context"
>     "fmt"
>     "log"
>     "os"
>
>     "github.com/basetenlabs/baseten-go/client"
>     "github.com/basetenlabs/baseten-go/sandbox"
> )
>
> func main() {
>     ctx := context.Background()
>     apiKey := os.Getenv("BASETEN_API_KEY")
>     sandboxes, err := sandbox.NewClient(sandbox.ClientOptions{APIKey: apiKey})
>     if err != nil {
>         log.Fatal(err)
>     }
>     configuration, err := sandboxes.API().GetSandboxConfiguration(ctx)
>     if err != nil {
>         log.Fatal(err)
>     }
>     for _, region := range configuration.Regions {
>         fmt.Println(region.Name, region.Location)
>     }
>
>     management, err := client.NewManagementClient(client.ManagementClientOptions{APIKey: apiKey})
>     if err != nil {
>         log.Fatal(err)
>     }
>     drive, err := management.Drives().Create(ctx, client.DriveCreateOptions{
>         Name:   "shared-notes",
>         Region: "us-was-1",
>     })
>     if err != nil {
>         log.Fatal(err)
>     }
>     fmt.Println(drive.Region)
> }
> ```

> [tab] API
>
> 1. Run the following command to
>    [list the regions](https://docs.baseten.co/reference/management-api/sandboxes/get-sandbox-configuration.md)
>    available to your account:
>
>    ```bash theme={"system"}
>    curl --url https://api.baseten.co/v1/sandboxes/configuration \
>      --header "Authorization: Bearer $SANDBOX_TOKEN"
>    ```
>
>    Each entry in `regions` has a `name`, such as `us-was-1`, and a
>    `location`.
>
> 2. Run the following command to create the drive in `us-was-1`. Replace
>    `us-was-1` with a name from the list:
>
>    ```bash theme={"system"}
>    curl --request POST \
>      --url https://api.baseten.co/v1/drives/instances \
>      --header "Authorization: Bearer $SANDBOX_TOKEN" \
>      --header "Content-Type: application/json" \
>      --data '{"name": "shared-notes", "region": "us-was-1"}'
>    ```

> [tab] CLI
>
> The CLI doesn't list sandbox regions. To create the drive in the region of
> a sandbox that will mount it:
>
> 1. Run the following command to print the region of the sandbox named
>    `hello-world`:
>
>    ```bash theme={"system"}
>    baseten sandbox describe --name hello-world --jq '.region'
>    ```
>
> 2. Run the following command to create the drive in that region, such as
>    `us-was-1`:
>
>    ```bash theme={"system"}
>    baseten drive create --name shared-notes --region us-was-1
>    ```

## Next steps

> [card] [Mount a drive](https://docs.baseten.co/drives/mount-drives.md)
>
> Mount the drive in a running sandbox.

> [card] [Get a drive](https://docs.baseten.co/drives/get-drives.md)
>
> Check a drive's status, region, and labels.
