Skip to content

Commit ccd5040

Browse files
committed
Update with newest workers examples
1 parent 514c79f commit ccd5040

File tree

3 files changed

+70
-24
lines changed

3 files changed

+70
-24
lines changed

src/content/changelog/workers/2025-08-11-new-workers-api.mdx renamed to src/content/changelog/workers/2025-09-02-new-workers-api.mdx

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
---
22
title: Simpler Workers API that lets you directly manage Workers, Versions, and Deployments
33
description: Simpler Workers API, SDK methods, and Terraform resources for directly managing Workers, Versions, and Deployments
4-
date: 2025-08-11
4+
date: 2025-09-02
55
---
66

77
The current Workers script upload API is convenient for a one-shot Workers deploy. However, it unavoidably creates [Versions and Deployments](/workers/configuration/versions-and-deployments/), and sometimes you don't want that to happen. Sometimes, you want more (and simpler!) control over how and when these are created.
88

9-
In [Cloudflare Terraform Provider](https://registry.terraform.io/providers/cloudflare/cloudflare/5.9.0/docs) 5.9.0 and [cloudflare-typescript](https://github.com/cloudflare/cloudflare-typescript) 4.6.0 (TODO double check these versions), we're introducing a clean, separated API that makes Workers, Versions, and Deployments individual "capital R" resources with clear concerns. It's now possible to create a Worker entity–and reference it elsewhere–before uploading code, bindings, or making it routable from the internet. You'll also enjoy a more ergonomic way to upload modules, fewer total endpoints, and clearer [API docs](/api/resources/workers/). Check out [Infrastructure as Code (IaC)](/workers/platform/infrastructure-as-code) for detailed examples.
9+
In [Cloudflare Terraform Provider](https://registry.terraform.io/providers/cloudflare/cloudflare/5.9.0/docs) 5.9.0 and [cloudflare-typescript](https://github.com/cloudflare/cloudflare-typescript) 4.6.0, we're introducing a clean, separated API that makes Workers, Versions, and Deployments individual "capital R" resources with clear concerns. It's now possible to create a Worker entity–and reference it elsewhere–before uploading code, bindings, or making it routable from the internet. You'll also enjoy a more ergonomic way to upload modules, fewer total endpoints, and clearer [API docs](/api/resources/workers/). Check out [Infrastructure as Code (IaC)](/workers/platform/infrastructure-as-code) for detailed examples.
1010

1111
## cloudflare-typescript example
1212

1313
```javascript
1414
const scriptContent = `
1515
export default {
1616
async fetch(request, env, ctx) {
17-
return new Response(env.MESSAGE, { status: 200 });
17+
return new Response("Hello, world!", { status: 200 });
1818
}
1919
};
2020
`;
2121

2222
/**
2323
* Create a Worker and set non-versioned settings like observability
2424
*/
25-
const worker = await client.workers.create({
25+
const worker = await client.workers.beta.workers.create({
2626
"my-worker",
2727
account_id: "...",
2828
observability: {
@@ -34,7 +34,7 @@ const worker = await client.workers.create({
3434
* Create the first version of the Worker
3535
* This is where code and bindings are defined and can be different between versions
3636
*/
37-
const version = await client.workers.versions.create(worker.id, {
37+
const version = await client.workers.beta.workers.versions.create(worker.id, {
3838
account_id: "...",
3939
main_module: "my-worker-script",
4040
compatibility_date: $today,
@@ -65,7 +65,7 @@ const deployment = await client.workers.scripts.deployments.create(worker.name,
6565

6666
## Terraform
6767

68-
In Terraform, you now have the option to just manage the Worker entity. You don't need to manage code, config, or deployments in your plan. This gives you flexibility to use Wrangler for build and deploy—in a different repo—while maintaining presence in your Terraform plan.
68+
In Terraform, you now have the option to just manage the Worker entity. You don't need to manage code, config, or deployments in your plan. This gives you flexibility to use Wrangler for build and deploy while maintaining presence in your Terraform plan.
6969

7070
```tf
7171
resource "cloudflare_worker" "my_worker" {
@@ -81,20 +81,26 @@ resource "cloudflare_worker" "my_worker" {
8181
## A radically simpler API
8282

8383
We wanted to make things easier, so we simplified these 8 APIs...
84-
- /workers/scripts/:script_name
85-
- /workers/scripts/:script_name/content
86-
- /workers/scripts/:script_name/script-settings
87-
- /workers/scripts/:script_name/settings
88-
- /workers/scripts/:script_name/subdomain
89-
- /workers/scripts/:script_name/secrets
90-
- /workers/scripts/:script_name/versions
91-
- /workers/scripts/:script_name/deployments
84+
- `/workers/scripts/:script_name`
85+
- `/workers/scripts/:script_name/content`
86+
- `/workers/scripts/:script_name/script-settings`
87+
- `/workers/scripts/:script_name/settings`
88+
- `/workers/scripts/:script_name/subdomain`
89+
- `/workers/scripts/:script_name/secrets`
90+
- `/workers/scripts/:script_name/versions`
91+
- `/workers/scripts/:script_name/deployments`
9292

9393
Into just three!
94-
- /workers/workers/:worker_id
95-
- /workers/workers/:worker_id/versions
96-
- /workers/scripts/:script_name/deployments
94+
- `/workers/workers/:worker_id`
95+
- `/workers/workers/:worker_id/versions`
96+
- `/workers/scripts/:script_name/deployments`
9797

98-
It's not just fewer endpoints... can you guess which of the old APIs created a new Version and Deployment and which didn't? That's no longer a mystery. POST `/versions` creates a Version and POST `/deployments` creates a Deployment.
98+
It's not just fewer endpoints... can you guess which of the old APIs created a new Version and Deployment and which didn't? That's no longer a mystery. `/versions` creates Versions and `/deployments` creates Deployments.
9999

100-
For now, the new APIs are in beta and the old ones will remain operational. In the future, we plan to mark the old APIs as deprecated in our OpenAPI docs, and remove the old Terraform resources and SDK methods in future major versions.
100+
For now, the new APIs are in **beta** and the old ones will remain operational. In the future, we plan to mark the old APIs as deprecated in our OpenAPI docs, and remove the old Terraform resources and SDK methods in future major versions.
101+
102+
:::note
103+
- `/workers/workers/` is deliberate and not a typo. This is so we can support both Worker ID and aliased name: `/workers/workers/7f694d3716bc4a63ad285159ff1fa375` and `/workers/workers/my-worker` both work.
104+
- The deployments endpoint is unchanged because there's no change to the functionality at this time. It will be changed in the future to be consistent with the rest of the APIs.
105+
- We won't deprecate the ability to one-shot create a Worker and deployment without a better replacement.
106+
:::

src/content/docs/workers/platform/infrastructure-as-code.mdx

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Icon } from "astro-icon/components";
1010

1111
While [Wrangler](/workers/wrangler/configuration) makes it easy to upload and manage Workers, there are times when you need a more programmatic approach. This could involve using Infrastructure as Code (IaC) tools or interacting directly with the [Workers API](/api/resources/workers/). Examples include build and deploy scripts, CI/CD pipelines, custom developer tools, and automated testing.
1212

13-
To make this easier, Cloudflare provides SDK libraries for popular languages, including [cloudflare-typescript](https://github.com/cloudflare/cloudflare-typescript) and [cloudflare-python](https://github.com/cloudflare/cloudflare-python). For IaC, you can use tools like HashiCorp's Terraform and the [Cloudflare Terraform Provider](/terraform) manage Workers resources.
13+
To make this easier, Cloudflare provides SDK libraries for popular languages such as [cloudflare-typescript](https://github.com/cloudflare/cloudflare-typescript) and [cloudflare-python](https://github.com/cloudflare/cloudflare-python). For IaC, you can use tools like HashiCorp's Terraform and the [Cloudflare Terraform Provider](/terraform) to manage Workers resources.
1414

1515
Below are examples of deploying a Worker using different tools and languages, along with important considerations for managing Workers with IaC.
1616

@@ -26,7 +26,7 @@ Generally, you'd run this bundling step before applying your Terraform plan or u
2626
wrangler deploy --dry-run --outdir build
2727
```
2828

29-
When using Wrangler for building but another method for uploading, make sure to copy all of your config from `wrangler.json` into your Terraform config, API request, or other. This is especially important with `compatibility_date` or flags your script relies on.
29+
When using Wrangler for building and a different method for uploading, make sure to copy all of your config from `wrangler.json` into your Terraform config or API request. This is especially important with `compatibility_date` or flags your script relies on.
3030

3131
## Terraform
3232

@@ -40,6 +40,9 @@ variable "account_id" {
4040
resource "cloudflare_worker" "my_worker" {
4141
account_id = var.account_id
4242
name = "my-worker"
43+
observability = {
44+
enabled = true
45+
}
4346
}
4447
4548
resource "cloudflare_worker_version" "my_worker_version" {
@@ -68,14 +71,16 @@ resource "cloudflare_workers_deployment" "my_worker_deployment" {
6871
}
6972
```
7073

74+
Notice how you don't have to manage all of these resources in Terraform. For example, you could just the `cloudflare_worker` resource and seamlessly use Wrangler or your own deployment tools for Versions or Deployments.
75+
7176
## Cloudflare API Libraries
7277

7378
This example uses the [cloudflare-typescript](https://github.com/cloudflare/cloudflare-typescript) SDK which provides convenient access to the Cloudflare REST API from server-side JavaScript or TypeScript.
7479

7580
<GitHubCode
7681
repo="cloudflare/cloudflare-typescript"
7782
file="examples/workers/script-upload.ts"
78-
commit="a7baf4fed47910228880800d5887a5d438baa6ad"
83+
commit="cb55a45d615425f6aaf9ee8a237d2c5424bf30b7"
7984
lang="ts"
8085
useTypeScriptExample={true}
8186
/>
@@ -346,3 +351,38 @@ EOF
346351

347352
</TabItem>
348353
</Tabs>
354+
355+
## Considerations with Durable Objects
356+
357+
[Durable Object](/durable-objects/) migrations are applied with deployments. This means you can't bind to a durable object in a Version if a deployment doesn't exist i.e. migrations haven't been applied. For example, running this in Terraform will fail the first time the plan is applied:
358+
359+
```tf
360+
resource "cloudflare_worker" "my_worker" {
361+
account_id = var.account_id
362+
name = "my-worker"
363+
}
364+
365+
resource "cloudflare_worker_version" "my_worker_version" {
366+
account_id = var.account_id
367+
worker_id = cloudflare_worker.my_worker.id
368+
bindings = [
369+
{
370+
type = "durable_object"
371+
name = "my_durable_object"
372+
class_name = "MyDurableObjectClass"
373+
}
374+
]
375+
migrations = {
376+
new_sqlite_classes = [
377+
"MyDurableObjectClass"
378+
]
379+
}
380+
# ...version props ommitted for brevity
381+
}
382+
383+
resource "cloudflare_workers_deployment" "my_worker_deployment" {
384+
# ...deployment props ommitted for brevity
385+
}
386+
```
387+
388+
To make this succeed, you first have to comment out the `durable_object` binding block, apply the plan, uncomment it, comment out the `migrations` block, then apply again. This time the plan will succeed. This also applies to the API or SDKs. This is an example where it makes sense to just manage the `cloudflare_worker` and/or `cloudflare_workers_deployment` resources while using Wrangler for build and Version management.

src/content/docs/workers/static-assets/direct-upload.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,12 +215,12 @@ Optionally, an assets binding can be provided if you wish to fetch and serve ass
215215

216216
## Programmatic Example
217217

218-
TODO update this with the prod commit
218+
This example is from [cloudflare-typescript](https://github.com/cloudflare/cloudflare-typescript/blob/main/examples/workers/script-with-assets-upload.ts).
219219

220220
<GitHubCode
221221
repo="cloudflare/cloudflare-typescript"
222222
file="examples/workers/script-with-assets-upload.ts"
223-
commit="a7baf4fed47910228880800d5887a5d438baa6ad"
223+
commit="cb55a45d615425f6aaf9ee8a237d2c5424bf30b7"
224224
lang="ts"
225225
useTypeScriptExample={true}
226226
/>

0 commit comments

Comments
 (0)