|
| 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 | +[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. |
| 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/) |
| 18 | + |
| 19 | +#### Resource isolation |
| 20 | + |
| 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. |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | +## Adding Bindings to User Workers |
| 26 | +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 |
| 28 | + |
| 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. |
| 30 | + |
| 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. |
| 32 | + |
| 33 | +:::note |
| 34 | +For every product, be sure to check product specific limits. Some resources may require contacting Support to request limit increases. |
| 35 | +::: |
| 36 | + |
| 37 | +### 2. Attach the binding to the User Worker |
| 38 | + |
| 39 | +#### Using the API |
| 40 | + |
| 41 | +Use the [Upload User Worker API](http://localhost:1111/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 | +##### Example API request |
| 52 | + |
| 53 | +```bash |
| 54 | +curl -X PUT \ |
| 55 | + "https://api.cloudflare.com/client/v4/accounts/<account-id>/workers/dispatch/namespaces/<your-namespace>/scripts/<script-name>" \ |
| 56 | + -H "Content-Type: multipart/form-data" \ |
| 57 | + -H "Authorization: Bearer <api-token>" \ |
| 58 | + -F 'metadata={ |
| 59 | + "main_module": "worker.js", |
| 60 | + "bindings": [ |
| 61 | + { |
| 62 | + "type": "kv_namespace", |
| 63 | + "name": "USER_KV", |
| 64 | + "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 | + } |
| 75 | + ] |
| 76 | + }' \ |
| 77 | + -F 'worker.js=@/path/to/worker.js' |
| 78 | +``` |
| 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: |
| 80 | + |
| 81 | +```bash |
| 82 | +curl -X PUT \ |
| 83 | + "https://api.cloudflare.com/client/v4/accounts/<account-id>/workers/dispatch/namespaces/<your-namespace>/scripts/<script-name>" \ |
| 84 | + -H "Content-Type: multipart/form-data" \ |
| 85 | + -H "Authorization: Bearer <api-token>" \ |
| 86 | + -F 'metadata={ |
| 87 | + "bindings": [ |
| 88 | + { |
| 89 | + "type": "r2_bucket", |
| 90 | + "name": "STORAGE", |
| 91 | + "bucket_name": "<your-bucket-name>" |
| 92 | + } |
| 93 | + ], |
| 94 | + "keep_bindings": ["kv_namespace", "d1", "ai"] |
| 95 | + }' |
| 96 | +``` |
| 97 | +Once the API request is made, the Worker will be redeployed with the new bindings. The bindings will then become accessable 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: |
| 139 | +```bash |
| 140 | +npx wrangler deploy --name $SCRIPT_NAME --dispatch-namespace $NAMESPACE_NAME |
| 141 | +``` |
| 142 | + |
| 143 | +Once the Worker is deployed, the bindings will become accessable from the Worker code using `env.BINDING_NAME` (e.g., `env.USER_KV.get()`). |
0 commit comments