Skip to content

Commit 1aad27f

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

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ 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 in use.
30+
:::
31+
32+
2733
## Attributes
2834

2935
The following attributes are configurable at the top-level.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
- 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)
20+
21+
```bash
22+
curl https://api.cloudflare.com/client/v4/accounts/<account_id>/workers/scripts/my-hello-world-script \
23+
-X PUT \
24+
-H 'Authorization: Bearer <api_token>' \
25+
-F 'metadata={
26+
"main_module":"my-hello-world-script.mjs",
27+
"bindings": [
28+
{
29+
"type": "plain_text",
30+
"name": "MESSAGE",
31+
"text": "Hello World!"
32+
}
33+
],
34+
"compatibility_date": "2025-03-11"
35+
};type=application/json' \
36+
-F 'my-hello-world-script.mjs=@-;filename=my-hello-world-script.mjs;type=application/javascript+module' <<EOF
37+
export default {
38+
async fetch(request, env, ctx) {
39+
return new Response(env.MESSAGE, { status: 200 });
40+
}
41+
};
42+
EOF
43+
```
44+
45+
:::note
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 (where the contents of that file can be the same script content above).
47+
:::
48+
49+
### cloudflare-typescript
50+
51+
- [cloudflare-typescript github](https://github.com/cloudflare/cloudflare-typescript)
52+
- Note this example uses a [Global API Key](/fundamentals/api/get-started/keys) instead of an API token
53+
54+
```mdx
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+
66+
### Terraform
67+
68+
- [Cloudflare Terraform docs](/terraform)
69+
- [cloudflare_workers_script (Resource) docs](https://registry.terraform.io/providers/cloudflare/cloudflare/latest/docs/resources/workers_script)
70+
71+
In this example, you need a local file named `my-hello-world-script.mjs` with script content similar to the above examples.
72+
73+
```tf
74+
resource "cloudflare_workers_script" "my-hello-world-script" {
75+
account_id = "<replace_me>"
76+
script_name = "my-hello-world-script"
77+
main_module = "my-hello-world-script.mjs"
78+
content = file("my-hello-world-script.mjs")
79+
bindings = [{
80+
name = "MESSAGE"
81+
type = "plain_text"
82+
test = "Hello World!"
83+
}]
84+
}
85+
```
86+
87+
:::note
88+
The Workers Script resource does not have a metadata property. All its child properties are at the top-level of the resource class.
89+
:::

0 commit comments

Comments
 (0)