-
Notifications
You must be signed in to change notification settings - Fork 10k
Add Workers for Platforms bindings documentation #22975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
babc0b2
docs: add Workers for Platforms bindings documentation
dinasaur404 342ccb8
Update bindings.mdx
dinasaur404 6fe9f3c
Update src/content/docs/cloudflare-for-platforms/workers-for-platform…
dinasaur404 d385b8d
Update src/content/docs/cloudflare-for-platforms/workers-for-platform…
dinasaur404 d9add4e
Address PR feedback on Workers for Platforms bindings documentation
dinasaur404 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
...nt/docs/cloudflare-for-platforms/workers-for-platforms/get-started/bindings.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| --- | ||
| pcx_content_type: how-to | ||
| title: Bindings | ||
| sidebar: | ||
| order: 5 | ||
| --- | ||
| import { Aside } from "@astrojs/starlight/components"; | ||
|
|
||
| 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. | ||
|
|
||
| With bindings, each of your users can have their own: | ||
| - [KV namespace](/kv/) that they can use to store and retrieve data | ||
| - [R2 bucket](/r2/) that they can use to store files and assets | ||
| - [Analytics Engine](/analytics/analytics-engine/) dataset that they can use to collect observability data | ||
| - [Durable Objects](/durable-objects/) class that they can use for stateful coordination | ||
|
|
||
| #### Resource isolation | ||
|
|
||
| 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. | ||
|
|
||
|  | ||
|
|
||
| ## Adding a KV Namespace to a User Worker | ||
| 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/). | ||
|
|
||
| ### 1. Create a KV namespace | ||
| Create a KV namespace using the [Cloudflare API](/api/resources/kv/subresources/namespaces/methods/bulk_update/). | ||
|
|
||
| ### 2. Attach the KV namespace to the User Worker | ||
|
|
||
| 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. | ||
|
|
||
| :::note | ||
| 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/). | ||
| ::: | ||
|
|
||
| ##### Example API request | ||
|
|
||
| ```bash | ||
irvinebroque marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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. | ||
|
|
||
| ```bash | ||
| 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"] | ||
| }' | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.