You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Bindings](/workers/runtime-apis/bindings/)are resources (KV store, database, etc.) that you can attach to User Workers to enable end customers to build more powerful applications. By utilizing bindings, you can extend Cloudflare's developer platform to your end customers without needing to build the infrastructure components yourself.
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
10
11
-
With bindings, your users can:
12
-
- Use [KV](/kv/) for high-read, eventually consistent key-value storage
13
-
- Use [D1](/d1/) for relational data and complex queries
14
-
- Store large files and assets in [R2](/r2/)
15
-
- Run AI inference using [Workers AI](/workers-ai/)
16
-
- Gain observability via [Analytics Engine](/analytics/analytics-engine/)
17
-
- Manage stateful coordination with [Durable Objects](/durable-objects/)
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
18
16
19
17
#### Resource isolation
20
18
21
-
Each Worker can only access the bindings that are explicitly attached to it. For full isolation, you can attach a unique resource (like a D1 database or KV namespace) to every User Worker. If multiple Workers need to access the same data, you can reuse a shared resource, but every Worker will still have its own binding to that shared resource.
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.
You can attach [bindings](/workers/runtime-apis/bindings/) to User Workers to give them access to resources like D1, KV, R2, or Workers Analytics Engine. This section explains how to create and attach those resources so they can be used from the User Worker.
27
-
### 1. Create the resources
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/).
28
25
29
-
For most bindings (e.g. KV, D1, R2, or Durable Objects), you'll first need to create the resource using the Wrangler CLI or Cloudflare API. This will generate a resource ID that you'll use when attaching the binding to the Worker in the next step.
26
+
### 1. Create a KV namespace
27
+
Create a KV namespace using the [Cloudflare API](/api/resources/kv/subresources/namespaces/methods/bulk_update/).
30
28
31
-
Some bindings, like Workers AI or Workers Analytics Engine, don’t require you to create a resource in advance. If you're using one of those, you can skip this step and go directly to Step 2.
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
32
33
33
:::note
34
-
For every product, be sure to check product specific limits. Some resources may require contacting Support to request limit increases.
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
35
:::
36
36
37
-
### 2. Attach the binding to the User Worker
38
-
39
-
#### Using the API
40
-
41
-
Use the [Upload User Worker API](/api/resources/workers_for_platforms/subresources/dispatch/subresources/namespaces/subresources/scripts/methods/update/) to attach bindings to a Worker. You can do this when you're first uploading the Worker script or when updating an existing Worker.
42
-
43
-
Once the API request is made, the Worker will be automatically redeployed with the updated configuration, making the binding available for use.
44
-
45
-
You'll attach bindings using the `bindings` array within the `metadata` object of your API request. For each binding, you'll need to specify:
46
-
47
-
-`type`: The binding type (e.g., kv_namespace, d1, r2_bucket, ai)
48
-
-`name`: The variable name used to access the binding in the Worker code
49
-
- (Optional) Resource-specific identifier: The namespace ID, database ID, bucket name, etc.
50
-
51
37
##### Example API request
52
38
53
39
```bash
@@ -62,21 +48,14 @@ curl -X PUT \
62
48
"type": "kv_namespace",
63
49
"name": "USER_KV",
64
50
"namespace_id": "<your-namespace-id>"
65
-
},
66
-
{
67
-
"type": "d1",
68
-
"name": "DB",
69
-
"id": "<your-database-id>"
70
-
},
71
-
{
72
-
"type": "ai",
73
-
"name": "AI"
74
51
}
75
52
]
76
53
}' \
77
54
-F 'worker.js=@/path/to/worker.js'
78
55
```
79
-
Once you've added bindings to a Worker, if you want to add additional ones, use the `keep_bindings` parameter to ensure existing bindings are preserved while adding new ones:
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.
80
59
81
60
```bash
82
61
curl -X PUT \
@@ -91,53 +70,6 @@ curl -X PUT \
91
70
"bucket_name": "<your-bucket-name>"
92
71
}
93
72
],
94
-
"keep_bindings": ["kv_namespace", "d1", "ai"]
73
+
"keep_bindings": ["kv_namespace"]
95
74
}'
96
75
```
97
-
Once the API request is made, the Worker will be redeployed with the new bindings. The bindings will then become accessible from the Worker code using `env.BINDING_NAME` (e.g., `env.USER_KV.get()`).
98
-
99
-
#### Using Wrangler
100
-
101
-
If your platform supports a CLI-based deployment process, you can use Wrangler to add bindings to your User Workers.
102
-
103
-
The bindings will be managed in the [Wrangler configuration file](/workers/wrangler/configuration/):
104
-
105
-
import { WranglerConfig } from"~/components";
106
-
107
-
<WranglerConfig>
108
-
109
-
```toml
110
-
name = "user-worker"
111
-
main = "src/index.js"
112
-
compatibility_date = "2025-06-10"
113
-
114
-
# KV binding
115
-
kv_namespaces = [
116
-
{ binding = "USER_KV", id = "<your-namespace-id>" }
117
-
]
118
-
119
-
# D1 binding
120
-
[[d1_databases]]
121
-
binding = "DB"
122
-
database_id = "<your-database-id>"
123
-
124
-
# R2 binding
125
-
[[r2_buckets]]
126
-
binding = "STORAGE"
127
-
bucket_name = "<your-bucket-name>"
128
-
129
-
# Workers AI binding
130
-
[ai]
131
-
binding = "AI"
132
-
```
133
-
134
-
</WranglerConfig>
135
-
136
-
**Deploy the Worker**
137
-
138
-
Use Wrangler to deploy the User Worker with the bindings:
Copy file name to clipboardExpand all lines: src/content/docs/cloudflare-for-platforms/workers-for-platforms/get-started/user-workers.mdx
+2-2Lines changed: 2 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -30,6 +30,6 @@ Since you will be deploying Workers on behalf of your users, you will likely wan
30
30
31
31
## Bindings
32
32
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/).
34
34
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