Skip to content
On this page

Create a new cloudflare worker using API

It's not well documented, but it's easily possible to create a cloudflare worker using the API.

The API is documented as Upload Worker Module - https://developers.cloudflare.com/api/operations/worker-script-upload-worker-module

Metadata is required for the worker, more details - https://developers.cloudflare.com/workers/configuration/multipart-upload-metadata

INFO

javascript+module is required in the Content-Type for the main script file when using ES Module syntax, even though main_module is set. Otherwise it returns with uncaught syntaxerror: unexpected token 'export'

Sample code using fetch

javascript
const api_token = '...'
const metadata = {
    "main_module": "main.js",
    "bindings": [
      {
        "type": "plain_text",
        "name": "MESSAGE",
        "text": "Hello, world!"
      }
    ],
    "compatibility_date": "2021-09-14"
}
const script = "export ..."
const fdata = new FormData()
fdata.set('metadata', new File([JSON.stringify(metadata)], 'metadata.json', {type: 'application/json'}))
fdata.set('main.js', new File([script], 'main.js', {type: 'application/javascript+module'}))

const res = await fetch('https://api.cloudflare.com/client/v4/accounts/{account_id}/workers/scripts/{script_name}/subdomain/{subdomain}', {
    method: 'PUT',
    body: fdata,
    headers: {
        'Authorization': `Bearer ${api_token}`,
        // dont set Content-Type
    }
})

Update

It's possible to do it in a (kind of) better way using Cloudflare SDK. Typescript is broken though as of Aug 24

typescript
import { Cloudflare } from 'cloudflare'
// @ts-ignore
import type {ScriptUpdateParams} from 'cloudflare/src/resources/workers/scripts/scripts'

const cf = new Cloudflare({
    apiToken: "...",
    fetch: fetch,
})
const account_id = '...'

const metadata: ScriptUpdateParams.Variant0.Metadata = {
    "main_module": "main.js",
    "bindings": [
        {
            "type": "plain_text",
            "name": "MESSAGE",
            "text": "Hello, world!"
        }
    ],
    "compatibility_date": "2021-09-14"
}
const script = "export ..."

const uw2 = await cf.workers.scripts.update(workerName, {
    account_id,
    // @ts-ignore
    metadata: new File([JSON.stringify(metadata)], 'metadata.json', {type: 'application/json'}),
    // @ts-ignore
    ["main.js"]: new File([script], 'main.js', {type: 'application/javascript+module'}),
})

Made with ❤️ using the awesome vitepress