How to test webhooks locally without ngrok or a tunnel
Forward real, signed webhook events to localhost, trigger test events without a payment and replay failures, all from the Bachs CLI.
Webhooks are awkward to build against. Your handler has to be reachable from the internet before you can see a single event, so most developers either deploy unfinished code or run a tunnel such as ngrok, paste its temporary URL into a dashboard, and copy a signing secret into their environment. If the handler fails, they fix it and go round again, often with a new tunnel URL to register.
The Bachs CLI removes that loop. It forwards real, signed webhook events to a port on your own machine, sends you test events on demand and replays the ones that failed, with no public URL and no tunnel. This guide shows how to set it up and test a webhook handler end to end on localhost.
Key takeaways
bachs listen --forward-to localhost:3000/webhooksforwards real, signed events to your machine over an outbound connection, so there is nothing to expose.bachs trigger collection.succeededsends a test event in the sandbox without a real payment.- Each session has its own signing secret, so the verification code you test locally is the code you run in production.
Why is testing webhooks locally hard?
A payment provider delivers a webhook by sending an HTTP request to an address on the internet. Your laptop doesn't have one. It sits behind a router that doesn't accept requests from outside, which is why developers reach for tunnels in the first place.
Tunnels work, but they add steps. The URL often changes each time you restart the tunnel, so you register it again. You copy a signing secret between the dashboard and your environment. And you still need a real payment to produce an event, which means clicking through a checkout every time you want to test.
How do you forward webhooks to localhost?
Install the CLI with Homebrew, npm or Scoop:
brew install bachsdev/bachs/bachs
npm install -g @bachs/cli Then connect it to your sandbox account. bachs login --sandbox prints a short code and opens your browser. Check the code matches the one in your terminal, approve it in the dashboard, and the CLI is paired.
Now point the CLI at the route your handler listens on:
bachs listen --forward-to localhost:3000/webhooks The CLI prints a webhook signing secret that starts with whsec_. Copy it into your local environment. Every event Bachs sends to your sandbox account now arrives at your handler, signed with that secret. To forward only what you're working on, name the events with --events, for example --events collection.succeeded,refund.paid.
Each delivery prints in your terminal as it arrives, with the status your handler returned and how long it took. A tick means your handler returned a 2xx. Anything else shows the status or the connection error, which is usually enough to find the problem without adding logging.
How do you send a test webhook without making a payment?
Leave bachs listen running and open a second terminal:
bachs trigger collection.succeeded The sample event goes through the normal delivery path, so it's signed and recorded exactly like a real one and reaches your listening session the same way.
Triggering only works in the sandbox, on purpose. A fake "payment succeeded" event in production could make your system deliver an order nobody paid for, so the CLI refuses rather than warns. In production, the way to see an event again is to replay a real one.
How do you verify the webhook signature locally?
Signature checks are worth getting right before you go live. A handler that verifies incorrectly usually looks fine until it starts rejecting live traffic.
Each forwarded request carries an X-Bachs-Signature-V2 header. Verify it against the raw request body, before you parse any JSON, because re-serializing JSON changes the bytes and breaks the check. The Node.js and Python SDKs do this for you with webhooks.constructEvent and webhooks.construct_event. They accept any matching signature, so your handler keeps working while you rotate a secret, and they reject deliveries more than five minutes old.
Because the forwarded event is signed with your session's own secret, the code you test locally is the code you run in production. Only the secret changes. See test webhooks locally for the full verification code.
How do you replay a webhook that failed?
When a delivery goes wrong, the first question is what Bachs actually sent:
bachs events listshows your recent events and how each delivery went.bachs events list --undeliveredshows events that matched no destination at all. If an event seems to have vanished, this usually explains it: the endpoint isn't subscribed to that type.bachs events replayfollowed by an event ID sends that event again.bachs events replay-failedredelivers everything that failed, for example after an outage on your side. Run it with--dry-runfirst to see exactly what it will resend.
How does it work without a tunnel?
Your machine opens the connection to Bachs, not the other way round. It's an ordinary outgoing HTTPS connection, so there's no public URL, no inbound port and no firewall change, and it works behind a home router and on office networks.
When you press Ctrl-C, the session closes. Your registered webhook destinations are never touched, so your production endpoint keeps receiving events as before.
Do you still need ngrok?
Not for Bachs webhooks. Compared with a tunnel:
- No URL to register. There's no temporary address to paste into a dashboard, and nothing to update when you restart.
- No secret to copy from a dashboard. The CLI prints a signing secret for the session.
- No payment needed to test.
bachs triggerproduces a signed event on demand. - Failures are visible. Each delivery's status prints as it arrives, and failed events can be replayed from the same tool.
A tunnel is still useful when another service, without its own CLI, needs to reach your machine.
What else can the Bachs CLI do?
Every Bachs API resource is a command. Type bachs customers on its own and the CLI lists what you can do with it. Customers, payments, subscriptions, payouts, refunds, products and transfers all work the same way, so you can look up the customer behind a failed webhook or check a payment's status without opening the dashboard.
Before you change anything, bachs whoami shows which environment you're connected to, the API address and the start of the key in use. The key created by bachs login can read your data and run local testing, but it can't move money, and it expires after 30 days. It's separate from your server's keys, so you can revoke it in the developer portal without touching production.
See the command reference for every command and flag.
Get started
- Install the CLI and run
bachs login --sandbox. See the CLI overview. - Start your app, then run
bachs listen --forward-towith your handler's address. - Copy the signing secret into your local environment.
- In a second terminal, run
bachs trigger collection.succeededand watch it arrive. - When your handler works, register your production endpoint and swap in its signing secret.