|
| 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 | + |
| 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 | +``` |
0 commit comments