Media Library

Assign an aspect to an asset

Assign 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, or an organization-wide robot token with read/write access to 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 only the aspect change to the asset document.

Set aspects with the Sanity CLI

Use the media import CLI command to set aspects on a single asset or a set of assets. For more information, see importing assets. If the asset already has a value for that aspect, media import skips it. Pass --replace-aspects to overwrite existing aspect data.

Pro tip

Set aspects with the HTTP API

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

This example patches the value of a single-field aspect named comment.

This modifies the asset document to look something like this:

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

Set aspects with the @sanity/client library

Configure your client with a Media Library resource, then use client.patch() to assign aspect values to an asset:

Was this page helpful?