Functions: New Function Type: Media Library Asset Functions

We've added a new Sanity Function type that lets you respond to changes in your media library. This update brings the same event-driven capabilities from document functions to your media library assets.

✨ Highlights

React to Media Library asset events

You can now run functions whenever assets are uploaded, updated, or deleted in your media library. This new asset function type extends the event-driven architecture already available for documents, giving you more control over your content workflow automation.

Use this new function type to:

Know when new versions of an asset are created.

Clean up dependencies when assets are deleted.

Trigger notifications or webhooks for asset events.

Here's an example of defining a Media Library asset function:

import { defineBlueprint, defineMediaLibraryAssetFunction } from '@sanity/blueprints'

export default defineBlueprint({
resources: [
defineMediaLibraryAssetFunction({
name: 'ml-asset',
event: {
on: ['update'],
filter: "delta::changedAny(title)",
projection: "{_id, title, assetType}",
resource: {
type: 'media-library',
id: 'mlFqEeKZYecz',
}
},
})
],
})

This function type specifically targets documents with a _type of sanity.asset. You can apply additional filters, as shown in the example above, but only sanity.asset documents will invoke the function.

More Media Library functions will come in the future to accommodate more document types.

To get started, update to the latest @sanity/blueprints and @sanity/functions libraries using npm or pnpm, and ensure you're using the latest sanity CLI (v4.14.0). ML support in functions requires @sanity/blueprints v0.4.0 or later and @sanity/functions v1.1.0 or later.

For more information about Functions, see the Sanity Functions documentation.

Note: This functionality requires that your plan includes access to Media Library.