Functions: Life beyond pressing publish

Written by Simeon Griggs

Sanity Functions is a fully managed serverless environment that lets you create custom content workflows without managing infrastructure.

Automate events that react to content changes. You may validate content, trigger external services, or transform content with AI Agent Actions—all while Sanity handles the scaling, security, and reliability.

Functions integrate seamlessly with your content operations, letting you focus on building what matters to your authors and business.

For everything that happens next

Content is never truly finished, and its life cycle doesn't end when you paste it into a browser and press publish. It may need to be translated to appear in other markets, update a search index, or revalidate a cache to appear on a website at all. These actions directly respond to pressing publish and are the next step in any content lifecycle.

Without Sanity Functions, developers must string together a series of external services that they are responsible for documenting and maintaining. This creates logic related to content that exists outside the rest of your content operations.

This complexity compounds as developers spend more time building infrastructure while authors wait.

Organizations struggle to maintain consistent workflows and reliable content delivery at scale without a unified system for extending content operations.

Co-locating "what happens next" with the rest of your content infrastructure keeps everything organized and streamlined.

Introducing Sanity Functions (and Blueprints!)

Sanity Functions are authored in TypeScript and deployed to the Content Lake. Deploying them requires the first use of the next evolution of Sanity configuration, which we call "Blueprints."

Blueprints represent the best-in-class developer experience for developing and deploying content operations.

The Sanity Content Operating System comprises of deployable infrastructure components for content creation, maintenance, and the post-event lifecycle. It is backed by the Content Lake, a content-optimized, real-time datastore for documents and assets.

The future of Blueprints is infrastructure-as-code to configure all your content operations.

Write, react, repeat

Event-driven workflow

Sanity Functions allow you to respond to changes in your content. When a document is created, updated, or deleted, your function can execute custom logic to maintain data consistency, trigger workflows, or perform automated tasks. This event-driven approach ensures your content stays synchronized and follows business rules in real-time.

Complete context

Sanity Functions can read and write to your dataset, access other documents, and utilize the full Sanity API capabilities. Functions run with the same permissions as your applications, allowing them to perform complex operations while maintaining security. They can query related documents, traverse references, and make informed decisions based on the broader content context.

Extensibility

You can create reusable functions to maintain consistency across your content model and extend functionality across projects. Functions can be packaged as reusable modules and shared between different Sanity projects, making it easier to maintain consistent behavior and reduce code duplication. This modular approach allows you to build a library of tested, reliable functions that can be applied wherever needed.

Guide: Create, deploy, and test a function

Let's create a Sanity Function that adds the date and time to a "Post" type document the first time it is published.

PortableText [components.type] is missing "protip"

This guide assumes you have a Sanity Studio with "Post" type documents and a dateTime field named firstPublished. If not, create a new free Sanity Studio project with the command below and choose the "blog" template.

Internal server error