# JavaScript examples

Copy any of these JavaScript snippets into a function as a starting point for your own code. The examples cover the patterns functions are most often used for: returning a response or a JSON payload, redirecting traffic, reading cookies, running an A/B test, and denying a request by country. They are grouped by where the function runs: Applications, for functions that handle the `fetch` event on an application, and Firewall, for functions that handle the `firewall` event on a firewall.

Most handlers in this collection are written in the Service Worker pattern, which registers the handler with `addEventListener`. New code uses ES Modules, and [Process a request body](/en/documentation/platform/functions/process-request-body/) is written that way: refer to [Best practices](/en/documentation/platform/functions/best-practices/) for the current pattern, and to [Migrate handler patterns in Functions](/en/documentation/guides/application-development/functions-and-runtime/migrate-handler-patterns/) for the before-and-after of each handler.

---

## Applications

- [A/B testing](/en/documentation/platform/functions/ab-testing.md): Split traffic between two responses in a function, picking a group at random and recording it in a cookie.
- [Extract a cookie value](/en/documentation/platform/functions/cookie-value.md): Read a named cookie from the request header in a function and use its value to shape the response you return.
- [Hello world](/en/documentation/platform/functions/hello-world.md): Return a fixed string from a function, without contacting an origin, and confirm the function is deployed and serving traffic.
- [Process a request body](/en/documentation/platform/functions/process-request-body.md): Read the body a client sends, switching on the Content-Type header to parse JSON, form data, text, binary, and streamed payloads.
- [Redirect all requests to one URL](/en/documentation/platform/functions/redirect-requests.md): Send every incoming request to one destination URL with a 301 redirect returned by a function, before it reaches your origin.
- [Respond with another site](/en/documentation/platform/functions/respond-site.md): Answer a request with content fetched from another site, relaying the upstream response while the browser keeps the original URL.
- [REST APIs](/en/documentation/platform/functions/rest-apis.md): Call a REST API from a function with fetch, using the GET, DELETE, POST, PUT, and PATCH methods with JSON data.
- [Return HTML](/en/documentation/platform/functions/return-html.md): Build an HTML page inside a function and return it with the correct content-type header, without rendering it on an origin server.
- [Return JSON](/en/documentation/platform/functions/return-json.md): Serialize an object and return it as a JSON response with the correct content-type header, for lightweight APIs and middleware.
- [Using args](/en/documentation/platform/functions/using-args.md): Pass JSON Args into a function from its instance and read them at runtime to change behavior without editing code.

---

## Firewall

- [Add a response header](/en/documentation/platform/functions/adding-response-header.md): Add a response header based on the visitor's country, with a function that runs on a firewall and reads GeoIP metadata.
- [Deny a request by country](/en/documentation/platform/functions/deny-request.md): Block requests from a chosen country before they reach your application, with a function that runs on a firewall.
- [Functions on a firewall](/en/documentation/platform/functions/general-firewall-example.md): Deny, drop, modify, or answer a request from one firewall handler, using the event actions available to a function on a firewall.

---

## Related resources

- [Quickstart](/en/documentation/platform/functions/quickstart.md): Take one of these snippets from the editor to a URI that answers it.
- [Handlers](/en/documentation/devtools/runtime/api-reference/handlers.md): Read the reference for the `fetch` and `firewall` handlers and the parameters each one receives.
- [Best practices](/en/documentation/platform/functions/best-practices.md): Scope, configure, and instrument a function so it stays inside the ceilings that bound every invocation.
- [Functions limits](/en/documentation/platform/functions/limits.md): Review the default limits for functions and function instances, including code size, memory, and execution time.
- [Troubleshooting](/en/documentation/platform/functions/troubleshooting.md): Find the cause when a function never runs, stops before it responds, or produces no log output.
