|
| 1 | +--- |
| 2 | +title: Managing Workers with the API and IaC |
| 3 | +pcx_content_type: concept |
| 4 | +sidebar: |
| 5 | + order: 11 |
| 6 | +--- |
| 7 | + |
| 8 | +import { GitHubCode } from "~/components"; |
| 9 | + |
| 10 | +Uploading and managing a Worker is easy with [Wrangler](/workers/wrangler/configuration), but sometimes you need to do it more programmatically or scalably. You might do this with IaC ("Infrastructure as Code") tools or by calling the [Cloudflare API](https://developers.cloudflare.com/api) directly. Use cases for the API include build and deploy scripts, CI/CD pipelines, custom dev tools, or testing. We provide API SDK libraries for common languages that make interacting with the API easier, such as [cloudflare-typescript](https://github.com/cloudflare/cloudflare-typescript). For IaC, a common tool is HashiCorp's Terraform. You can use the [Cloudflare Terraform Provider](/terraform) to create and manage Workers resources. |
| 11 | + |
| 12 | +## Examples |
| 13 | + |
| 14 | +Here are examples of uploading a Worker script with common tools and languages. In particular, they highlight how to upload script content and format [multipart upload metadata](/workers/configuration/multipart-upload-metadata) which is different with each approach. |
| 15 | + |
| 16 | +### curl |
| 17 | + |
| 18 | +Open a terminal or create a shell script to upload a Worker easily with curl. For this example, replace `<account_id>` with your [account id](/fundamentals/setup/find-account-and-zone-ids) and `<api_token>` with an [API token](/fundamentals/api/get-started/create-token) (not Global API key). Reference the Upload Worker Module API docs [here](https://developers.cloudflare.com/api/resources/workers/subresources/scripts/methods/update/). |
| 19 | + |
| 20 | +```bash |
| 21 | +curl https://api.cloudflare.com/client/v4/accounts/<account_id>/workers/scripts/my-hello-world-script \ |
| 22 | + -X PUT \ |
| 23 | + -H 'Authorization: Bearer <api_token>' \ |
| 24 | + -F 'metadata={ |
| 25 | + "main_module": "my-hello-world-script.mjs", |
| 26 | + "bindings": [ |
| 27 | + { |
| 28 | + "type": "plain_text", |
| 29 | + "name": "MESSAGE", |
| 30 | + "text": "Hello World!" |
| 31 | + } |
| 32 | + ], |
| 33 | + "compatibility_date": "2025-03-11" |
| 34 | + };type=application/json' \ |
| 35 | + -F 'my-hello-world-script.mjs=@-;filename=my-hello-world-script.mjs;type=application/javascript+module' <<EOF |
| 36 | +export default { |
| 37 | + async fetch(request, env, ctx) { |
| 38 | + return new Response(env.MESSAGE, { status: 200 }); |
| 39 | + } |
| 40 | +}; |
| 41 | +EOF |
| 42 | +``` |
| 43 | + |
| 44 | +:::note |
| 45 | + |
| 46 | +Change `my-hello-world-script.mjs=@-;` to `my-hello-world-script.mjs=@./my-hello-world-script.mjs;` and remove everything after and including `<<EOF` to upload a local file named `my-hello-world-script.mjs` instead. |
| 47 | +::: |
| 48 | + |
| 49 | +### JavaScript/TypeScript |
| 50 | + |
| 51 | +This example uses the [cloudflare-typescript](https://github.com/cloudflare/cloudflare-typescript) library which provides convenient access to the Cloudflare REST API from server-side JavaScript or TypeScript. |
| 52 | + |
| 53 | +<GitHubCode |
| 54 | + repo="cloudflare/cloudflare-typescript" |
| 55 | + file="examples/workers/script-upload.ts" |
| 56 | + commit="4fa90bf9f27574834a9b14a89de11ac56b8269f9" |
| 57 | + lang="ts" |
| 58 | + code={{ |
| 59 | + collapse: "1-2", |
| 60 | + }} |
| 61 | +/> |
| 62 | + |
| 63 | +### Terraform |
| 64 | + |
| 65 | +In this example, you need a local file named `my-hello-world-script.mjs` with script content similar to the above examples. Replace `account_id` with your own. Learn more about the Cloudflare Terraform Provider [here](/terraform). |
| 66 | + |
| 67 | +```tf |
| 68 | +terraform { |
| 69 | + required_providers { |
| 70 | + cloudflare = { |
| 71 | + source = "cloudflare/cloudflare" |
| 72 | + version = "~> 5" |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | +
|
| 77 | +resource "cloudflare_workers_script" "my-hello-world-script" { |
| 78 | + account_id = "<replace_me>" |
| 79 | + script_name = "my-hello-world-script" |
| 80 | + main_module = "my-hello-world-script.mjs" |
| 81 | + content = trimspace(file("my-hello-world-script.mjs")) |
| 82 | + bindings = [{ |
| 83 | + name = "MESSAGE" |
| 84 | + type = "plain_text" |
| 85 | + text = "Hello World!" |
| 86 | + }] |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +:::note |
| 91 | + |
| 92 | +- `trimspace()` removes empty lines in the file |
| 93 | +- The Workers Script resource does not have a `metadata` property like in the above examples. All of the properties found in `metadata` are instead at the top-level of the resource class, such as `bindings` or `compatibility_date`. Please see the [cloudflare_workers_script (Resource) docs](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/workers_script). |
| 94 | + ::: |
0 commit comments