Answer requests from code on your machine
Before you begin
- Plan
- Included in Pro and Team. How plans and limits work
- Role
- Admin or higher.
- You need
- A tunnel program, such as ngrok or Cloudflare's `cloudflared`
- Code on your machine that answers requests, such as a server on `http://localhost:3000`
MockHub never connects to your computer, but a tunnel gives a server on it a public address. Make that address the project’s upstream, and your code answers every request no mock matches.
-
Start your code, such as a server on
http://localhost:3000. -
In another terminal, start a tunnel to the same port, and copy the
https://address it prints. With ngrok:Terminal window ngrok http 3000Choose a tunnel compares it with Cloudflare’s.
-
Open the project’s Captures tab. Under Recording, select Start recording.
-
In What answers a request, keep Proxy unmatched.
-
Enter the tunnel’s address as the Upstream base URL, such as
https://example-dev.ngrok-free.app. -
Select Save recording. The dialog Start forwarding to the upstream? names the address; select Start forwarding.
-
Send a request that no mock matches to the project’s address:
Terminal window curl https://orders-api.mockhub.dev/orders/42/refunds
Result
Section titled “Result”Your code receives GET /orders/42/refunds, and the caller gets what your code answers. Requests a mock matches are still answered by the mock. In Request logs each forwarded request shows as Forwarded to your upstream, and while Store request logs is on, Captures keeps it under a Proxying unmatched requests tag.
The request reaches your code as the caller sent it, headers and body included, with Host set to the tunnel’s name. A path in the Upstream base URL comes first: with https://example-dev.ngrok-free.app/api, the request above arrives at /api/orders/42/refunds.
To have your code answer every request, even those a mock matches, choose Record everything instead of Proxy unmatched. Record traffic from a real API explains the rest of the Recording settings.
Answer a provider’s address check
Section titled “Answer a provider’s address check”Some providers check a webhook address before they use it, by sending a request and reading the answer, such as Slack’s url_verification or Meta’s hub.challenge. The project’s inbox cannot pass that check: it always gives its own {"received":true} and never forwards. Give the provider an ordinary path of the project instead.
-
With the project forwarding to your tunnel as above, have your code answer the check on a path no mock matches, such as
/hooks/slack. -
In the provider’s settings, set the webhook address to that path of the project, such as
https://orders-api.mockhub.dev/hooks/slack.
The check, and every event after it, reaches your code, and the provider gets your code’s answer. The body arrives as sent, so a signature the provider computed over it checks out. A signature over the full URL, such as Twilio’s, covers the project’s address, not the tunnel’s. These requests are forwarded requests: they count against the upstream allowance and appear in the request logs as Forwarded to your upstream, not under Webhook inbox.
Choose a tunnel
Section titled “Choose a tunnel”Any tunnel that gives your server an address on the public internet works. MockHub refuses private addresses as an upstream, localhost included.
| Tunnel | Start it with | Its address |
|---|---|---|
| ngrok | ngrok http 3000, after running ngrok config add-authtoken <token> once with your ngrok account’s token |
Your account’s dev domain, such as https://example-dev.ngrok-free.app, the same on every run |
| Cloudflare quick tunnel | cloudflared tunnel --url http://localhost:3000, with no account |
A new https://….trycloudflare.com address on every run |
- A new address means changing the Upstream base URL: under Recording, select Change. Stored upstream headers belong to one address, so their values must be typed again.
- ngrok’s free plan shows its own warning page, instead of your code’s answer, to requests from a web browser. To skip it, add an upstream header named
ngrok-skip-browser-warningwith any value, such as1, as Send a credential to the upstream shows. - Each tunnel has its own limits and terms, in its documentation linked above.
On the Captures tab, select Stop recording under Recording, then stop the tunnel. The project answers from its mocks again with the next request, and keeps the address for next time.
Stopping only the tunnel does not stop forwarding. Until someone selects Stop recording, a request no mock matches gets the tunnel service’s error for an address that is offline, or a 502 from MockHub, instead of the 404 No mock matched.
Limits
Section titled “Limits”- For everyone on the project. While the project forwards, every request no mock matches goes to your machine, whoever sends it. Use a project only you send requests to.
- Counted. Every forwarded request counts against the workspace’s upstream allowance as well as the plan’s request limits. When the allowance runs out, a request that needs forwarding gets a
429. - The tunnel’s address is public. Anyone who learns it reaches your code without going through MockHub. To accept only what the project forwards, add an upstream header with a secret value, such as
X-Tunnel-Secret, and have your code refuse requests without it. - Not the inbox. Posts to
/__inboxare never forwarded. To receive them on your machine while the inbox keeps answering, usemockhub listen. - One upstream a project, so one tunnel. To forward to two machines, use two projects.
- MockHub provides no tunnel of its own: forwarded requests depend on the one you run.
- In the demo, a project can forward only to MockHub’s sample API. Your own code needs an account.