Back to Blog
Development5 min read

Test Webhooks on Localhost With a URL That Never Changes

DT
DomainDock Team
Engineering ·

You've written the checkout. The payment goes through. Then nothing happens, because the part that marks the order as paid lives in a webhook handler and Stripe has no way to reach localhost:3000.

Every webhook provider has the same requirement: a public HTTPS URL it can POST to. Your laptop doesn't have one. Here's how to get one that's worth keeping. Plus the two bugs that eat most of the afternoon once you do.

Option 1: the Stripe CLI

For Stripe alone, start with their CLI. It's well made and free:

stripe listen --forward-to localhost:3000/webhooks/stripe

It prints a signing secret (whsec_...) for your handler and forwards events from your test account to your local app. In a second terminal, stripe trigger payment_intent.succeeded creates an event on demand.

Where it stops:

  • It's Stripe only. GitHub, Shopify, Slack, Twilio, Clerk and Resend each need their own answer. Most of them don't have a forwarding CLI.
  • It's not your real endpoint. The CLI isn't the webhook endpoint you set up in the Dashboard, with its chosen event types and its own signing secret. Bugs in that setup show up first in production.
  • It runs on one machine. A teammate or a phone testing the full flow can't use your stripe listen.

Option 2: a real endpoint on your laptop

The other way is to give your local app an actual address and register it with the provider like you would in production.

A tunnel does that. A small agent runs next to your app and connects out to a server with a public address. Requests to your address come down that connection to your app. Nothing is opened on your router.

The detail that matters for webhooks is the address itself. If it changes every time you restart the tunnel, you're back in the Stripe Dashboard every morning editing the endpoint URL. You want one address you set once:

  1. In DomainDock, click Add domain, pick a name like acme-hooks and choose On my computer.
  2. Run the command it gives you, with the port your app listens on:
frpc http -s tunnel.digitalweb.host -P 443 -p wss -l 3000 \
  -d acme-hooks.digitalweb.host -n acme-hooks.digitalweb.host \
  --metadatas token=<your-token>
  1. In Stripe's webhook settings, add an endpoint at https://acme-hooks.digitalweb.host/webhooks/stripe and select the events you handle.
  2. Copy that endpoint's signing secret into your local .env.

From now on, whenever the agent runs, Stripe delivers to your laptop. Close the laptop on Friday, open it on Monday, run the agent: same URL, same secret, nothing to update. Do the same for GitHub (repository Settings → Webhooks) and any other provider, all pointing at the same address with a different path.

Bug 1: the signature check fails

You'll see No signatures found matching the expected signature within the first ten minutes. It's almost never the secret.

Providers sign the exact bytes they sent. If your framework parses the JSON first and you verify JSON.stringify(req.body), the bytes differ (key order, spacing, unicode escapes) and the check fails. Read the raw body on the webhook route:

// Express: raw body on this route only, before any global express.json()
app.post('/webhooks/stripe', express.raw({ type: 'application/json' }), (req, res) => {
  let event;
  try {
    event = stripe.webhooks.constructEvent(
      req.body, // a Buffer, untouched
      req.headers['stripe-signature'],
      process.env.STRIPE_WEBHOOK_SECRET,
    );
  } catch (err) {
    return res.status(400).send(`Webhook error: ${err.message}`);
  }
  res.sendStatus(200);
  queue.add(event); // do the slow work after answering
});

GitHub works the same way with a different header. It sends X-Hub-Signature-256, an HMAC-SHA256 of the raw body using the secret you set, prefixed with sha256=. Compare it with a constant-time comparison, not ===.

If the body is raw and it still fails, check which secret you're using. Every Stripe endpoint has its own. The CLI's secret is different from your Dashboard endpoint's.

Bug 2: the same event arrives twice

Providers retry when they don't get a 2xx answer quickly. A handler that sends an email and then times out on a slow database call gets the event again. The customer gets two emails.

Two habits fix it:

  • Answer first, work later. Return 200 as soon as the signature checks out, then process the event in a queue or background job.
  • Make handlers idempotent. Store each event's ID (evt_... for Stripe, the X-GitHub-Delivery header for GitHub) and skip any you've already handled.

A tunnel is a good place to find out whether your handler gets this right, because you can stop your app mid-delivery and watch the retry arrive. In the provider's delivery log you'll see each attempt with its status code. GitHub also has a Redeliver button for any past delivery.

Keep the endpoint open, the rest locked

If you password-protect your tunnel for a client demo, webhooks stop: Stripe can't type a password. Don't punch holes in the lock. Use two addresses: acme-demo with a password for people and acme-hooks without one for providers, each with its own tunnel. The webhook address is protected by signature checks instead, which is what providers expect.

A tunnelled dev server is reachable by anyone who finds the address while the agent runs. Webhook routes with signature checks are fine to expose. Debug pages and admin routes on the same app are a different story, so keep them off or behind the password address.

When you go to production

Deploy, then change the target of a production domain to your server. Your staging and production endpoints stay separate in Stripe, each with its own secret. The local one keeps working for the next feature.

The tunnel guide covers the Docker version of the agent and what visitors see while it's stopped. If you're weighing tunnel tools, ngrok vs Cloudflare Tunnel vs DomainDock compares them.

Frequently asked questions

Can Stripe send webhooks to localhost?

Not directly. Stripe's servers can't reach localhost on your machine. Either run the Stripe CLI (it forwards events to your local app) or give your app a public HTTPS address with a tunnel and add that address as a webhook endpoint.

Do I need the Stripe CLI if I use a tunnel?

No. With a tunnel your local app is a real endpoint and Stripe delivers to it like it would in production. The CLI is still handy for `stripe trigger`, which creates test events on demand.

Why does my webhook signature check fail locally?

Almost always because the body was parsed before the check. Signatures are computed over the exact raw bytes, so read the raw body on the webhook route. The other common cause is using the signing secret of a different endpoint.

Can I password-protect a webhook URL?

Webhook providers can't type a password, so a password-protected address will reject them. Give the webhook endpoint its own address without a password and verify the provider's signature in your handler instead.

webhooksstripegithublocalhosttunnel
Newsletter

Developer infrastructure, in your inbox

New guides on domains, SSL certificates and deploying to your own server. A couple of emails a month at most.

No spam. Every email has a one-click unsubscribe.