|
| 1 | +--- |
| 2 | +title: API and IaC |
| 3 | +pcx_content_type: concept |
| 4 | +sidebar: |
| 5 | + order: 5 |
| 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. 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 might 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. We have a [Cloudflare Terraform Provider](/terraform) with Workers resources. |
| 11 | + |
| 12 | +## Examples |
| 13 | + |
| 14 | +Here are examples of uploading a Worker script with common tools and languages. In particular, this highlights how to upload script content and format [multipart upload metadata](/workers/configuration/multipart-upload-metadata) which is different for each approach. |
| 15 | + |
| 16 | +### cURL |
| 17 | + |
| 18 | +- [Upload Worker Module API docs](https://developers.cloudflare.com/api/resources/workers/subresources/scripts/methods/update/) |
| 19 | + |
| 20 | +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) |
| 21 | + |
| 22 | +```bash |
| 23 | +curl https://api.cloudflare.com/client/v4/accounts/<account_id>/workers/scripts/my-hello-world-script \ |
| 24 | + -X PUT \ |
| 25 | + -H 'Authorization: Bearer <api_token>' \ |
| 26 | + -F 'metadata={ |
| 27 | + "main_module":"my-hello-world-script.mjs", |
| 28 | + "bindings": [ |
| 29 | + { |
| 30 | + "type": "plain_text", |
| 31 | + "name": "MESSAGE", |
| 32 | + "text": "Hello World!" |
| 33 | + } |
| 34 | + ], |
| 35 | + "compatibility_date": "2025-03-11" |
| 36 | + };type=application/json' \ |
| 37 | + -F 'my-hello-world-script.mjs=@-;filename=my-hello-world-script.mjs;type=application/javascript+module' <<EOF |
| 38 | +export default { |
| 39 | + async fetch(request, env, ctx) { |
| 40 | + return new Response(env.MESSAGE, { status: 200 }); |
| 41 | + } |
| 42 | +}; |
| 43 | +EOF |
| 44 | +``` |
| 45 | + |
| 46 | +:::note |
| 47 | + |
| 48 | +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. |
| 49 | +::: |
| 50 | + |
| 51 | +### cloudflare-typescript |
| 52 | + |
| 53 | +- [cloudflare-typescript github](https://github.com/cloudflare/cloudflare-typescript) |
| 54 | + |
| 55 | +<GitHubCode |
| 56 | + repo="cloudflare/cloudflare-typescript" |
| 57 | + file="examples/workers/script-upload.ts" |
| 58 | + commit="91d4cec33d0cfbc24747b500f67747bf7c6b5462" |
| 59 | + lang="ts" |
| 60 | + code={{ |
| 61 | + collapse: "1-2", |
| 62 | + }} |
| 63 | +/> |
| 64 | + |
| 65 | +### Terraform |
| 66 | + |
| 67 | +- [Cloudflare Terraform docs](/terraform) |
| 68 | +- [cloudflare_workers_script (Resource) docs](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/workers_script) |
| 69 | + |
| 70 | +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. |
| 71 | + |
| 72 | +```tf |
| 73 | +terraform { |
| 74 | + required_providers { |
| 75 | + cloudflare = { |
| 76 | + source = "cloudflare/cloudflare" |
| 77 | + version = "~> 5" |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | +
|
| 82 | +resource "cloudflare_workers_script" "my-hello-world-script" { |
| 83 | + account_id = "<replace_me>" |
| 84 | + script_name = "my-hello-world-script" |
| 85 | + main_module = "my-hello-world-script.mjs" |
| 86 | + content = file("my-hello-world-script.mjs") |
| 87 | + bindings = [{ |
| 88 | + name = "MESSAGE" |
| 89 | + type = "plain_text" |
| 90 | + test = "Hello World!" |
| 91 | + }] |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +:::note |
| 96 | + |
| 97 | +The Workers Script resource does not have a metadata property. All its child properties are at the top-level of the resource class. |
| 98 | +::: |
0 commit comments