πŸ‘€ Our most exciting product launch yet πŸš€ Join us May 8th for Sanity Connect

Hooking into document content changes and running custom logic in Sanity.io

7 replies
Last updated: Mar 30, 2022
Hey, does anyone know if there is a way to hook into document content changes and run custom logic before/after they happen? For example, if a boolean field is changed on one document, I'd like to detect that change in a hook that allows me to automate the creation of another type of document?
Mar 30, 2022, 12:26 PM
Hey User. Yes it is! We have two options for achieving this:
1.
Custom document actions allow you to override the default Userdio actions (such as "publish"). This allows you to run any code you require in response to document changes made in the Userdio.2.
GROQ-powered webhooks can notify a web service when documents are created, updated, or deleted. This allows you to run any code you require in response to document changes made in the Userdio or the API.Both of these options will allow you to create a new document in response to the creation/update of a source document.
Mar 30, 2022, 12:38 PM
Thanks User, I've been playing around with document actions for modifying the document currently being edited, but not seen how to create a new document - would you mind pointing me in the direction of what you would do to create a new doc?
Mar 30, 2022, 12:51 PM
No problem! You can do this using the Sanity client, which allows you to interact with any document.
First, import the client:


import sanityClient from 'part:@sanity/base/client'
Then set the API version:


const client = sanityClient.withConfig({ apiVersion: '2022-03-30' })
Now you can use the client to perform actions.
Here are the docs for creating documents using the client .
Mar 30, 2022, 1:03 PM
Thanks so much for your help! I also need to react to document changes made via the API as well as the studio, so as you suggest I will use webhooks. However, do you know if there is a potential feature down the line to have hooks similar to webhooks, but where we can fire some custom logic locally within our studio code? Similar to strapi lifecycle hooks if you are familiar with those!
Mar 30, 2022, 1:12 PM
As you noted, you need a server that is always available to respond to changes. Sanity Userdio is purely a client-side app, so unfortunately there is no server running to respond to changes. Sanity's GROQ-powered webhooks and Strapi's Server Hooks are roughly analogous, with the only difference being that Sanity doesn't provide any abstraction for handling webhooks (although we do have a tool for verifying webhook requests ).
We've found Vercel to be a great option for bundling your Userdio and webhook handlers together in the same project. If you deploy to Vercel,
you can create an `api` directory inside your Userdio project to contain your webhook handlers .
Mar 30, 2022, 1:29 PM
Ah of course, I hadn't thought about studio being client-side only! Thanks again User
Mar 30, 2022, 1:31 PM
You're welcome πŸ™‚. Let us know if we can help with anything else!
Mar 30, 2022, 1:32 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?