GuideMock responses
JS Hook responses
Write a function that reads the request and builds the response dynamically.
Dynamic JavaScript responses are a Pro feature.
Switch the body tab from JSON to JS Hook and write a function body. The returned value becomes the response body; async / await is supported.
// Single argument: context (remote modules cannot be imported in the sandbox)
// context.req — { url, method, headers, query, body, params }
// context.loadFaker() — loads Faker (en / fr / de / ru)
// context.loadScript(url) — loads a UMD / IIFE script
const body = context.req.body || {};
if (body.coupon === 'BLACKFRIDAY') {
return {
code: 0,
data: { orderId: 'A20261012', discount: 0.3, message: 'Coupon applied' },
};
}
return {
code: 0,
data: { orderId: 'A20261012', discount: 0, message: 'No coupon' },
};Available variables
| Variable | Fields | Notes |
|---|---|---|
context.req | url, method, headers, query, body, params, gql | body is parsed when it is JSON; params holds the path params; gql is only available for GraphQL requests |
context.loadFaker() | Faker instances | const { faker, fakerEN, fakerFR, fakerDE, fakerRU } = await context.loadFaker(); faker is the English (en) instance |
context.loadScript(url) | Loads a UMD / IIFE script | The sandbox CSP only allows the extension’s own files, so a remote import() never works; scripts are cached by URL |
Click Try it to run the hook against an editable req and inspect the return value without sending a request. Errors are reported with the thrown message in the panel, and the request falls through to the network.

Limits
- No `import()`. The sandbox page’s CSP is
script-src 'self'; remote,data:andblob:imports always fail. Load a UMD build withcontext.loadScript(url)instead. - ES modules are not supported by
loadScript— pass an IIFE / UMD build (*.min.js). - No page access. Hooks cannot read the DOM,
windowglobals orlocalStorageof the page; only the request context is available. - Execution has a timeout — a hook that blocks forever is aborted and the request passes through.
Hooks run as function bodies inside an isolated extension sandbox. Importing a rule file containing JS code shows a trust warning — only import files from sources you trust.
Stuck, or need something clarified? support@mockrew.com·Terms of Service