GROQ webhooks in Sanity

Saturday, May 6, 2023

The webhooks within Sanity CMS offer an amazingly powerful way to send data to 3rd parties when changes to content occur. However, the webhooks often require extra data retrieval to ensure the consuming service has all the necessary information for appropriate action. Sanity comes with a number of built-in functions, and can utilise most existing GROQ abilities out of the box.

This additional data could include the nature of the operation conducted, like CRUD operations (Create, Read, Update, Delete), which can be fetched using the delta::operation() function.

In some scenarios, you might need specific properties such as _id or _type. These can be accessed using standard GROQ queries, ensuring you have the specific data you need.

Additionally, Sanity CMS offers unique functions to fetch the data pre or post-operation. The before() and after() functions can take a set of required properties as parameters, for instance, before() { title, slug }. In this context, you can also trace references using the standard arrow notation, like tags[] -> { name }.

By leveraging these functions and queries, you can enhance the functionality of your webhooks, providing a more detailed, responsive, and effective system.

2  //  META
3  "revisionId": _rev,
4  "operation": delta::operation(), // CRUD
5  // Properties
6  _id,
7  _type,  
8  // Data from before/after the operation  
9  "before": before() {
10     ..., // All fields
11     tags[]->{ // Follow referenced field 
12       "slug": slug.current, // and retrieve a specific key
13     },
14  },
15  "after": after() {
16     ...,
17     tags[]->{
18       "slug": slug.current,
19     },
20  },

Other posts

Tagged with: