Compute and AI

Functions

Learn how to take advantage of Functions in your Sanity projects.

Functions enable you to execute custom logic whenever changes occur in your content—all without requiring your own infrastructure.

With Functions, you can:

  • Enrich, validate, and constrain your content in new ways.
  • Create complex workflows.
  • Connect changes in your content to external applications and services. Refresh a CDN cache, trigger social posts, update inventory, and more.

Requirements

  • Functions run on Node.js v22.x. We encourage you to use the same version in local testing to avoid unsupported features or syntax changes.
  • sanity CLI v3.88.1 or higher is required to interact with Blueprints and Functions. You can always run the latest CLI commands with npx sanity@latest.

Core concepts

Functions

Functions are small, single-purpose pieces of code that run on Sanity's cloud infrastructure. They act on changes in your content and allow you to extend the capabilities of your existing content management workflows. Functions are linked to projects, but not datasets. This means the same functions will run across all of your datasets.

When changes in your content trigger a function, they pass along details about the document. You can further refine this with a GROQ projection, ensuring your function has the data it needs.

Functions can access the full range of Sanity's APIs so you can interact with all of your content, not just the document that triggered the change.

All function code resides in a dedicated directory for each individual function.

To keep functions slim and organized, treat them like small projects. Add packages directly to each function's directory.

Blueprints

A function on its own doesn't know much about the larger Sanity ecosystem. That's where Blueprints come in. A blueprint is a template that describes Sanity resources. For Functions, blueprints describe when and where your function should trigger.

Event-driven workflow

Functions work on an event system. They react to changes in your data. Did an editor publish a new document? Run a function. New comments on a draft? Run a function.

Limited events at launch

Testing and logging

Your development process might need some adjustments to work with functions. As they run remotely on Sanity's infrastructure, you'll rely on local test commands and checking the logs from the CLI to debug your function logic.

Limitations

Max function size

Limit: 200MB

Although your individual function's TypeScript or JavaScript code may appear small, it can rapidly expand in size when packages are included.

We strongly suggest keeping your functions small. The larger the function, the slower it is. You can limit the size, and therefore increase the execution speed by:

  • Limit dependency usage to only what's necessary.
  • Choose performant, slim libraries.
  • If you must use large libraries, consider bundling or tree-shaking in advance.

If your functions require too many dependencies, it may help to narrow their purpose and split the logic into multiple functions.

Max function execution time

Functions default to a max execution time of 10 seconds. In the Blueprint configuration, this can be configured from 1 to 900 seconds.

No robot token support for deployment

At this time, you should only deploy Blueprints with personal authentication tokens. You can still use robot tokens inside a function

Was this page helpful?