Skip to main content

Sanity Webhooks for Cache Invalidation

Set up automatic cache invalidation when content is updated in Sanity.

Setup

  1. Go to Sanity Manage
  2. Select your project
  3. Go to APIWebhooks
  4. Click Create webhook

Configuration

Configure the webhook with:

  • Name: Cache Invalidation
  • URL: https://your-domain.com/api/webhooks/sanity
  • Dataset: production (or your dataset)
  • Trigger on: Create, Update, Delete
  • HTTP method: POST
  • Secret: Set SANITY_WEBHOOK_SECRET in your env
  • HTTP Headers:
    Authorization: Bearer ${SANITY_WEBHOOK_SECRET}

Testing

Test the webhook manually:

curl -X POST https://your-domain.com/api/webhooks/sanity \
-H "Authorization: Bearer your-secret" \
-H "Content-Type: application/json" \
-d '{"_type": "hero"}'

How It Works

  1. Content is published in Sanity
  2. Sanity sends webhook to your API
  3. API validates the webhook secret
  4. Cache is automatically cleared
  5. Next request fetches fresh content

Next Steps