Media Library

Add an aspect to an asset

Add aspects to an asset programmatically.

This guide explores options for programmatically assigning aspects to Media Library assets.

Prerequisites:

  • mediaLibraryId: The ID for your organization's Media Library.
  • Read/write access to documents in Media Library.
  • A personal authentication token. At this time, robot tokens are not supported for Media Library.

Mutate the asset

To add an aspect to an asset, you need to mutate the asset in Media Library. Rather than capture and rewrite the whole asset, use a patch to apply just the change in aspect to the asset document.

Option 1: use the CLI

The media import CLI command can be used to set aspects on a single asset or set of assets. More details are available in our guide on importing assets.

Protip

Option 2: use the HTTP API

Use the media-libraries/<media-library-id>/mutate endpoint to apply the mutation.

In this example, we patch the value of a single field aspect with a name of comment.

This modifies the asset document to look something like this:

For nested fields or more complex aspects, start with the outer-most name and work down to the individual field level.

Option 3: use @sanity/client

We're actively working to make interacting with Media Library form the Sanity JavaScript client happen. This section will be updated once client support is available.

Was this page helpful?