Skip to content

Commit 0587b45

Browse files
dinasaur404windsurf-bot[bot]
authored andcommitted
[C4Plat] Add Workers for Platforms bindings documentation (#22975)
--------- Co-authored-by: windsurf-bot[bot] <189301087+windsurf-bot[bot]@users.noreply.github.com>
1 parent f6607f5 commit 0587b45

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
---
2+
pcx_content_type: how-to
3+
title: Bindings
4+
sidebar:
5+
order: 5
6+
---
7+
import { Aside } from "@astrojs/starlight/components";
8+
9+
When you deploy User Workers through Workers for Platforms, you can attach [bindings](/workers/runtime-apis/bindings/) to give them access to resources like [KV namespaces](/kv/), [D1 databases](/d1/), [R2 buckets](/r2/), and more. This enables your end customers to build more powerful applications without you having to build the infrastructure components yourself.
10+
11+
With bindings, each of your users can have their own:
12+
- [KV namespace](/kv/) that they can use to store and retrieve data
13+
- [R2 bucket](/r2/) that they can use to store files and assets
14+
- [Analytics Engine](/analytics/analytics-engine/) dataset that they can use to collect observability data
15+
- [Durable Objects](/durable-objects/) class that they can use for stateful coordination
16+
17+
#### Resource isolation
18+
19+
Each User Worker can only access the bindings that are explicitly attached to it. For complete isolation, you can create and attach a unique resource (like a D1 database or KV namespace) to every User Worker.
20+
21+
![Resource Isolation Model](~/assets/images/reference-architecture/programmable-platforms/programmable-platforms-5.svg "Resource Isolation Model")
22+
23+
## Adding a KV Namespace to a User Worker
24+
This example walks through how to create a [KV namespace](/kv/) and attach it to a User Worker. The same process can be used to attach to other [bindings](/workers/runtime-apis/bindings/).
25+
26+
### 1. Create a KV namespace
27+
Create a KV namespace using the [Cloudflare API](/api/resources/kv/subresources/namespaces/methods/bulk_update/).
28+
29+
### 2. Attach the KV namespace to the User Worker
30+
31+
Use the [Upload User Worker API](/api/resources/workers_for_platforms/subresources/dispatch/subresources/namespaces/subresources/scripts/methods/update/) to attach the KV namespace binding to the Worker. You can do this when you're first uploading the Worker script or when updating an existing Worker.
32+
33+
:::note
34+
When using the API to upload scripts, bindings must be specified in the `metadata` object of your multipart upload request. You cannot upload the `wrangler.toml` file as a module to configure the bindings. For more details about multipart uploads, see [Multipart upload metadata](/workers/configuration/multipart-upload-metadata/).
35+
:::
36+
37+
##### Example API request
38+
39+
```bash
40+
curl -X PUT \
41+
"https://api.cloudflare.com/client/v4/accounts/<account-id>/workers/dispatch/namespaces/<your-namespace>/scripts/<script-name>" \
42+
-H "Content-Type: multipart/form-data" \
43+
-H "Authorization: Bearer <api-token>" \
44+
-F 'metadata={
45+
"main_module": "worker.js",
46+
"bindings": [
47+
{
48+
"type": "kv_namespace",
49+
"name": "USER_KV",
50+
"namespace_id": "<your-namespace-id>"
51+
}
52+
]
53+
}' \
54+
-F 'worker.js=@/path/to/worker.js'
55+
```
56+
Now, the User Worker has can access the `USER_KV` binding through the `env` argument using `env.USER_DATA.get()`, `env.USER_DATA.put()`, and other KV methods.
57+
58+
Note: If you plan to add new bindings to the Worker, use the `keep_bindings` parameter to ensure existing bindings are preserved while adding new ones.
59+
60+
```bash
61+
curl -X PUT \
62+
"https://api.cloudflare.com/client/v4/accounts/<account-id>/workers/dispatch/namespaces/<your-namespace>/scripts/<script-name>" \
63+
-H "Content-Type: multipart/form-data" \
64+
-H "Authorization: Bearer <api-token>" \
65+
-F 'metadata={
66+
"bindings": [
67+
{
68+
"type": "r2_bucket",
69+
"name": "STORAGE",
70+
"bucket_name": "<your-bucket-name>"
71+
}
72+
],
73+
"keep_bindings": ["kv_namespace"]
74+
}'
75+
```

src/content/docs/cloudflare-for-platforms/workers-for-platforms/get-started/user-workers.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ Since you will be deploying Workers on behalf of your users, you will likely wan
3030

3131
## Bindings
3232

33-
You can use any Workers [bindings](/workers/runtime-apis/bindings/) with the dynamic dispatch Worker or any user Workers.
33+
You can use any Workers [bindings](/workers/runtime-apis/bindings/) with the dynamic dispatch Worker or any [user Workers](/cloudflare-for-platforms/workers-for-platforms/get-started/bindings/).
3434

35-
Bindings for your user Workers can be defined on [multipart script uploads](/api/resources/workers_for_platforms/subresources/dispatch/subresources/namespaces/subresources/scripts/subresources/content/methods/update/) in the [metadata](/workers/configuration/multipart-upload-metadata/) part.
35+
To learn how to extend KV, D1, R2, and other resources to your User Workers through bindings, see [Bindings](/cloudflare-for-platforms/workers-for-platforms/get-started/bindings/).

0 commit comments

Comments
 (0)