Functions examples
Find the pages of the Functions examples in one list, each with a summary of what it covers.
This section holds the pages of the Functions examples.
Copy ready-to-use JavaScript snippets for functions, from returning a response to reading cookies and denying requests on a firewall.
Split traffic between two responses in a function, picking a group at random and recording it in a cookie.
Add a response header based on the visitor's country, with a function that runs on a firewall and reads GeoIP metadata.
Block requests from a chosen country before they reach your application, with a function that runs on a firewall.
Read a named cookie from the request header in a function and use its value to shape the response you return.
Deny, drop, modify, or answer a request from one firewall handler, using the event actions available to a function on a firewall.
Return a fixed string from a function, without contacting an origin, and confirm the function is deployed and serving traffic.
Read the body of a request inside a function, switching on the Content-Type header to parse JSON, form data, text, binary, and streamed payloads.
Send every incoming request to one destination URL with a 301 redirect returned by a function, before it reaches your origin.
Answer a request with content fetched from another site, relaying the upstream response while the browser keeps the original URL.
Call a REST API from a function with fetch, using the GET, DELETE, POST, PUT, and PATCH methods with JSON data.
Build an HTML page inside a function and return it with the correct content-type header, without rendering it on an origin server.
Serialize an object and return it as a JSON response with the correct content-type header, for lightweight APIs and middleware.
Pass JSON Args into a function from its instance and read them at runtime to change behavior without editing code.