Skip to content

Commit bcf678e

Browse files
committed
[Workers] Add getting started guide for API and IaC
1 parent 031f2c8 commit bcf678e

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed

src/content/docs/workers/configuration/multipart-upload-metadata.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ If you're using the [Workers Script Upload API](/api/resources/workers/subresour
2424
}
2525
```
2626

27+
:::note
28+
29+
See the [Workers API and IaC](/workers/get-started/api-iac) getting started guide for examples of this.
30+
:::
31+
2732
## Attributes
2833

2934
The following attributes are configurable at the top-level.
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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 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+
- [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="4fa90bf9f27574834a9b14a89de11ac56b8269f9"
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+
text = "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

Comments
 (0)