🔮 Sanity Create is here. Writing is reinvented. Try now, no developer setup

Webhooks don't indicate document creation, update or deletion. Possible solution found.

4 replies
Last updated: Jan 3, 2023
Hi all. It appears that the webhooks don't provide any information (with the payload or headers) on whether a document was created, updated or deleted, which seems quite odd because you can limit the entire webhook based on this. Is this correct?
Jan 3, 2023, 11:02 AM
I found this as a solution - which I think would be useful to share in the Webhooks documentation:
{
  "transactionId": _rev,
  "projectId": sanity::projectId(),
  "dataset": sanity::dataset(),
  "ids": {
    "created": [select(before() == null && after() != null => { _id, _type })],
    "deleted": [select(before() != null && after() == null => { _id, _type })],
    "updated": [select(before() != null && after() != null => { _id, _type })],
    "all": [{ _id, _type }]
  }
}
Adapted from:
https://www.sanity.io/manage/webhooks/share?name=Legacy+webhook&description=Recreat[…]thod=POST&apiVersion=v2021-03-25&includeDrafts=&headers=%7B%7D (via https://www.sanity.io/plugins/sanity-algolia )
Jan 3, 2023, 11:11 AM
Thanks for sharing your solution
Jan 3, 2023, 6:49 PM
Turns out that this projection mimics the older hooks payload format. I don’t think there will be more than a single document passed in the new GROQ based webhooks, so it still feels a bit awkward to jump through hoops like that. It would be better to have a custom HTTP header that passes the operation (create/update/delete) along. Or maybe a GROQ function to use within the projection.
Jan 3, 2023, 7:05 PM
Yeah, that is quite verbose and using a header would be much easier.
Jan 3, 2023, 7:48 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?