> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://akamai.ferndocs.com/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://akamai.ferndocs.com/_mcp/server.

# respondWith support for the onOriginResponse event handler

You can now use `onOriginResponse` and `onOriginRequest` to dynamically build a response from your JavaScript code using the [`respondWith`](doc:request-object#respondwith) method.

If you include both the construct-response in the metadata and `respondWith` in your JavaScript code, the last one that runs will win.

Use the `onOriginResponse` event handler to :

- Construct a response based on the results of a conditional match of the response received from an origin server.
- Construct a custom error or redirect page if the origin responds with an error or redirect status code.
- Provide multiple choice, status code 300, origin response handling. 
- If the origin replies with a list of URLs pointing to domains in various physical locations, you can apply a rule that:
  - Uses the best geolocation match.
  - Constructs a response using `responseWith` that includes only the relevant response. 
- Handle 5xx errors from the origin, using a `respondWith` redirect page that provides details about the original query instead of transferring the 5xx status code.
- Handle any type of origin response authentication procedure.

Use the `onOriginRequest` event handler to:

- Test the outcome of a client-request metadata stage execution.
- Use the test results to make decisions based on variables set up during that stage. 
- Validate the completion of a sub-request invoked using the `onClientRequest` event handler. If the attempt fails and information is missing from the request the results should be sent to Origin, and the flow terminated using `respondWith`.