Skip to content
Start here

Create Route

client.Workers.Routes.New(ctx, params) (*RouteNewResponse, error)
POST/zones/{zone_id}/workers/routes

Creates a route that maps a URL pattern to a Worker.

Security
API Token

The preferred authorization scheme for interacting with the Cloudflare API. Create a token.

Example:Authorization: Bearer Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY
API Email + API Key

The previous authorization scheme for interacting with the Cloudflare API, used in conjunction with a Global API key.

Example:X-Auth-Email: user@example.com

The previous authorization scheme for interacting with the Cloudflare API. When possible, use API tokens instead of Global API keys.

Example:X-Auth-Key: 144c9defac04969c7bfad8efaa8ea194
Accepted Permissions (at least one required)
Workers Routes Write
ParametersExpand Collapse
params RouteNewParams
ZoneID param.Field[string]

Path param: Identifier.

maxLength32
Pattern param.Field[string]

Body param: Pattern to match incoming requests against. Learn more.

Script param.Field[string]optional

Body param: Name of the script to run if the route matches.

ReturnsExpand Collapse
type RouteNewResponse struct{…}
ID string

Identifier.

maxLength32
Pattern string

Pattern to match incoming requests against. Learn more.

Script stringoptional

Name of the script to run if the route matches.

Create Route

package main

import (
  "context"
  "fmt"

  "github.com/cloudflare/cloudflare-go"
  "github.com/cloudflare/cloudflare-go/option"
  "github.com/cloudflare/cloudflare-go/workers"
)

func main() {
  client := cloudflare.NewClient(
    option.WithAPIToken("Sn3lZJTBX6kkg7OdcBUAxOO963GEIyGQqnFTOFYY"),
  )
  route, err := client.Workers.Routes.New(context.TODO(), workers.RouteNewParams{
    ZoneID: cloudflare.F("023e105f4ecef8ad9ca31a8372d0c353"),
    Pattern: cloudflare.F("example.com/*"),
  })
  if err != nil {
    panic(err.Error())
  }
  fmt.Printf("%+v\n", route.ID)
}
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "result": {
    "id": "023e105f4ecef8ad9ca31a8372d0c353",
    "pattern": "example.com/*",
    "script": "my-workers-script"
  },
  "success": true
}
Returns Examples
{
  "errors": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "messages": [
    {
      "code": 1000,
      "message": "message",
      "documentation_url": "documentation_url",
      "source": {
        "pointer": "pointer"
      }
    }
  ],
  "result": {
    "id": "023e105f4ecef8ad9ca31a8372d0c353",
    "pattern": "example.com/*",
    "script": "my-workers-script"
  },
  "success": true
}