Edit on github

#Cloudflare Workers - Quickstart


Node.js v6.5.0 or later. Serverless CLI v1.31.0 or later. You can run npm install -g serverless to install it. you also need our serverless-cloudflare-workers plugin. You can install it in your project with npm install --save serverless-cloudflare-workers.

#Create a new service

Create a new service using either the cloudflare-workers or cloudflare-workers-enterprise template, depending on your Cloudflare domain’s plan level, and specifying a unique name and an optional path for your service.

# Create a new Serverless Service/Project
$ serverless create --template cloudflare-workers --path new-project
# Change into the newly created directory
$ cd new-project
# Install npm dependencies
$ npm install

Note: there are two templates for Cloudflare Workers: cloudflare-workers and cloudflare-workers-enterprise. The enterprise template sets up a project that can natively deploy multiple scripts, each with their own routes. It requires an enterprise Cloudflare account to use.

The cloudflare-workers template still supports conditional routing and multiple scripts if you use a tool like webpack.

#Deploy, test and diagnose your service

You will need to set your Global API key from Cloudflare as an environmental variable named CLOUDFLARE_AUTH_KEY, and your Cloudflare account email as an environmental variable named CLOUDFLARE_AUTH_EMAIL. You can get your Global API key from your Cloudflare profile page. You will also need to set accountId and zoneId in serverless.yml under service.config. The first part of the path when you open Cloudflare dashboard as a logged in user is your accountId, e.g. dash.cloudflare.com/{accountId}. And the zoneId can be found from the overview tab after selecting the desired zone from the Cloudflare dashboard.

Environmental variables are variables that live inside your terminal.

For Mac and Linux users, you can set environmental variables like this:


And for Windows (CMD) users, you can set environmental variables like this:


You’ll need to redefine your environmental variables after each time you close your terminal.

  1. Deploy the Service

Use this when you have made changes to your Functions, Events or Resources in serverless.yml or you simply want to deploy all changes within your Service at the same time. If you've made changes to your routes since last deploying, the Serverless Framework will update them on the server for you.

serverless deploy
  1. Deploy the Function

Use this to quickly upload and overwrite your function code, allowing you to develop faster.

serverless deploy -f hello
  1. Invoke the Function

Invokes the Function and returns results.

serverless invoke --function helloWorld

Hello world

Your Function must have the events field populated in order for the serverless tool to know exactly which route to request. Defining the headers field is optional.

# serverless.yml
    name: foo
    script: bar
      - http:
          url: example.com/foo/bar
          # Defines the method used by serverless when the `invoke` command is used. Cloudflare Workers only support GET requests for now
          method: GET
            someKey: someValue


If at any point, you no longer need your service, you can run the following command to remove the Functions, Events and Resources that were created.

serverless remove