Media Library

Link assets to documents

Linking an asset in the Media Library to a document in your project requires a few extra steps.

When editors use Sanity Studio to add assets to documents, the studio and Media Library work together to link the project, document, and library. To do this programmatically, like when migrating a large number of assets to your library, you'll need to perform the steps manually.

Rendering assets

In this guide, you'll learn how to link Media Library assets to documents in a project dataset. There are three key steps:

  • Upload the asset to Media Library, or query an existing asset in Media Library to obtain the necessary IDs.
  • Link the Media Library asset to your project dataset.
  • Patch any documents that use the asset with the correct references.

Prerequisites:

  • An environment where you can make HTTP requests. The code examples use fetch in Node.js, which was made stable in v21.0. You can substitute any request library and any language.
  • Project and library identifiers:
    • mediaLibraryId from your Media Library.
    • projectId for the project you want to link the assets to.
    • dataset for the project you want to link the assets to.
  • A personal authentication token with permission to read/write from both the Media Library and your project. The examples read the token from the SANITY_API_TOKEN environment variable.
  • An API version of 2025-02-19 or later. Global document references, which link Media Library assets to documents in a dataset, aren't supported on earlier versions. The examples in this guide use v2025-02-19 in their request URLs.

Gather the asset IDs

Linking a Media Library asset requires two identifiers:

  • Asset ID: The primary identifier (_id) for an asset document in your Media Library.
  • Asset instance ID: The identifier for a versioned instance of the asset. An asset can have several versions. currentVersion points to the active instance.

There are two ways to obtain these values.

Get IDs during upload

After uploading an asset with the media-libraries/upload endpoint, the response contains asset._id and assetInstance._id. For details on using the API to upload assets, see the following guide.

Get IDs by querying assets

If your assets are already in Media Library, you can query the Media Library API for asset documents. If you already know the Asset ID, you can query it directly with the _id == '<asset-id>' GROQ filter. If you don't know the ID(s), you can query all assets with _type == 'sanity.asset'.

For example, this query returns all asset documents in the library:

In the example response JSON, the highlighted lines show the IDs you need.

  • Asset ID: The _id of the document.
  • Asset instance ID: The currentVersion._ref.

Link the asset to your project dataset

The next step is to link the asset to your project dataset with the assets/media-library-link API. This creates a local reference point that you can use throughout the dataset. Note the _id and the media._ref in the response for the next step.

If you link an asset immediately after uploading it, the request can fail with status 422 and the message Media library asset is not ready while Media Library finishes processing the asset. Wait a few seconds, then retry the request.

Patch the documents

Now that the asset is linked to your project and dataset, you can attach it to documents with a document mutation.

Use the document ID and the media reference from the previous step to attach the asset to documents in your dataset. As with local assets, you can patch linked Media Library assets to a document. The key difference is that you need to update the asset and media objects.

This example adds the asset to a poster field on the target document. Replace the path to the asset and media to match the shape of your content.

Writes that add the reference need the same API version

You captured the media._ref in the previous step, but you can also build it by combining the resource type (media-library) with your library ID and the asset ID, separated by colons: media-library:MEDIA_LIBRARY_ID:ASSET_ID.

These same steps apply for linking Media Library assets to any documents.

Was this page helpful?