Unlock seamless workflows and faster delivery with our latest releases – get the details
Last updated November 21, 2021

GROQ-powered webhooks - Send messages to Slack

By Tomas Hensrud Gulla

The newly released GROQ-powered webhooks unlock some interesting new possibilities. You can send customized HTTP requests when content is created, updated, or deleted. Use the query language GROQ to define which documents should trigger a webhook and what its payload should be.

Before the release of GROQ-powered webhooks you could also integrate Sanity with Slack, but then you needed some kind of logic in between. With GROQ, it's easier than ever!

Set up incoming webhook for Slack

While working on a recent project I wanted to post some information to Slack, to keep track of its status. Step one is setting up the incoming webhook.

1. Create a new Slack app in the workspace where you want to post messages.

2. From the Features page, activate Incoming Webhooks.

3. Click Add New Webhook to Workspace.

4. Pick a channel that the app will post to, then click Allow.

5. Copy your Incoming Webhook URL and use it to post messages to Slack.

Create a simple Slack webhook in Sanity

Log in to sanity.io, select your project and create your webhook on the API tab.

Paste the webhook URL from Slack, then select what actions (create/update/delete) to trigger on.

My first webhook is very simple. It will trigger when a new document of type user is created, and it will post a message with the user's name and its referenced team.

When a new user joins, this is posted to Slack.

Create another Slack webhook in Sanity

I want to trigger another webhook when a user's score has changed. Using the before() and after() delta functions I can make the webhook trigger only when the points have increased, and not when there is any other change.

When a user solves a task, and gets a higher score, this is posted to Slack.

That's it!

Sanity – build remarkable experiences at scale

Sanity Composable Content Cloud is the headless CMS that gives you (and your team) a content backend to drive websites and applications with modern tooling. It offers a real-time editing environment for content creators that’s easy to configure but designed to be customized with JavaScript and React when needed. With the hosted document store, you query content freely and easily integrate with any framework or data source to distribute and enrich content.

Sanity scales from weekend projects to enterprise needs and is used by companies like Puma, AT&T, Burger King, Tata, and Figma.

Other guides by author

Bulk deletion using Sanity CLI

Using Sanity Studio, you can delete documents one by one. Learn how to perform mass deletion using Sanity CLI.

Tomas Hensrud Gulla
Go to Bulk deletion using Sanity CLI