Testing a Webhook with Localhost Endpoint using ngrok

5 replies
Last updated: May 28, 2020
Does anyone know if it’s possible to test a Webhook with an endpoint running on localhost? I’m assuming that the sanity server is the one that actually calls the webhook so testing on localhost wouldn’t work?
AI Update

You're correct that Sanity's servers are the ones calling your webhook endpoint, so they can't directly reach localhost. However, there are definitely ways to test webhooks during development!

The most common approach is to use a tunneling service like ngrok or similar tools (Cloudflare Tunnel, LocalTunnel, etc.) that create a public URL that forwards requests to your local development server. The Sanity documentation on GROQ-powered webhooks mentions testing webhooks, and the community commonly uses this pattern.

Here's how it typically works:

  1. Start your local server (e.g., localhost:3000)
  2. Run ngrok: ngrok http 3000
  3. Copy the public HTTPS URL ngrok provides (e.g., https://abc123.ngrok.io)
  4. Use that URL as your webhook endpoint in Sanity's webhook configuration
  5. Requests from Sanity will now tunnel through to your localhost

Important security note: When testing webhooks, you'll want to verify the webhook signature to ensure requests are actually from Sanity. Sanity sends an X-Sanity-Signature header that you can use to verify the request authenticity using your webhook secret. This is especially important when using public tunnel URLs.

Alternative: Sanity Functions

If you're building automation or reacting to content changes, consider using Sanity Functions instead of webhooks. Functions are serverless compute that run within Sanity's infrastructure and have built-in local testing capabilities. They're event-driven, respond to content changes, and you can test them locally without any tunneling setup. Functions are generally the modern, recommended approach for content automation unless you specifically need to integrate with an external system that requires webhooks.

The GitHub repo for sanity-algolia even mentions this exact challenge: "Testing the webhook flow locally is slightly tricky as you can't point the webhook to localhost, as such you must set up a proxy to allow your local dev server..."

So yes, ngrok or similar tunneling tools are your friend for webhook testing!

Looks like localhost isn’t allowed as a hook url. Makes sense, but that means I have to have an endpoint already deployed before I can test
You could use ngrok to tunnel to your local endpoint
ahhhh great idea. Thanks!
For what it’s worth, I got a quick serverless function deployed to netlify and that worked as well but ngrok will be super useful for testing this giong forward

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.

Was this answer helpful?