Cloudflare::API::Workers

NAME

Cloudflare::API::Workers - manage Worker scripts, versions, assets, and routes

SYNOPSIS

my $workers=$api->workers();
my $version=$workers->upload_version('my-app',
    metadata => {
        main_module        => 'worker.mjs',
        compatibility_date => '2026-09-22'
    },
    files => [{ name => 'worker.mjs', path => 'dist/worker.mjs' }]
);
$workers->create_deployment('my-app', {
    strategy => 'percentage',
    versions => [{ version_id => $version->{'id'}, percentage => 100 }]
});

DESCRIPTION

Most Worker methods use the account ID configured on Cloudflare::API. Route methods instead take a zone ID explicitly. The module sends prepared modules and Cloudflare metadata; it does not build scripts, invoke npm or Wrangler, generate a Worker entry point, or create routes automatically.

Cloudflare's Worker identifiers have distinct purposes. The id returned by list_scripts() is the script name used in API paths, tag is the immutable Worker ID, tags contains user-assigned labels, and etag identifies the current script content. The search API calls the immutable tag value id. inspect_script() uses the unambiguous selector names name, tag, and etag.

Non-list JSON methods return Cloudflare's decoded result by default. Except where noted, pass full_response => 1 to return the complete decoded Cloudflare response. List methods follow all available pages and return a flat array reference. Their _page() forms return lazy HTTP::API::Core::Pagination objects, while _page_response() forms make one request and retain the complete decoded response. Script names, version IDs, secret names, and route IDs are percent-encoded in URLs.

METHODS

Every list_*() or search_scripts() call may hold its complete result set in memory. Use its _page() form when the result may be large. full_response is unavailable for list method families; use the corresponding _page_response() method for one complete Cloudflare response.

Write bodies must be hash references. Missing account context, invalid identifiers, selectors or body shapes, ambiguous inspection matches, and unknown upload options cause exceptions before or during the request. The Cloudflare::API man page describes HTTP, transport, and Cloudflare response failures.

MODULE UPLOADS

upload_script() and upload_version() require metadata with a non-empty main_module that matches the name of one uploaded file. Supply Cloudflare fields such as compatibility_date and bindings in the metadata. files must be a non-empty array of entries with a name and exactly one of path or content; each entry may also set content_type (default application/javascript+module). Names may contain letters, numbers, dots, dashes, underscores, and slashes for nested modules. Duplicate names are rejected. Module content and the multipart request are assembled in memory, so large uploads need enough process memory.

Use upload_script() when immediate deployment is intended. To stage a version, use upload_version(), inspect it with get_version() if needed, then call create_deployment() to make it active. Version upload alone does not change traffic.

STATIC ASSETS

upload_assets($name, $source, %options) accepts a directory path, an array reference of filenames or { path => $file, name => 'nested/page.html', content_type => 'image/jxl' } entries, or a hash reference mapping absolute URL paths to content scalars or { path => $file } entries. Directory uploads recurse and preserve paths relative to the directory. Array filenames use their basenames unless name is supplied. Duplicate URL paths are rejected; directory and file-list uploads reject symlinks. The source must not be empty. prefix => '/docs' places every URL path under /docs.

The method hashes the content, registers a manifest, uploads the buckets Cloudflare requests, and returns a manifest plus a short-lived completion jwt. Uploads are assembled in memory. Common HTML, CSS, JavaScript, JSON, text, font, PDF, WASM, and image extensions receive a MIME type; unknown extensions use application/octet-stream. An entry may override the MIME type with content_type.

Asset upload does not deploy a Worker. Put the returned token into a version's metadata, along with an assets binding, then deploy that version:

my $assets=$workers->upload_assets('my-app', 'dist', prefix => '/docs');
my $version=$workers->upload_version('my-app',
    metadata => {
        main_module        => 'worker.mjs',
        compatibility_date => '2026-09-22',
        assets             => { jwt => $assets->{'jwt'} },
        bindings           => [{ type => 'assets', name => 'ASSETS' }]
    },
    files => [{ name => 'worker.mjs', path => 'dist/worker.mjs' }]
);

The prepared Worker must route requests to its asset binding, for example with env.ASSETS.fetch(request). Treat the JWT as a credential and keep it out of logs. See cloudflare-api --man for command-line asset source options.

SEE ALSO

Cloudflare::API, Cloudflare::API::Zones, Cloudflare::API::SecretsStore

AUTHOR

Andrew Speer andrew.speer@isolutions.com.au

LICENSE and COPYRIGHT

Copyright (c) 2026 Andrew Speer. This software is free software under the same terms as Perl 5.