# Sirv plugin for Sanity Studio

![Sanity logo](https://sirv.sirv.com/website/sanity-logo.png?w=230)

Add Sirv as a first-class media source inside [Sanity Studio](https://www.sanity.io/) (v3, v4 and v5).

Using an AI coding agent?

Install the [`image-optimization`](https://www.skills.sh/sirv/image-optimization-skill/image-optimization) skill, and your agent can set up Sirv images in your project. Ask in plain words, such as “Serve our product images from Sirv and make them load faster.” [See all 8 skills](/help/rest-api/sirv-skills-for-ai-agents/)

Editors connect their Sirv account and browse the Sirv DAM (folders, search, thumbnails and live previews).

They pick images, videos, [360 spins](/help/spin/getting-started/how-spin-files-are-created/), [Sirv Media Viewers](/help/sirv-media-viewer/), [3D models](/help/sirv-media-viewer/media-types/3d-model/) or any file directly from a field. No more switching tabs to copy URLs.

Picked Sirv-native assets **stay on Sirv**.

Your content stores only the asset path, delivery domain and dimensions. Your frontend renders them with the companion package `@sirv/react`.

It gives you responsive `srcset`, lazy loading, sirv.js spins, viewers and 3D models, and combined galleries.

Images are delivered at the perfect dimensions in the [optimal format](/help/dynamic-imaging/format-quality/format/) (often WebP or AVIF), typically **40-70% smaller**, from the nearest of Sirv’s global CDN servers.

-   **No backend required** – the Studio talks to Sirv’s [REST API](/help/rest-api/sirv-rest-api/) directly.
-   **No credentials shipped in the plugin** – each editor pastes their own per-account REST credentials, stored encrypted in the dataset’s secrets.
-   **Self-hosted friendly** – designed for a locally-run or self-hosted Studio.

## Installation

1.  Install the plugin (and the frontend renderer) from npm:
    
    ```bash
    npm install @sirv/sanity-plugin
    # to display picked assets on your website
    npm install @sirv/react
    ```
    
2.  Add the plugin to your `sanity.config.ts`. It registers everything automatically:
    
    ```js
    import {defineConfig} from 'sanity'
    import {sirv} from '@sirv/sanity-plugin'
    
    
    export default defineConfig({
      // ...your config
      plugins: [sirv()],
    })
    ```
    
3.  Start your Studio. A new **Sirv** tool appears in the top menu, and a **Browse Sirv** option appears on every image and file field.
    

### Requirements

-   Sanity Studio v3, v4 or v5.
-   React 18 or 19.
-   A Sirv account. [Create one free](https://app.sirv.com/signup) if you don’t have one yet.

## Connect your Sirv account

The first time you use the plugin, connect your Sirv account from the **Sirv** tool (cog icon in the Studio’s top menu).

1.  In Sirv, open [Settings > API access](https://app.sirv.com/settings/api) and copy your REST API **Client ID** and **Client secret**.
    
2.  Paste them into the Sirv tool and click **Connect**:
    
    ![Connect your Sirv account with a client ID and secret](https://sirv.sirv.com/website/screenshots/sanity/sanity-connect-credentials.png?profile=screenshots-grey-border)
    
3.  Choose the **delivery domain** to serve assets from (your `account.sirv.com` host or a [custom domain](/help/getting-started/custom-domain/)).
    
    That’s it. The connection is saved in the dataset and reused silently on future visits. Use **Disconnect** in the same tool to switch accounts.
    

**Use a private dataset.** The connection (your REST credentials) is saved as an encrypted dataset document, so a private dataset keeps it from being publicly readable.

The credentials are never bundled into the Studio JavaScript, and the plugin ships none of its own.

## Usage

There are two ways to use Sirv in your content model: the automatic **Browse Sirv** option on standard fields, and the dedicated Sirv field types you add to your schema.

### Browse Sirv on image and file fields

Every standard `image` and `file` field automatically gains a **Browse Sirv** option in its _Select_ menu. Picking an asset imports it into Sanity (served afterwards from Sanity’s CDN).

Image fields show images only; file fields show every asset type, including generic files such as PDF and ZIP.

![The Sirv DAM browser inside Sanity Studio](https://sirv.sirv.com/website/screenshots/sanity/sanity-dam-browser.png?profile=screenshots-grey-border)

No configuration is needed – just having an image or file field is enough:

```js
defineField({name: 'cover', type: 'image'})    // gains "Browse Sirv"
defineField({name: 'brochure', type: 'file'})  // any Sirv file (PDF, zip, ...)
```

### sirvMedia field (recommended)

The `sirvMedia` field stores a single Sirv-native asset. The asset **stays on Sirv**; the field keeps its path, delivery domain, dimensions and optional transformations.

Restrict the pickable types with `options.allowedTypes`. Alt text and caption are auto-filled from the asset’s Sirv metadata.

```js
defineField({
  name: 'hero',
  type: 'sirvMedia',
  options: {allowedTypes: ['image', 'video']}, // image | video | spin | view | model
})
```

### sirvMediaList (gallery) field

An ordered gallery of Sirv-native assets, with a persistent add button and **multi-select** in the browser (tick several assets, add them together).

```js
defineField({
  name: 'gallery',
  type: 'sirvMediaList',
  options: {allowedTypes: ['image', 'video', 'spin', 'view', 'model']},
})
```

### sirvAssetUrl field

A `url` field whose value is a picked Sirv asset’s delivery URL (any type, generic files included). Use it when you just need a plain string URL.

```js
defineField({name: 'downloadUrl', type: 'sirvAssetUrl'})
```

### 3D models (.glb)

Sirv treats GLB 3D models as a native media type, alongside images, videos, 360 spins and Sirv Media Viewers.

Pick a `.glb` file into a `sirvMedia` or `sirvMediaList` field, and the DAM browser shows a live, rotatable preview. Render it on your site with `<SirvModel>`. Drag to rotate, scroll to zoom:

To restrict a field to 3D models only, set `allowedTypes` to `['model']`:

```js
defineField({
  name: 'model3d',
  type: 'sirvMedia',
  options: {allowedTypes: ['model']},
})
```

Render it on the frontend with the dedicated component, or let `<SirvMedia>` dispatch automatically from a stored value:

```js
import {SirvModel, SirvMedia, fromSanityMedia} from '@sirv/react'

// directly, by path:
<SirvModel path="/models/chair.glb" width={600} height={600} />

// or from a stored sirvMedia value:
<SirvMedia value={fromSanityMedia(post.model3d)} />
```

## Supported asset types

| Type | Extensions | Rendered by @sirv/react as |
| --- | --- | --- |
| Image | jpg, png, webp, avif, gif, … | `<SirvImage>` (responsive srcset + lazy) |
| Video | mp4, mov, webm, … | `<SirvVideo>` (poster + controls) |
| 360 Spin | .spin | `<SirvSpin>` (sirv.js) |
| Sirv Media Viewer | .view | `<SirvView>` (sirv.js composite) |
| 3D Model | .glb | `<SirvModel>` (sirv.js) |
| File | pdf, zip, … | imported into Sanity / plain URL |

## Display on your website

Sirv-native values are plain JSON, so you fetch them with a normal GROQ query and render them with the companion package [`@sirv/react`](https://www.npmjs.com/package/@sirv/react).

It handles responsive `srcset`, lazy loading, and auto-loads sirv.js for spins and viewers.

```js
import {SirvProvider, SirvMedia, SirvGallery, fromSanityMedia} from '@sirv/react'

export function Post({post}) {
  return (
    <SirvProvider alias="your-account.sirv.com">
      {/* single asset: image, video, spin, view or 3D model */}
      <SirvMedia value={fromSanityMedia(post.hero)} width={800} />

      {/* gallery: separate items, or one combined Sirv Media Viewer */}
      <SirvGallery items={post.gallery.map(fromSanityMedia)} layout="viewer" />
    </SirvProvider>
  )
}
```

For Next.js, you can also plug Sirv into `next/image` as a custom loader – see the [@sirv/react documentation](https://www.npmjs.com/package/@sirv/react) for every component and the loader.

## Stored value

A `sirvMedia` value (and each item of a `sirvMediaList`) is a flat object:

```js
{
  "_type": "sirvMedia",
  "mediaType": "image",            // image | video | spin | view | model
  "sirvPath": "/products/shoe.jpg",
  "sirvAlias": "your-account.sirv.com", // chosen delivery domain
  "originalUrl": "https://your-account.sirv.com/products/shoe.jpg",
  "width": 2000, "height": 1500,
  "alt": "...", "caption": "...",
  "transformations": {"quality": 82}
}
```

A `sirvAssetUrl` field stores a plain URL string. Standard `image` and `file` fields store regular Sanity asset references.

## Common questions

Do I need a server or backend?

No. The Studio calls the Sirv REST API directly. Nothing else to host.

Where are my Sirv credentials stored?

Encrypted in your Sanity dataset's secrets, per project. They are never bundled into the Studio JavaScript and the plugin ships none of its own. Use a private dataset.

Do assets get copied into Sanity?

Only when you use the **Browse Sirv** option on a standard image/file field. The recommended `sirvMedia` / `sirvMediaList` / `sirvAssetUrl` fields keep assets on Sirv and store just a reference.

Which frameworks can render the assets?

Any React 18/19 frontend (Next.js, Remix, Vite, and so on) via `@sirv/react`. The stored value is plain JSON, so non-React frontends can build Sirv URLs from it directly.

Can I restrict which asset types a field accepts?

Yes, with `options.allowedTypes` on `sirvMedia` and `sirvMediaList` (any of `image`, `video`, `spin`, `view`, `model`).
