Test Webhooks Locally with Cloudflare Tunnels

by Fahim Mahmud Chisti

You want to test a webhook from Stripe or GitHub, but your development server runs on localhost, which the provider cannot reach. This tutorial shows how to test webhooks locally with Cloudflare Tunnels: first a free throwaway public URL in one command, then a permanent hostname on your own domain that points at your laptop.

Quick answer: Install cloudflared, start your local server, and run cloudflared tunnel --url http://localhost:3000. It prints a public trycloudflare.com address that forwards to your machine; paste that URL plus your route into the provider’s webhook settings. For a URL that never changes, create a named tunnel on a domain in your Cloudflare account.

  1. Start a local webhook listener, for example an Express app on port 3000.
  2. Install cloudflared with Homebrew, apt or the Windows download.
  3. Run cloudflared tunnel --url http://localhost:3000 and copy the URL it prints.
  4. Send a test POST to that URL with curl, then register it with your provider.
  5. For a fixed hostname: tunnel login, tunnel create, config.yml, tunnel route dns, tunnel run.
In this guide
  1. Before You Start
  2. Why Cloudflare Tunnels Instead of Ngrok
  3. Step 1: Start a Local Webhook Listener
  4. Step 2: Install cloudflared
  5. Step 3: Open a Quick Tunnel to Localhost
  6. Test It: Send a Webhook Through the Tunnel
  7. Step 4: Set Up a Permanent Hostname on Your Domain
  8. The Gotcha: Body Parsers Break Signature Checks
  9. If It Still Doesn’t Work
  10. Frequently Asked Questions
  11. Next Steps
Terminal screen running a Cloudflare Tunnel routing webhook requests to a local Express server.

Before You Start

  • Node.js 18 or newer for the sample listener (any local server works).
  • Permission to install software on your machine (Homebrew, sudo for apt, or a Windows executable).
  • For the permanent hostname only: a free Cloudflare account and a domain whose nameservers point to Cloudflare. Quick tunnels need neither.
  • About 15 minutes for the quick tunnel, plus a few more for the named tunnel. Anyone who knows the public URL can reach your local app while the tunnel runs, so stop it when you finish.

Why Cloudflare Tunnels Instead of Ngrok

For years Ngrok was the default tool for local webhook debugging. Its free plan now includes one auto-assigned dev domain that survives restarts, but you cannot choose the hostname, and the plan has monthly request and data-transfer caps. A webhook URL you control saves you from updating your endpoint in several provider dashboards.

Cloudflare Tunnel covers both needs. A small program, cloudflared, runs on your machine and opens an outbound-only connection to Cloudflare’s network, so you do not open any ports on your router. A quick tunnel gives you a free, throwaway public URL. If you have a domain on Cloudflare, you can also give your local machine a permanent subdomain such as dev-webhook.yourdomain.com at no cost.

Step 1: Start a Local Webhook Listener

Before you route traffic in from the internet, you need a local server to receive it. This small app uses the Express.js framework to log every incoming webhook.

Create a new folder, initialise the project and install Express:

mkdir local-webhook-tester
cd local-webhook-tester
npm init -y
npm install express

Next, create an index.js file. It adds a POST endpoint that logs the headers and body of each request, so you can confirm the tunnel works:

const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.post('/webhook', (req, res) => {
  console.log('--- RECEIVED WEBHOOK ---');
  console.log('Headers:', JSON.stringify(req.headers, null, 2));
  console.log('Payload:', JSON.stringify(req.body, null, 2));
  res.status(200).json({ received: true });
});
app.listen(PORT, () => {
  console.log(`Local webhook listener running on port ${PORT}`);
});

Start the server with node index.js. It prints Local webhook listener running on port 3000 and waits for requests. Leave this terminal open.

Step 2: Install cloudflared

The cloudflared command-line tool creates and maintains the tunnel. Install it in a new terminal window.

On macOS, use Homebrew:

brew install cloudflared

On Ubuntu or Debian, run these commands to add Cloudflare’s package repository and install the package:

sudo mkdir -p --mode=0755 /usr/share/keyrings
curl -fsSL https://pkg.cloudflare.com/cloudflare-main.gpg | sudo tee /usr/share/keyrings/cloudflare-main.gpg >/dev/null
echo 'deb [signed-by=/usr/share/keyrings/cloudflare-main.gpg] https://pkg.cloudflare.com/cloudflared any main' | sudo tee /etc/apt/sources.list.d/cloudflared.list
sudo apt-get update && sudo apt-get install cloudflared

On Windows or other systems, download the latest executable from the cloudflared download page and add it to your PATH. Check the install with cloudflared --version, which prints the installed version.

Step 3: Open a Quick Tunnel to Localhost

If you just want a public URL for a testing session, use a quick tunnel. It needs no account and no domain. Run this in the second terminal:

cloudflared tunnel --url http://localhost:3000

The command prints several log lines while it connects to Cloudflare. Among them is a box that contains your public address, a random set of words ending in .trycloudflare.com. Copy it. The address can take a few seconds to start working.

Test It: Send a Webhook Through the Tunnel

In a third terminal, send a test POST to the tunnel URL with /webhook on the end. Replace the hostname with the one your tunnel printed:

curl -X POST https://some-random-words.trycloudflare.com/webhook -H "Content-Type: application/json" -d '{"event":"test.ping"}'

