> For AI agents: the complete Sanity documentation index is available at [https://www.sanity.io/docs/llms.txt](https://www.sanity.io/docs/llms.txt).

# Configure Media Library with Blueprints

Configure Media Library fields as code with defineMediaLibraryConfig, a singleton config resource.

Media Library is Sanity's organization-wide asset management. With Blueprints, you can manage its configuration as code with a singleton config resource, deployed alongside your other infrastructure.

> [!WARNING]
> Beta
> Singleton configs as blueprint resources are in beta. Their fields can change in a future release of `@sanity/blueprints`.

In this guide, you'll define a Media Library configuration resource with Blueprints and deploy the blueprint to Sanity.

Prerequisites:

- The latest version of the `sanity` CLI is recommended to interact with Blueprints. You can always run the latest CLI commands with `npx sanity@latest`.
- An organization with Media Library and permission to configure it.
- `@sanity/blueprints` 0.26.0 or later.

## Initialize a new blueprint

To initialize a blueprint in the current directory, run the command below. Replace the project ID with your own. Skip to the next section if you already have a blueprint set up.

**npm**

```shell
npx sanity@latest blueprints init . --type ts --project-id <project-id> --stack-name production
```

**pnpm**

```shell
pnpm dlx sanity@latest blueprints init . --type ts --project-id <project-id> --stack-name production
```

**yarn**

```shell
yarn dlx sanity@latest blueprints init . --type ts --project-id <project-id> --stack-name production
```

**bun**

```shell
bunx sanity@latest blueprints init . --type ts --project-id <project-id> --stack-name production
```



## Configure Media Library

Use `defineMediaLibraryConfig` to declare the custom fields your Media Library exposes. Blueprints resolves your organization's Media Library installation automatically.

**sanity.blueprint.ts**

```typescript
import {defineBlueprint, defineMediaLibraryConfig} from '@sanity/blueprints'

export default defineBlueprint({
  resources: [
    defineMediaLibraryConfig({
      name: 'media-library',
      root: './media-library',
      fields: [{name: 'brand', title: 'Brand', src: './src/fields/brand.tsx'}],
    }),
  ],
})
```

- `name`: The resource name, unique within the blueprint.
- `root`: The config directory, relative to the blueprint file. It contains the config's `package.json`.
- `fields`: The custom fields. Declare at least one. Each field has a unique `name`, a `title`, a `src` path relative to `root`, and an optional `public` flag that makes the field readable outside your organization.

Fields use the same shape as `unstable_defineMediaLibrary` in `sanity.cli.ts`. For what a field's `src` file exports, see the [Extension API reference](https://www.sanity.io/docs/dashboard-beta/extension-api-reference).

## Deploy the blueprint

Next, deploy the blueprint.

**npm**

```shell
npx sanity blueprints deploy
```

**pnpm**

```shell
pnpm dlx sanity blueprints deploy
```

**yarn**

```shell
yarn dlx sanity blueprints deploy
```

**bun**

```shell
bunx sanity blueprints deploy
```

After the deployment finishes, your Media Library configuration is applied to your organization.

If you need to make changes, update the blueprint file (`sanity.blueprint.ts`) and run the deploy command again.

## Destroy a deployed blueprint

Sometimes you want to remove a deployed resource so it won't run anymore or affect any future usage quotas. 

To remove a resources created with a blueprint, you need to either:

1. Remove the definition from the blueprint, and run the `deploy` command again.
2. Destroy the blueprint with the `destroy` command.

The `blueprints destroy` command removes, or undeploys*,* the blueprint and all of its resources from Sanity's infrastructure. It does not remove your local files. 

**npm**

```shell
npx sanity blueprints destroy
```

**pnpm**

```shell
pnpm dlx sanity blueprints destroy
```

**yarn**

```shell
yarn dlx sanity blueprints destroy
```

**bun**

```shell
bunx sanity blueprints destroy
```

To remove the resource from the blueprint locally, you can remove it from the `resources` array in the `sanity.blueprint.ts` file, then delete any associated files.

### Redeploying a destroyed blueprint

When you run `blueprints destroy`, it's as if you never used `blueprints init` during setup. The only difference is you still have all the files in your directory. To use this blueprint again and redeploy it, you'll need to let Sanity know about it. You can do this by running init again:

**npm**

```shell
npx sanity blueprints init
```

**pnpm**

```shell
pnpm dlx sanity blueprints init
```

**yarn**

```shell
yarn dlx sanity blueprints init
```

**bun**

```shell
bunx sanity blueprints init
```

This launches an editing interface that lets you reconfigure the blueprint, if needed, and it reconnects the blueprint to Sanity. Now you can add more functions or redeploy. Keep in mind that any environment variables added before destroying the blueprint will not carry over.



## Learn more about Media Library

#### Explore Media Library

[Media Library introduction](https://www.sanity.io/docs/media-library/introduction)
Learn about Media Library, how to incorporate it into your workflow, and how to get started.

[Configure your studios and CLI to use Media Library](https://www.sanity.io/docs/media-library/configure-library)
Once Media Library is enabled for your organization, you can connect it to your Studio and the CLI.

[Define an application with Blueprints](https://www.sanity.io/docs/blueprints/deploy-an-application-with-blueprints)
Deploy an SDK app as a blueprint resource with defineApplication and its view definers.

