How to mock an API in a PR preview before the backend is ready
Return sample JSON from the Gregale edge on a PR preview, test loading and error states, then switch the same route back to the app.
You can mock an API for frontend development by returning fixed JSON on the
same preview URL that serves your UI. On a Gregale PR preview, a
respond edge rule can answer
GET /api/orders before the real endpoint exists. Your frontend keeps
calling fetch('/api/orders'); once the backend is ready, disable the rule
and let that same request reach the app.
Download the runnable starter. It includes an orders page, a server that deliberately returns 404 on the orders endpoint, and a script to check the success, error and fallback responses on a disposable preview.
1. Use a pull request preview, not your production app
Start with a Gregale PR preview for the
frontend. In the commands below, set PREVIEW_APP to its app slug and
PREVIEW_HOST to the exact hostname from the preview URL, without
https://. Run these commands with permission to manage that preview's edge
rules. Gregale only accepts respond rules on preview apps. Use synthetic
data, especially if anyone with the preview URL can open it.
2. Mock one route and one HTTP method
gregale edge rule create --app "$PREVIEW_APP" --kind respond \
--match-host "$PREVIEW_HOST" --match-path /api/orders --match-method GET \
--respond-status 200 \
--respond-body '{"orders":[{"id":"demo-1042","status":"shipped"}]}'
Save the rule ID from the command. Open the orders page to see the sample
order. A POST /api/orders still reaches the app because only GET matches.
Keep the fixture shaped like the real response so the UI does not end up
depending on fields the backend won't return.
3. Rehearse the error and hand the route back to the app
Set RULE_ID to the ID you saved, then change the response on the same rule:
gregale edge rule update "$RULE_ID" --app "$PREVIEW_APP" --kind respond \
--respond-status 503 --respond-body '{"error":"orders temporarily unavailable"}'
Reload the page or click Load orders to check the unavailable state. This is a synthetic 503 for testing the UI, not an outage in your backend. Then disable the rule:
gregale edge rule update "$RULE_ID" --app "$PREVIEW_APP" --disable
The starter expects the app's distinctive 404 body again. A real app would now serve whatever response its endpoint implements. Verify that response before asking reviewers to rely on it. The script compares the HTML before and after and confirms the same deployment serves the preview.
When to use an edge mock
Use it for a fixed success, empty or error response on a shared preview. It doesn't persist writes, simulate authentication or replace backend integration tests. Prefer a stateful mock or a real test backend when you need a sequence of requests to change data.
If the frontend and API will eventually run as separate services, see how to put them behind one hostname.
Questions
- How can I mock an API for frontend development without a mock server?
On a Gregale PR preview, use a respond edge rule on the exact hostname, method and path. Your frontend calls the same URL and gets the rule’s JSON and status without a backend handler.
- Can I use a Gregale respond rule in production?
No. Gregale only accepts respond rules on preview apps. Use synthetic data and match only the route and method you want to mock.
- How do I switch the mock back to the real API?
Disable the respond rule and check that the same URL reaches your app. This rule change does not redeploy the frontend.