Media Library

Query aspects in Media Library

Query aspects in the Media Library dataset.

When you create and deploy an aspect, it's stored in the Media Library dataset alongside your asset documents. In this guide, you'll query your Media Library based on your aspects.

Prerequisites:

  • API version v2025-02-19 or later

Some examples use JavaScript's fetch to perform API calls, but the same principles apply regardless of the language or request library.

Retrieve aspect data in a dataset query

Visibility

To retrieve aspect data in your dataset queries, supply the media reference along with the aspect name to the media::aspect GROQ function.

The media field is a Global Document Reference that links to the corresponding asset in the Media Library. Its _ref value follows the format media-library:{LIBRARY_ID}:{ASSET_DOCUMENT_ID}, where ASSET_DOCUMENT_ID is the _id of the sanity.asset document in the library (for example, 2ygmRi950252vmDzYfw3WPqhye3).

To resolve aspects in a non-authenticated query, the aspect definition has to be marked as public.

Use the Media Library reference

An alternative method to retrieve aspect data in your dataset queries is to supply the media reference to the documents::get GROQ function. This function lets you dereference Global Document References.

The following example dereferences the linked asset to retrieve the asset's url, then dereferences the media reference to obtain the aspects object:

The shape of the aspect data is dependent on your aspect schema.

List all aspects

Aspects are Sanity documents with a _type of sanity.asset.aspect. You can query them with GROQ using the Media Library's query endpoint:

Like other Sanity queries, you'll receive a response containing the original query, a result, sync tags, and the response time. Here's an example response for a single-field boolean aspect:

Query assets by aspect details

Any asset document in your library that has an assigned aspect includes those aspect details in an aspects property. You can query for specific aspect information using GROQ and the Media Library's query endpoint.

As an example, if you want to query all assets that have the placeholder aspect set to true, you can perform the following query:

This request returns any sanity.asset document with the placeholder aspect set to true.

Query the Media Library endpoint with POST instead of GET

You can also query with a POST request. Instead of using the ?query= parameter, set the body to your stringified query, the method to POST, and the Content-type to application/json. Here's the same query as a POST request:

Was this page helpful?