Bindings
When you deploy User Workers through Workers for Platforms, you can attach bindings to give them access to resources like KV namespaces, D1 databases, R2 buckets, and more. This enables your end customers to build more powerful applications without you having to build the infrastructure components yourself.
With bindings, each of your users can have their own:
- KV namespace that they can use to store and retrieve data
- R2 bucket that they can use to store files and assets
- Analytics Engine dataset that they can use to collect observability data
- Durable Objects class that they can use for stateful coordination
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.
This example walks through how to create a KV namespace and attach it to a User Worker. The same process can be used to attach to other bindings.
Create a KV namespace using the Cloudflare API.
Use the Upload User Worker API 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.
curl -X PUT \ "https://api.cloudflare.com/client/v4/accounts/<account-id>/workers/dispatch/namespaces/<your-namespace>/scripts/<script-name>" \ -H "Content-Type: multipart/form-data" \ -H "Authorization: Bearer <api-token>" \ -F 'metadata={ "main_module": "worker.js", "bindings": [ { "type": "kv_namespace", "name": "USER_KV", "namespace_id": "<your-namespace-id>" } ] }' \ -F 'worker.js=@/path/to/worker.js'
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.
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.
curl -X PUT \ "https://api.cloudflare.com/client/v4/accounts/<account-id>/workers/dispatch/namespaces/<your-namespace>/scripts/<script-name>" \ -H "Content-Type: multipart/form-data" \ -H "Authorization: Bearer <api-token>" \ -F 'metadata={ "bindings": [ { "type": "r2_bucket", "name": "STORAGE", "bucket_name": "<your-bucket-name>" } ], "keep_bindings": ["kv_namespace"] }'
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Products
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark