How to get / patch / delete route id based on pattern?

Hi Cloudflare dev team,

I need a way to fetch route id based on the pattern, Right now, it seems the only way to fetch all routes and loop them through, which is going to be a bottleneck with my app, when there are 10s of thousands of routes. and I feel a simple pattern based API search would be more efficient.

It would be great to have all GET / PATCH / DELETE based on route eq pattern … but at least a GET would be a big help at this point

Thanks.

Wouldn’t it depend on the tools you use to query the API Cloudflare API v4 Documentation? You can use CF API to query the worker routes and match on pattern. For example, I have custom shell script to be able to add/delete/manage CF DNS and Worker Routes via the API and parsing JSON output using jq tool.

Step 1. Get zone id assign to variable zid

domain=your_cf_zone_domain.com
token=YOUR_CF_TOKEN_ID
accountid=YOUR_CF_ACCOUNT_ID
endpoint=https://api.cloudflare.com/client/v4/

endpoint_target="zones/?name=${domain}&status=active&page=1&per_page=100&order=status&direction=desc&match=all"
zid=$(curl -4sX GET "${endpoint}${endpoint_target}" -H "Content-Type:application/json" -H "Authorization: Bearer $token" | jq -r '.result[] | .id')

Step 2. Get worker route which matches pattern = https://domain.com/*

domain=domain.com
token=YOUR_CF_TOKEN_ID
accountid=YOUR_CF_ACCOUNT_ID
endpoint=https://api.cloudflare.com/client/v4/

endpoint_target="zones/$zid/workers/routes"
curl -4sX GET "${endpoint}${endpoint_target}" -H "Content-Type:application/json" -H "Authorization: Bearer $token" | jq -r '.result[] | select(.pattern == "https://domain.com/*")'
{
  "id": "474debe00a274be2aa4ac08c2cedXXXX",
  "pattern": "https://domain.com/*",
  "script": "cf_worker_script_name",
  "environment": "production",
  "request_limit_fail_open": false
}

If you need to get that pattern matches’ route id

domain=domain.com
token=YOUR_CF_TOKEN_ID
accountid=YOUR_CF_ACCOUNT_ID
endpoint=https://api.cloudflare.com/client/v4/

endpoint_target="zones/$zid/workers/routes"
curl -4sX GET "${endpoint}${endpoint_target}" -H "Content-Type:application/json" -H "Authorization: Bearer $token" | jq -r '.result[] | select(.pattern == "https://domain.com/*")' | jq -r '.id'
474debe00a274be2aa4ac08c2cedXXXX

Then you can use that matched route id for your PATCH/DELETE requests etc.

2 Likes

This will still get all the routes in the result array, and then its your job to search via shell command or loop through to find the route.

I was looking for endpoint to find that particular route directly, just like you can match zones by name, where you get result only for that zone

CF API unfortunately doesn’t list anything like that. How long does it take to get all >10K the Worker routes listed via CF API ? I’d never seen how >10K CF Worker routes would react to CF API querying! On rough estimates for my 30 CF Worker routes taking 0.35s to query via CF API, I’d say 10K CF Worker routes takes ~2mins ?

Have you tried contacting CF support to suggest such a feature and/or ask what suggestions they have for your usage case scenario?

Another way you can possible do it is do a query once to list/log them all and then map the pattern with the routeid for a historic record and then in future you can just query by the routeid. Then you can have that one time query done as a background cron task of sorts keeping the routeid to pattern mapping up to date.

curl -4sX GET "${endpoint}${endpoint_target}" -H "Content-Type:application/json" -H "Authorization: Bearer $token" | jq -r '.result[] | select(.pattern == "https://domain.com/*")' | jq -r '{id: .id, pattern: .pattern}'
{
  "id": "474debe00a274be2aa4ac08c2cedXXXX",
  "pattern": "https://domain.com/*"
}

except instead of querying the CF API via curl you would query a saved log of all the results.

Yeah, it it takes approx a minute.

It’s for a SaaS app and that means we are using/blocking resources when multiple users are using it and this one feature will quickly become our bottleneck.

Right now we are storing all route ids in a database to patch / delete it. That also means we will be storing millions of records and have to maintain it as well to avoid entries becoming redundant over the period of time.

That’s why I was hoping for a pattern based route get/patch/delete to avoid logging in the db and also do the API quickly without blocking much server resources.

I don’t think CF entertain API related support. Let’s see if any of the Dev team catches this thread.

Never hurts to ask :slight_smile:

1 Like

@MoreHelp

Also additional details and use case provided in ticket #2384946