curl prints {"received":true}, and the terminal running index.js logs --- RECEIVED WEBHOOK --- followed by the headers and the payload. The route only accepts POST, so opening the URL in a browser shows Express’s Cannot GET /webhook page; that is expected.

Once curl works, paste the same URL into your provider’s webhook settings and trigger a test event from their dashboard.

Step 4: Set Up a Permanent Hostname on Your Domain

Quick tunnel URLs change every time you restart the command. For a permanent URL, you need a free Cloudflare account and a domain that uses Cloudflare’s nameservers.

First, log the CLI in to your Cloudflare account:

cloudflared tunnel login

This opens a browser window. Select your domain and authorise it. The CLI then saves a certificate file, usually at ~/.cloudflared/cert.pem.

Next, create a named tunnel. This example calls it local-dev:

cloudflared tunnel create local-dev

The command prints a tunnel ID (a UUID) and writes a JSON credentials file named after it into ~/.cloudflared/. You can list your tunnels at any time with cloudflared tunnel list.

Now create a file named config.yml in your ~/.cloudflared/ folder (if you keep it anywhere else, pass its path with --config when you run the tunnel). Replace YOUR_TUNNEL_UUID with your tunnel ID, and use your own home folder in the credentials path; on Linux it starts with /home/ rather than /Users/:

tunnel: YOUR_TUNNEL_UUID
credentials-file: /Users/yourusername/.cloudflared/YOUR_TUNNEL_UUID.json
ingress:
  - hostname: dev-webhook.yourdomain.com
    service: http://localhost:3000
  - service: http_status:404

The last rule, http_status:404, answers any hostname that does not match the rules above it.

Next, point your subdomain at the tunnel by creating a DNS record for it:

cloudflared tunnel route dns local-dev dev-webhook.yourdomain.com

Finally, start the tunnel with your configuration:

cloudflared tunnel run local-dev

Traffic sent to https://dev-webhook.yourdomain.com/webhook now reaches port 3000 on your machine. Repeat the curl test with that hostname to confirm. You can stop and start the tunnel as often as you like; the hostname stays the same.

The Gotcha: Body Parsers Break Signature Checks

Once real provider traffic reaches your local server, the first thing that usually breaks is webhook signature verification: your handler rejects requests even though the payloads look correct.

The cause is body parsing. Most providers sign the raw request bytes. If you verify against the parsed JSON instead, re-serialising it does not reproduce those exact bytes, so the signature never matches.

To fix it, give Express’s JSON parser a verify function that keeps the raw body on the request object. The snippet below shows the pattern. In your own index.js, the app.use(express.json({ verify … })) call replaces the plain app.use(express.json()) line, and the route shows where req.rawBody becomes available:

const express = require('express');
const app = express();
app.use(express.json({
  verify: (req, res, buf) => {
    req.rawBody = buf;
  }
}));
app.post('/webhook', (req, res) => {
  // Use req.rawBody (a Buffer) here to verify signatures
  console.log('Raw body preserved:', req.rawBody?.toString());
  res.sendStatus(200);
});

You now have the parsed object in req.body and the exact raw payload in req.rawBody, so signature verification can pass. The tutorial on how to verify webhook signatures with HMAC in Node.js shows the full check, including timestamp protection.

If It Still Doesn’t Work

  • The tunnel URL returns 502 Bad Gateway: cloudflared is running but nothing answers on port 3000. Start node index.js or fix the port in the --url flag or config.yml.
  • Cloudflare shows error 1033: the hostname points to a tunnel that is not running. Start it with cloudflared tunnel run local-dev.
  • tunnel route dns fails: a DNS record with that name usually exists already. Delete it in the Cloudflare dashboard, or pick another subdomain.
  • Every request gets a 404: you are calling the wrong path or method. The sample only accepts POST /webhook; for the named tunnel, also check that the hostname in config.yml matches the one you call.
  • Live updates never arrive through a quick tunnel: quick tunnels do not support Server-Sent Events. Use a named tunnel, or keep any SSE dashboard on localhost.

Frequently Asked Questions

Is a Cloudflare Tunnel secure?

Yes, it avoids opening inbound ports. cloudflared makes an outbound connection to Cloudflare, so you do not configure port forwarding on your router, and traffic passes through Cloudflare first. The hostname is still public, though: anyone who knows it can reach your local app while the tunnel runs, so verify webhook signatures and stop the tunnel when you are done.

Do I need a paid Cloudflare account to use tunnels?

No, Cloudflare Tunnel works on the free plan. You only need a free account and a domain on Cloudflare for a custom subdomain. Quick tunnels need neither, but Cloudflare intends them for testing and development only, and they have limits such as no Server-Sent Events support.

Can I route several local ports through one tunnel?

Yes, add one ingress rule per hostname in config.yml. For example, route api.yourdomain.com to port 4000 and web.yourdomain.com to port 3000, keep the http_status:404 rule last, and run cloudflared tunnel route dns once for each hostname.

Can I watch incoming webhooks live in a browser through the tunnel?

Not through a quick tunnel, because quick tunnels do not carry Server-Sent Events. Receive the webhooks through the tunnel and open the live dashboard on localhost, as in the guide to streaming webhooks to your browser with SSE.

Next Steps

You now have a public URL for local webhook testing, and optionally a permanent one on your own domain. Register it with your provider, add the raw-body capture and signature check, and save one sample payload per event type so you can replay it with curl later. For the rest of a dependable receiver, work through the API integrations and webhooks guide.

all_in_one_marketing_tool