Deploy a worker via HTTP api?

Hi all,
i’ve successfully create/updated workers pragmatically via http POST request shown here https://workers.Cloudflare.com/docs/reference/tooling/api/scripts/

But those show up as scripts that are not yet deployed. You can see a URL next to the worker if its deployed.

How can i also deploy them programatically? is there an api for it?

So, I thought I had the answer for you, as I went down the Makefile / bash script deployment route a while ago. Unfortunately, I only use the workers/script/$scriptname endpoint with the metadata.json automagically configuring the KV stores to attach. There might be some additional configuration options in metadata.json, but I’ve not used it yet.

Probably not much help, and I can’t seem to find the API docs that specify the metadata format right now. :frowning:

Sorry I couldn’t be more help

1 Like

Thanks for trying though,
its surprising to me that they didn’t do this, i mean the only reason i tried this platform was the simplicity of writing piece of javascript and getting it deployed running.

I’m sure they will add it in future.
My usecase is to deploy customer submitted potentially malicious javascript in a sandbox environment not in my backend…
I’m gonna try to use these v8 workers myself lets see if its easy enough

I just found this thread as I was about to open my own on the same topic. I’m surprised to see this functionality to be missing. For my use case, being able to deploy a worker using curl is a must, even though Wrangler is awesome. I solved this for me by writing a Node package for deploying a script to Workers (with KV support) without the use of Wrangler. It uses the same APIs as the “Quick Edit” web UI for Workers does. It requires filling in a few values to secrets.json for it to function. At the moment I don’t plan on automating getting these values (which could be done by logging in in the script and obtaining them, however, it would get complicated with 2FA pretty quick).

You can find the Node package here:

I just found that the docs mention this seemingly non-existent API:

You can even use our API to dynamically update your Worker whenever your data changes.

From https://developers.cloudflare.com/workers/archive/writing-workers/storing-data/

So either at some point it was there, or was supposed to be there, but they didn’t get to it, or someone thought was there when it wasn’t or it is actually there, just impossible to find documentation for?

:wave: @tomashubelbauer,

This API?

curl -X PUT "https://api.cloudflare.com/client/v4/accounts/$CF_ACCOUNT_ID/workers/scripts/$SCRIPT_NAME" \
     -H  "Authorization: Bearer $CF_API_TOKEN" \
     -H "Content-Type: application/javascript" \
     --data "addEventListener('fetch', event => { event.respondWith(fetch(event.request)) })"

It’s documented here: https://developers.cloudflare.com/workers/tooling/api/scripts/#upload-or-update-a-workers-script

– OG

1 Like

Awesome, thanks @olivergrant! I’ll be the last person to dispute my garbage searching skills, still seems to be somewhat well-hidden to me though. Anyway I’m just glad to see this actually exists. I’ve updated my library above to use this API, but it’s so simple it’s not even necessary to have it anymore. Would be cool if the endpoint for listing workers could return the worker URL, too. As it stands, the subdomain of workers.dev is not something which can be obtained using the API, correct?

Edit: I should also add that the OP’s problem with the API is not a thing anymore with the current API.

I’m not exactly sure how is this solved?

curl -X PUT "https://api.cloudflare.com/client/v4/accounts/$CF_ACCOUNT_ID/workers/scripts/$SCRIPT_NAME" \
 -H  "Authorization: Bearer $CF_API_TOKEN" \
 -H "Content-Type: application/javascript" \
 --data "addEventListener('fetch', event => { event.respondWith(fetch(event.request)) })"

Indeed creates a worker, but it’s not deployed. How do I toggle on the deploy thing just like the UI allows? I’m using workers.dev. I thought I should add a route but the endpoint for that is curl -X POST https://api.cloudflare.com/client/v4/zones/$ZONE_ID/workers/routes and a workers.dev subdomain is zoneless as far as I understand.

(sorry about the multiple-posting, wish I found the preview pane before…)

Found the answer by checking what the dashboard does. This is with Axios:

axios
        .post(
          `https://api.cloudflare.com/client/v4/accounts/${CF_ACCOUNT_ID}/workers/scripts/${SCRIPT_NAME}/subdomain`,
          { enabled: true },
          {
            headers: {
              "Content-Type": "application/json",
              Authorization: `Bearer ${CF_API_TOKEN}`,
            },
          }
        )
1 Like

Here’s some javascript code that contains examples of deploying to workers and workers.dev, including sites, kv bindings, kv namespaces, routes, and environment variables.

2 Likes

Above workarounds unfortunately don’t work anymore. Is there any way to deploy a worker through the API? I’m so confused as to why there is nothing about this in the documentation.

Is this possible? Nothing in this thread seems to work. Deploying the code via API is possible, but is it possible to publish to workers.dev via the API?

Edit: Never mind, yoav2’s answer above works. Missed the part about adding /subdomain to the end of url. Worked great!

1 Like