Cloudflare PUT API to set worker routes (for specific env) not working?

Hello, I would like to set routes for a worker with the given env, and do it with the Cloudflare REST API.

The following API call works, GET method:
https://api.cloudflare.com/client/v4/accounts/someIdHere/workers/services/aScriptNameHere/environments/envNameHere/routes

It returns this (that route has been added using the Cloudflare dashboard):

{
    "result": [
        {
            "id": "<someId>",
            "pattern": "test-something.mysite.com/*"
        }
    ],
    "success": true,
    "errors": [],
    "messages": []
}

When I try to call the PUT (or PATCH) method with the same API URL, but this JSON body:

[
  { "pattern": "test-otherthing.mysite.com/*" }
]

It returns:

{
    "result": null,
    "success": false,
    "errors": [
        {
            "code": 10002,
            "message": "workers.api.error.internal_server"
        }
    ],
    "messages": []
}

The bearer API token that I’m using, has the “Zone>Workers Routes>Edit” permission.

Can anyone help? Thank you very much.

Hi, that error message is quite odd. Can you share your account ID (not sensitive info), so the team and I can dig? Or, feel free to pass me your account email at azhao at cloudflare.com.

Hi azhao, this is the account ID:

940fdf871eba35d2e689079e78a1f103

There’s only one worker configured.

The env I’m working with is “dev4”, but had the same problem with other envs.

Thanks

Hi @azhao, have you & the team been able to reproduce and dig into the issue (if helpful, I can provide more info here or via email)? We are editing routes manually using a browser, but we would love to be able to use the API (if there’s an alternative API/tool/workaround to get the job done, we’d also be interested).

Solved the issue by using the worker routes API.

GET https://api.cloudflare.com/client/v4/zones/:zone_id/workers/routes

Returns this:

{
    "result": [
        {
            "id": "SOMEID",
            "pattern": "dev1.example.com",
            "script": "worker-two",
            "environment": "dev5",
            "request_limit_fail_open": false
        },
        {
            "id": "SOMEID",
            "pattern": "dev2.example.com/lorem/ipsum*",
            "script": "worker-one",
            "environment": "dev4",
            "request_limit_fail_open": false
        }
    ],
    "success": true,
    "errors": [],
    "messages": []
}

POST https://api.cloudflare.com/client/v4/zones/:zone_id/workers/routes

{
  "pattern":"mysite.com/lorem/ipsum*",
  "script":"new-script-name",
  "environment":"some-env-name"
}

If the route doesn’t exist it returns:

{
    "result": {
        "id": "SOMEIDHERE",
        "request_limit_fail_open": false
    },
    "success": true,
    "errors": [],
    "messages": []
}

If the route already exists it returns:

{
    "result": null,
    "success": false,
    "errors": [
        {
            "code": 10020,
            "message": "workers.api.error.duplicate_route"
        }
    ],
    "messages": []
}
1 Like