Sanity Webhook to GitHub Actions: Fix 422 Error with event_type Parameter
{
id: "atm-xxxxx",
projectId: "xxx",
inProgress: false,
duration: 226,
createdAt: "2022-02-xxxxx",
updatedAt: "2022-02-xxxx",
messageId: "msg-xxxxxxxxxx",
hookId: "xxxxxxxxx",
isFailure: true,
failureReason: "other",
resultCode: 422,
resultBody: "{"message":"Invalid request.\n\n\"_createdAt\", \"_id\", \"_rev\", \"_type\", \"_updatedAt\", \"header\", \"slug\", \"title\" are not permitted keys.\n\"event_type\" wasn't supplied.","documentation_url":"<https://docs.github.com/rest/reference/repos#create-a-repository-dispatch-event>"}"
}GitHub returns a 422 because a GROQ-powered webhook with an empty projection sends the whole document (_id, _type, _rev, title and so on) as the request body. GitHub's repository dispatch endpoint only accepts event_type (required) and an optional client_payload object. To fix it, set the webhook's Projection so the body has exactly that shape.
Configure the webhook
Webhooks live under API → Webhooks in your project settings at sanity.io/manage. You can also manage them with the CLI or the project APIs (webhooks docs).
- URL:
https://api.github.com/repos/{owner}/{repo}/dispatches - Trigger on: create, update and/or delete
- Filter: for example
_type == "post". If you leave it empty, every document triggers the webhook. - HTTP method: POST
- HTTP headers:
Authorization: Bearer <your GitHub token>, plus any other headers GitHub's docs require. Check GitHub's repository dispatch documentation for which token permissions the call needs. - Projection:
{
"event_type": "sanity-content-changed",
"client_payload": {
"documentId": _id,
"documentType": _type,
"slug": slug.current,
"title": title,
"dataset": sanity::dataset()
}
}The projection is the part that normally comes after the filter in a GROQ query. Its result becomes the JSON body of the request (Intro to Projections). You can also follow references ("authorName": author->name), use before()/after() to see what changed, or send everything with "client_payload": { ... }. Sub-queries such as *[...] are not supported in webhook projections.
Some details that matter here:
- The body is always sent as JSON (
content-type: application/json). - Any 400-range response, including this 422, is treated as undeliverable and not retried. Fix the projection, then trigger the webhook again.
- To see GitHub's response for each delivery, open the webhook's three-dot menu and check the attempts log.
- Shared webhook URLs include your header settings. Remove the token before you share one.
Use it in GitHub Actions
on:
repository_dispatch:
types: [sanity-content-changed]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Use payload data
run: |
echo "Document ID: ${{ github.event.client_payload.documentId }}"
echo "Title: ${{ github.event.client_payload.title }}"The types value has to match the event_type in your projection.
Alternative: a Sanity Function
If you need conditional logic or error handling, a Sanity Function can make the GitHub call instead. Functions run on Sanity's infrastructure when documents change. Set one up with npx sanity@latest blueprints init and npx sanity@latest functions add (quickstart), then declare it like this:
// sanity.blueprint.ts
import {defineBlueprint, defineDocumentFunction} from '@sanity/blueprints'
export default defineBlueprint({
resources: [
defineDocumentFunction({
name: 'trigger-github',
event: {
on: ['create', 'update'],
filter: '_type == "post"',
projection: '{_id, _type, title, "slug": slug.current}',
},
}),
],
})// functions/trigger-github/index.ts
import {documentEventHandler} from '@sanity/functions'
export const handler = documentEventHandler(async ({event}) => {
const res = await fetch('https://api.github.com/repos/OWNER/REPO/dispatches', {
method: 'POST',
headers: {
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
event_type: 'sanity-content-changed',
client_payload: event.data,
}),
})
if (!res.ok) console.error('GitHub dispatch failed', res.status, await res.text())
})Deploy with npx sanity blueprints deploy. Then store the token with npx sanity@latest functions env add trigger-github GITHUB_TOKEN <token>. The function has to be deployed before this command works. Variables added this way aren't available when you test locally, so set them on the test command instead: GITHUB_TOKEN="..." npx sanity@latest functions test trigger-github (environment variables). Read the logs with npx sanity functions logs trigger-github.
Show original thread6 replies
Was this answer helpful?
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.