Writing Workers

Get Started in the Playground

The Worker Playground makes it easy to experiment with workers. Workers you create in the playground can be tested against any site.

Launch Playground


Workers created in the Playground are only for experimentation and don’t run on all of Cloudflare’s infrastructure around the world. To enable that, write a worker inside your Cloudflare configuration dashboard.

Write a Real Worker

Once you’re ready to write a production Worker, move into the development environment inside the Cloudflare dashboard.

The site which you would like to develop a Worker on should be set up with Cloudflare. If not, sign up now and update your DNS.

Begin by logging in to your Cloudflare dashboard.


Navigate to the ‘Workers’ tab: Workers Tab


Click the ‘Launch Editor’ button. It will open the Workers code editor: Launch Workers


You will see a default workers script. This script, when deployed does a simple pass through and does not change any behavior on your site: Default Script

Replace the pre-filled script with the following snippet of code and save it:

addEventListener('fetch', event => {
  event.respondWith(fetchAndApply(event.request))
})

async function fetchAndApply(request) {
  return new Response('hello world')
}

This script, instead of loading from your origin, returns a response directly from our edge.


To enable this Worker on your site, go to the ‘Routes’ pane: Routes Tab


If your domain is ‘example.com’, create a route that says example.com/helloWorld and hit ‘Save’. Replace ‘example.com’ in this example with your actual domain: Create a Route


Visit your-site.com/helloWorld on your browser. You will see your worker returning the response: Visit Link


Check out our recipes to see what other cool things you can do with workers.