Response ModifyExecute JS• 6 min read
How to Programmatically Transform API Responses with JavaScript
Summary: Run custom inline JavaScript functions on incoming response payloads. Sort arrays, calculate dynamic prices, or mock personalized user experiences.
Why This Rule Matters for Developers & QA
Static mocks cannot handle dynamic business logic like filtering a list based on timestamp or incrementing sequence numbers. JavaScript transforms give you unlimited control.
Step-by-Step Implementation Guide
1
Set URL Rule Pattern
Target the dynamic API endpoint (e.g. `*/api/v1/products`).
2
Write JavaScript Transform Function
Implement a function `(data, req) => { ... return data; }` that mutates the payload.
3
Inspect Live Execution
Open DevTools or Network viewer to verify the transformed payload received by your client code.
Configuration Snippet
How to Programmatically Transform API Responses with JavaScript — proxyceptor-rule.json
JSON
// ProxyCeptor JS Transform Function(data) => { if (Array.isArray(data.items)) { // Discount all item prices by 50% for flash sale testing data.items = data.items.map(item => ({ ...item, price: Math.round(item.price * 0.5), sale_badge: 'TEST_50_OFF' })); } return data;}