canonical: https://jentic.com/apis/sanity.io/sanity-media-library

# Sanity Media Library

The Sanity Media Library API stores and organizes images, videos, and files in a Sanity Media Library, a store of assets kept apart from any one project dataset. It uploads assets directly or fetches them from a public HTTPS URL, with a type of image, video, or file and a cdnAccessPolicy of public, authenticated, or private, then queries and mutates asset documents with GROQ and the Content Lake mutation syntax. It also searches assets and the documents that reference them, delivers video through playback info, thumbnails, storyboards, streams, and renditions, generates subtitles with AI or accepts uploaded VTT and SRT files, and manages the signing keys used for protected media.

## For AI agents

Upload images, videos, and files to a Sanity Media Library, query and edit asset documents with GROQ, find where an asset is used, and add subtitles to videos.

## Scope

Does not transform or resize images on the fly, manage project datasets, or edit page content. Use for storing, querying, and serving assets in a Sanity Media Library only.

## Capabilities

- Upload an image, video, or file to a Media Library as multipart form-data or raw bytes, with a title, filename, sha1, and cdnAccessPolicy of public, authenticated, or private
- Import an asset from a public HTTPS URL through Sanity's egress proxy, up to 50 MiB, with a filename in the JSON body
- Query asset documents in a library with a GROQ query by GET or POST, and create, patch, or delete them through mutate with an optional dryRun
- Search assets and references with a term, filter, and projection, and list the documents that reference a given assetId
- Get video playback info with thumbnail, animated preview, storyboard, and subtitle options, or redirect to a stream or named rendition
- Generate AI subtitles for a video in a given languageCode, or upload a VTT or SRT subtitle file, and delete subtitles by subtitleId
- Create, list, read, and delete ed25519 signing keys for a library

## Use cases

### AI agent curating a shared media library

An agent asked to bring a batch of campaign images into Sanity searches Jentic for 'upload an image to the Sanity Media Library', loads the uploadAssetFromUrl operation, and imports each one from its HTTPS URL with a filename, a title, and the cdnAccessPolicy the team wants. It then uses searchAssets to confirm the uploads are findable, without the Sanity token entering its context.

Example prompt: POST `/media-libraries/{libraryId}/from-url` with type image, title Summer hero, cdnAccessPolicy public, and body {url: 'https://cdn.example.com/summer-hero.jpg', filename: 'summer-hero.jpg'}, then GET `/media-libraries/{libraryId}/refsearch` with term summer

### Safe asset cleanup

Deleting an image that a published page still uses breaks the page. Before removing anything, getReferencesToAsset lists the documents that reference an assetId, paged with a cursor and narrowed with excludePaths and perspective. Only unreferenced assets are then deleted through mutateMedia, and dryRun lets the deletion be checked first.

Example prompt: GET `/media-libraries/{libraryId}/references/documents/{assetId}/to` for an asset, and if no references are returned POST `/media-libraries/{libraryId}/mutate` with dryRun true and a delete mutation for that asset id

### Video subtitles and playback

Video in the Media Library needs captions and preview images. The generateSubtitle operation starts AI subtitle generation for a languageCode and returns a processing status right away, which requires AI features enabled for the organization. Teams with existing captions upload a VTT or SRT file with uploadSubtitle instead, and getPlaybackInfo returns thumbnail, animated preview, storyboard, and subtitle details for the player.

Example prompt: POST `/media-libraries/{libraryId}/video/{videoId}/subtitles` with languageCode en, then GET `/media-libraries/{libraryId}/video/{videoId}/playback-info` with subtitlesFormat vtt

### Querying assets with GROQ

Asset documents in a Media Library are queryable with GROQ, the same language used across Sanity. The query operation takes a GROQ query string by GET, and queryPost accepts it in a request body for longer queries, so an agent can list assets by type, find ones missing a title, or project only the fields a report needs.

Example prompt: POST `/media-libraries/{libraryId}/query` with a GROQ query selecting image assets that have no title, and return their ids and filenames

## Key endpoints

| Method | Path | Description |
| --- | --- | --- |
| POST | `/media-libraries/{libraryId}/upload` | Upload an asset |
| POST | `/media-libraries/{libraryId}/from-url` | Upload an asset from a URL |
| POST | `/media-libraries/{libraryId}/query` | Query media with GROQ |
| POST | `/media-libraries/{libraryId}/mutate` | Create, patch, or delete media documents |
| GET | `/media-libraries/{libraryId}/refsearch` | Search assets and references |
| GET | `/media-libraries/{libraryId}/references/documents/{assetId}/to` | List references to an asset |
| POST | `/media-libraries/{libraryId}/video/{videoId}/subtitles` | Generate subtitles with AI |
| GET | `/media-libraries/{libraryId}/video/{videoId}/playback-info` | Get video playback information |

## Key resources

