APIs and SDKs

Working with assets in @sanity/client

Learn how to upload, query, and delete assets in both Content Lake and Media Library using the Sanity JavaScript client.

The @sanity/client library provides methods for uploading, querying, and deleting assets in your Sanity project. This guide covers working with assets in Content Lake and introduces Media Library for video workflows.

Before you begin

Uploading assets to Content Lake

The client.assets.upload() method handles uploads to Content Lake. It accepts a type ('image' or 'file'), a body (file stream, buffer, Blob, or File object), and an optional configuration object. The method returns a promise that resolves to the created asset document.

Uploading from Node.js

In Node.js environments (build scripts, migrations, server-side processing), you can upload from file streams or buffers.

After uploading, you typically reference the asset from a document. Use patch() to set the asset reference on an existing document. For Media Library assets, see Link assets to documents.

Uploading from the browser

In browser environments, pass a File or Blob object to the upload method.

Specifying image metadata to extract

When uploading images, use the extract option to control which metadata is processed during upload. Available values include:

  • palette: Extracts dominant colors from the image.
  • location: Extracts GPS coordinates from EXIF data, if available.
  • exif: Extracts EXIF metadata like camera settings and timestamps.
  • blurhash: Generates a compact placeholder representation.

Querying assets

Assets in Content Lake are documents with the types sanity.imageAsset and sanity.fileAsset. You can query them with GROQ like any other document. For rendering images on your front end, see Presenting images. You can also manage assets through the Assets HTTP API.

Deleting assets from Content Lake

Delete an asset by passing its document ID to client.delete(). This permanently removes the asset and its associated data.

References are not removed automatically

Working with Media Library

Media Library is a specialized storage and delivery system for managing assets across your organization. It provides features like adaptive streaming, automatic transcoding, and thumbnail generation for video content. Unlike Content Lake assets, Media Library assets are stored separately and managed through a dedicated API.

This is a paid feature

This feature is available as an addon for certain Enterprise plans. Talk to sales to learn more.

Configuring the client for Media Library

To query or interact with Media Library from @sanity/client, configure the client with a resource property pointing to your Media Library instance.

Or, if you have a configured client and want to use the same configuration, use withConfig.

You can find your Media Library ID in the Sanity management console under your organization settings. See Configure your library for setup details.

Querying Media Library assets

With a Media Library-configured client, you can query assets using GROQ.

Uploading to Media Library

Upload assets to Media Library using the client.assets.upload() method on a Media Library-configured client.

Deleting Media Library assets

Media Library uses the same mutation API as Content Lake for deletions. To ensure both the published and draft versions are removed, use a transaction.

Getting video playback information

For video assets, use client.mediaLibrary.video.getPlaybackInfo() to retrieve playback URLs, thumbnails, and metadata like duration and aspect ratio. See Working with video for a complete guide to video delivery.

Was this page helpful?