Tags: Sanity

Sanity and Slack integration with GROQ-powered webhooks

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.
Grafisk brukergrensesnitt, tekst, applikasjon
2. From the Features page, activate Incoming Webhooks.
Grafisk brukergrensesnitt, tekst, applikasjon
3. Click Add New Webhook to Workspace
Grafisk brukergrensesnitt, tekst, applikasjon
4. Pick a channel that the app will post to, then click Allow.
Grafisk brukergrensesnitt, tekst, applikasjon
5. Copy your Incoming Webhook URL and use it to post messages to Slack.
Grafisk brukergrensesnitt, tekst, applikasjon

Create a simple Slack webhook in Sanity

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

Grafisk brukergrensesnitt, tekst, applikasjon, e-post

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.
Grafisk brukergrensesnitt, tekst, applikasjon
When a new user joins, this is posted to Slack.
Grafisk brukergrensesnitt, tekst, applikasjon, e-post

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.
Grafisk brukergrensesnitt, program
When a user solves a task, and gets a higher score, this is posted to Slack.

Other usages

Other use cases include, but are not limited to:

With the free plan, you can create up to four webhooks.

That's it!