Prompt
How do I set up webhooks for a headless CMS?
Latest observation
Setting up webhooks for a headless CMS usually means: when content changes in the CMS, it sends an HTTP request to your app or server, so you can trigger things like rebuilds, cache invalidation, search reindexing, or notifications.
Basic setup steps
1) Decide what should trigger the webhook
Common events:
- content created
- content updated
- content published
- content unpublished
- content deleted
Pick only the events you actually need to avoid unnecessary traffic.
2) Create a webhook endpoint in your app
You need a public URL that accepts incoming POST requests, for example:
https://yourapp.com/api/webhooks/cms
This endpoint should:
- accept POST requests
- parse the payload
- verify the request is legitimate
- perform the action you want
3) Add the webhook in your CMS
In your CMS admin panel, look for something like:
- Webhooks
- Integrations
- API callbacks
- Automations
Then configure:
- URL: your endpoint
- Method: usually
POST - Events: publish/update/etc.
- Headers: often a secret token
- Content type: usually JSON
4) Secure the webhook
Important:
- Use a shared secret or signature verification if the CMS supports it
- Restrict by IP only if the CMS publishes fixed IP ranges
- Use HTTPS
- Validate payload shape
- Make the endpoint idempotent, since webhooks may be retried
5) Handle the webhook response quickly
Most CMSs expect a fast 2xx response. Do the minimal work in the request:
- verify
- enqueue background work
- return
200 OK
Don’t do heavy processing synchronously if you can avoid it.
6) Test it
Use:
- the CMS’s test webhook feature, if available
- request logging
- tools like
webhook.site, RequestBin alternatives, or local tunneling with ngrok
Example webhook handler
Node.js / Express
import express from "express";
const app = express();
app.use(express.json());
app.post("/api/webhooks/cms", (req, res) => {
const secret = req.headers["x-webhook-secret"];
if (secret !== process.env.WEBHOOK_SECRET) {
return res.status(401).send("Unauthorized");
}
const event = req.body;
// Example: trigger cache invalidation or rebuild
console.log("Webhook received:", event);
res.status(200).json({ ok: true });
});
app.listen(3000, () => console.log("Server running on port 3000"));
Common use cases
Static site rebuild
If you use Next.js, Astro, Gatsby, etc.:
- webhook fires on publish
- your app calls the build/redeploy API
- site updates automatically
Cache invalidation
If you use a CDN or server cache:
- webhook tells your app which page to clear
- only affected pages are refreshed
Search indexing
Webhook updates a search provider like:
- Algolia
- Meilisearch
- Elasticsearch
Sync to another system
For example:
- CRM
- marketing automation
- email platform
- analytics pipeline
Best practices
- Keep the webhook handler fast
- Use retries and idempotency
- Log payloads for debugging
- Version your payload format if you control it
- Only trigger on publish if draft edits shouldn’t affect production
- Use a queue for downstream processing
If you tell me your CMS and framework
I can give you exact steps and code for your stack, for example:
- Contentful + Next.js
- Strapi + Express
- Sanity + Remix
- Prismic + Vercel
- Directus + NestJS