How to upload Worker with API Token?

I want to use the API token feature to upload a Cloudflare Worker. That’s after all much better than a general API key.

So I used the standard Workers template from the web interface. But I get the message back from the API that:

PUT method with API Tokens is not supported by this API code

But the Cloudflare API requires a PUT action to upload a Worker. :thinking:

So how do we upload Workers with an API Token? Is this even possible? :confused:

2 Likes

I asked Support and they confirmed that API tokens do work with Cloudflare Workers. So I went through all my settings & code again to find the error.

The mistake I made was to use the following API end point with API Tokens:

https://api.cloudflare.com/client/v4/zones/{ZONE_ID}/workers/script

This end point works fine with the regular, general purpose API key. But it fails with the API tokens feature.

This API end point to upload a worker does work with API Tokens:

https://api.cloudflare.com/client/v4/accounts/{ACCOUNT_ID}/workers/scripts/{SCRIPT_NAME}

It seems to be undocumented why API Tokens are incompatible with the earlier end point I used all the time earlier, but now I do have my issue solved. :slightly_smiling_face:

3 Likes

Thanks for sharing the solution! :slight_smile:

2 Likes

Thanks Thomas! :+1: Hopefully it helps someone else with the same problem.

1 Like

It did. Thanks.

Running into the same issue with zones/:zone_identifier/dns_records/:identifier for PUT and PATCH methods.

Edit: Ah, found the problem. Misassembled the URI. Confusing error message, but… I should not be coding after midnight…

1 Like

I ran into the same error, by coincidence my terminal line-broke right where the error was.

The real error is with a missing / character. In my case, my request URI was /zones/$ZONE-ID/dns_records$RECORD-ID (note the missing / between dns-records and $RECORD-ID) but I didn’t catch it right away because the edge of the terminal was right after the s in dns-records. Silly mistake, but sillier error message. Malformed URI requests should return with an error indicating such.