- **Media libraries** — Asset stores addressed by libraryId
- **Assets** — Images, videos, and files with a title, filename, and cdnAccessPolicy
- **References** — Documents that point at an asset
- **Videos and subtitles** — Video playback, renditions, and VTT or SRT subtitles, generated or uploaded
- **Signing keys** — Named ed25519 keys created and revoked per library

## AI readiness

This API is usable in Jentic One now. Its AI-readiness score against Jentic's framework shows where it stands today and where improvements would make it even easier for agents to use.

- **Score:** 64 / 100
- **Maturity:** AI-Aware
- **Dimensions:**
  - Foundational Compliance: 55 / 100
  - Developer Experience & Jentic Compatibility: 67 / 100
  - AI-Readiness & Agent Experience: 51 / 100
  - Agent Usability: 94 / 100
  - Security: 75 / 100
  - AI Discoverability: 64 / 100
- **View full report:** https://jentic.com/apis/sanity.io/sanity-media-library/scorecard
- **How the score is calculated:** https://docs.jentic.com/reference/api-readiness-framework/overview/
- **More about the dimensions:** https://docs.jentic.com/reference/api-readiness-framework/specification/#dimensional-model-overview

### Score it yourself

Every API in the directory is allowlisted, so you can re-score it with no key required.

- **Score your own API:** https://jentic.com/scorecard.md
- **Scoring CLI agent skill:** https://github.com/jentic/jentic-api-scorecard/blob/main/skills/jentic-api-scorecard/SKILL.md

```sh
npx @jentic/api-scorecard-cli score <openapi-url>
```

## Why Jentic

- **Setup:** Wiring the Sanity Media Library by hand means choosing between multipart and raw-byte uploads, passing type, title, and cdnAccessPolicy as query parameters, and knowing which operations need a token and which video routes are open. Through Jentic you install once, import it from the API Directory, store the token once, and your agent calls it.
- **Permission scoping:** Sanity puts the library id, and for video the video id, in the URL path (`/media-libraries/{libraryId}/...`), so a rule can pin your agent to one library and the operations you pick. You choose the operations it may call, so destructive ones like mutate deletions, deleteSubtitle, or deleteSigningKey are not included unless you add them.
- **Credential handling:** Your Sanity token is stored once, encrypted, by your own Jentic One instance and injected at execution time. It never enters the agent's prompt, logs, or context.
- **Discovery method:** Agents search Jentic by intent such as 'upload an image to the Sanity Media Library' or 'generate subtitles for a video', and Jentic returns the matching operation, such as uploadAssetFromUrl or generateSubtitle, with its input schema.

## Related APIs

- **Sanity Assets API** — Upload images and files into a single project dataset
- **Sanity Query API** — Run GROQ queries against project datasets
- **Cloudinary Upload API** — Upload and transform images and video in Cloudinary
- **Mux API** — Video hosting, streaming, and captions

## FAQ

### What authentication does the Sanity Media Library API use?

Per its OpenAPI spec, the Media Library API uses a bearer token (BearerAuth, a JWT) in the Authorization header for uploads, mutations, searches, references, subtitles, and signing keys. The two query operations accept a bearer token optionally. The six video playback, playback redirect, and rendition operations, GET and HEAD, are public operations that declare no security at all. Through Jentic the token is stored by your own instance and attached at call time where it is needed.

### Can I upload an asset from a URL?

Yes. POST to `/media-libraries/{libraryId}/from-url` with a JSON body holding the url and a filename. Sanity fetches a public or preauthorized HTTPS URL through its egress proxy, limited to 50 MiB, and does not forward your credentials or headers to the source. The type, title, and cdnAccessPolicy go in query parameters.

### How do I add subtitles to a Sanity video?

Call generateSubtitle with a languageCode to have AI produce them; it returns a processing status immediately and needs AI features enabled for the organization, otherwise it returns 400. To use your own captions, call uploadSubtitle with the VTT or SRT file as the raw body and languageCode, format, and closedCaptions as query parameters.

### What are the rate limits for the Sanity Media Library API?

The OpenAPI spec does not specify rate limits. Check the Sanity documentation at https://www.sanity.io/docs for current API limits for your plan.

### Is there a Sanity Media Library MCP server?

You don't need an MCP server to give your agent the Sanity Media Library API. Jentic connects it directly from the API Directory: import it, store your Sanity token once, and your agent calls the upload, query, and subtitle operations it needs without loading a separate server's tool definitions.

### How do I find where an asset is used through Jentic?

Search Jentic for 'find documents that reference a Sanity asset', load the getReferencesToAsset operation, and execute GET `/media-libraries/{libraryId}/references/documents/{assetId}/to` with the asset id, following the cursor for more results. To run it on your own infrastructure, install Jentic One from its GitHub repo.

### Can I limit what my agent is allowed to do with the Sanity Media Library API?

Yes. A rule that allows only the upload, from-url, and refsearch operations under one libraryId lets the agent add and find assets in that library, while mutate, subtitle deletion, and signing key changes stay off its list. Every call it makes is logged.
