Skip to content

Examples

Explore the following examples for Workers.

45 examples

103 Early Hints

Allow a client to request static assets while waiting for the HTML response.

A/B testing with same-URL direct access

Set up an A/B test by controlling what response is served based on cookies. This version supports passing the request through to test and control on the origin, bypassing random assignment.

Aggregate requests

Send two GET request to two urls and aggregates the responses into one response.

Alter headers

Example of how to add, change, or delete headers sent in a request or returned in a response.

Auth with headers

Allow or deny a request based on a known pre-shared key in a header. This is not meant to replace the WebCrypto API.

Block on TLS

Inspects the incoming request's TLS version and blocks if under TLSv1.2.

Bulk redirects

Redirect requests to certain URLs based on a mapped object to the request's URL.

Cache using fetch

Determine how to cache a resource by setting TTLs, custom cache keys, and cache headers in a fetch request.

Conditional response

Return a response based on the incoming request's URL, HTTP method, User Agent, IP address, ASN or device type.

Cookie parsing

Given the cookie name, get the value of a cookie. You can also use cookies for A/B testing.

Custom Domain with Images

Set up custom domain for Images using a Worker or serve images using a prefix path and Cloudflare registered domain.

Data loss prevention

Protect sensitive data to prevent data loss, and send alerts to a webhooks server in the event of a data breach.

Debugging logs

Send debugging information in an errored response to a logging service.

Fetch HTML

Send a request to a remote server, read HTML from the response, and serve that HTML.

Fetch JSON

Send a GET request and read in JSON from the response. Use to fetch external data.

Hot-link protection

Block other websites from linking to your content. This is useful for protecting images.

Modify response

Fetch and modify response properties which are immutable by creating a copy first.

Post JSON

Send a POST request with JSON data. Use to share data with external servers.

Read POST

Serve an HTML form, then read POST requests. Use also to read JSON or POST data from an incoming request.

Redirect

Redirect requests from one URL to another or from one set of URLs to another set.

Respond with another site

Respond to the Worker request with the response from another website (example.com in this example).

Return JSON

Return JSON directly from a Worker script, useful for building APIs and middleware.

Rewrite links

Rewrite URL links in HTML using the HTMLRewriter. This is useful for JAMstack websites.

Set security headers

Set common security headers (X-XSS-Protection, X-Frame-Options, X-Content-Type-Options, Permissions-Policy, Referrer-Policy, Strict-Transport-Security, Content-Security-Policy).

Sign requests

Verify a signed request using the HMAC and SHA-256 algorithms or return a 403.