Cloudflare Docs
Workers
Visit Workers on GitHub
Set theme to dark (⇧+D)

Examples

  • Deliver an HTML page from an HTML string directly inside the Worker script.

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

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

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

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

  • Access custom Cloudflare properties and control how Cloudflare features are applied to every request.

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

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

  • 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.

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

  • Change the headers sent in a request or returned in a response.

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

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

  • Resolve requests to your domain to a set of proxy third-party origin URLs.

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

  • Cache POST requests using the Cache API.

  • Send Additional Cache Tags using Workers

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

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

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

  • Add the necessary CORS headers to a third party API response.

  • Redirect a response based on the country code in the header of a visitor.

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

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

  • Personalize website styling based on localized user time.

  • Get all geolocation data fields and display them in HTML.

  • Fetch weather data from an API using the user's geolocation data.

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

  • Shows how to restrict access using the HTTP Basic schema.

  • Examine the contents of a Headers object by logging to console with a Map.

  • Create a modified request with edited properties based off of an incoming request.

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

  • Set multiple Cron Triggers on three different schedules.

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

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

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

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

  • Set a Cron Trigger for your Worker.

  • Sign and verify a request using the HMAC and SHA-256 algorithms or return a 403.

  • Use the Cache API to store responses in Cloudflare's cache.