2
0 Comments

Testing Webhooks Locally with Cloudflare's Free Tunneling Service

As a developer working with payment gateways and webhooks, testing these integrations in a local development environment is a routine but critical task. Traditionally, I relied on ngrok for this purpose, but its transition to a paid service made it less attractive for occasional use. This prompted me to explore other solutions, leading to a discovery that has significantly simplified my workflow: Cloudflare's tunneling service.

The Limitations of Ngrok and Other Services

Ngrok has been a staple in many developers' toolkits for testing local servers with external services. However, as ngrok moved to a paid model, the costs for short-term testing became unjustifiable. Alternatives often fell short, plagued by issues like mandatory verification pages that disrupted automated processes. This frustration led me to ask my Twitter network for alternatives, where I discovered Cloudflare's tunneling service.

Setting Up a Basic Tunnel with Cloudflare

Cloudflare offers a streamlined approach to tunneling with their cloudflared CLI tool. Here's how you can quickly set up a tunnel on macOS:

1. Download and Install cloudflared:

First, download the cloudflared tool from Cloudflare's website.

2. Start the Tunnel:

Use the command below to start tunneling your local environment:

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

This will connect http://localhost:8080 to a temporary subdomain at trycloudflare.com. The service provides a randomly generated URL, which you can use to access your local server publicly.
random generated url

Configuring a Persistent Tunnel URL with Cloudflare

For long-term projects, updating webhook URLs constantly is inefficient. Cloudflare allows the creation of persistent URLs using their DNS service at no additional cost.

1. Cloudflare Account Setup:

  • Register or log into your Cloudflare account and add your domain.
  • Configure Cloudflare DNS as your domain's nameserver and ensure domain ownership verification.

2. Create and Manage Your Tunnel

  • To authorize and select your domain for tunneling, log into Cloudflare using
cloudflared tunnel login
  • Subsequently, create your tunnel
cloudflared tunnel create myfirsttunnel

3. Tunnel Configuration

  • Navigate to the ~/.cloudflared directory on your system. Here, create a myfirsttunnel.yaml configuration file
url: http://localhost:8080
tunnel: YOUR_TUNNEL_UUID
credentials-file: /root/.cloudflared/YOUR_TUNNEL_UUID.json

Replace YOUR_TUNNEL_UUID with the actual tunnel ID.

  • If you encounter the error ERR Cannot determine default origin certificate path. No file cert.pem, try running the command with sudo:
sudo cloudflared tunnel ...

4. DNS Routing and Tunnel Execution

  • Assign a subdomain specifically for your tunnel
cloudflared tunnel route dns myfirsttunnel mysubdomain
  • To start the tunnel
cloudflared tunnel run myfirsttunnel

This will set up your tunnel at https://mysubdomain.yourdomain.com

Final notes

Exploring Cloudflare's tunneling service has been a practical learning experience, enhancing my ability to efficiently test webhooks and payment gateways locally. This exercise underscored the importance of adapting to new tools as older ones evolve or change their service models. While the setup described here is basic, it serves as a foundational step for developers looking to implement more sophisticated configurations.

For those interested in extending their use of Cloudflare's tunneling capabilities beyond the basics, the official Cloudflare documentation offers comprehensive guides and advanced options. This resource is invaluable for customizing and scaling your tunneling configurations to fit more complex project requirements.

This journey through setting up a local testing environment not only resolved immediate logistical challenges but also broadened my understanding of the potential within modern development tools. I encourage other developers to delve into these resources, as mastering such tools can significantly streamline the development and testing processes.

posted to Icon for group Developers
Developers
on June 9, 2024
Trending on Indie Hackers
I'm a lawyer who launched an AI contract tool on Product Hunt today — here's what building it as a non-technical founder actually felt like User Avatar 139 comments “This contract looked normal - but could cost millions” User Avatar 54 comments 👉 The most expensive contract mistakes don’t feel risky User Avatar 41 comments The indie maker's dilemma: 2 months in, 700 downloads, and I'm stuck User Avatar 35 comments I spent weeks building a food decision tool instead of something useful User Avatar 28 comments I just launched a browser API built for AI agents and LLMs User Avatar 23 comments