Skip to content

Commit 371bbeb

Browse files
[Secrets Store] Fix examples and review workers guide (#21603)
* Add async call to access secret * Remove extra env. from fixed auth bearer * Replace default store by first store * Add async call to first code example and fix env.<binding name> * Remove top level scope example * Further adjustments * More consistent naming: binding vs secret_name examples * Opt for binding variable in h3
1 parent e4b191e commit 371bbeb

File tree

1 file changed

+13
-27
lines changed

1 file changed

+13
-27
lines changed

src/content/docs/secrets-store/integrations/workers.mdx

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ This is different from Workers [Variables and Secrets](/workers/configuration/se
2020

2121
If [using the Dashboard](#via-dashboard), make sure you already have a Workers application. Refer to the [Workers get started](/workers/get-started/dashboard/) for guidance.
2222

23-
You should also have a store created under the Secrets Store tab on the Dashboard. The default store in your account is automatically created when a user with [Super Administrator or Secrets Store Admin role](/secrets-store/access-control/) interacts with it.
23+
You should also have a store created under the Secrets Store tab on the Dashboard. The first store in your account is created automatically when a user with [Super Administrator or Secrets Store Admin role](/secrets-store/access-control/) interacts with it.
2424

25-
You can also use the [Wrangler command](/workers/wrangler/commands/#secrets-store-store) `secrets-store store create <name>` to create your default store.
25+
You can also use the [Wrangler command](/workers/wrangler/commands/#secrets-store-store) `secrets-store store create <name>` to create your first store.
2626

2727
## 1. Set up account secrets in Secrets Store
2828

29-
If there are no account secrets yet, follow the steps below. You must have a [Super Administrator or a Secrets Store Admin role](/secrets-store/access-control/) within your Cloudflare account.
29+
If there are no secrets in the store yet, follow the steps below. You must have a [Super Administrator or a Secrets Store Admin role](/secrets-store/access-control/) within your Cloudflare account.
3030

3131
:::note
3232
You may also add account secrets directly from the Workers settings on the dashboard. You can skip to [step 2](#via-dashboard) to do that.
@@ -38,13 +38,13 @@ Use the [Wrangler command](/workers/wrangler/commands/#secrets-store-secret) `se
3838
To use the following example, replace the store ID and secret name by your actual data. You can find and copy the store ID from the [Secrets Store tab](https://dash.cloudflare.com/?to=/:account/secrets-store/) on the dashboard. A secret name cannot contain spaces.
3939

4040
```sh
41-
npx wrangler secrets-store secret create <STORE_ID> --name MY_SECRETS_STORE_SECRET --scopes workers --remote
41+
npx wrangler secrets-store secret create <STORE_ID> --name MY_SECRET_NAME --scopes workers --remote
4242
```
4343

4444
```sh output
4545
✓ Enter a secret value: › ***
4646

47-
🔐 Creating secret... (Name: MY_SECRETS_STORE_SECRET, Value: REDACTED, Scopes: workers, Comment: undefined)
47+
🔐 Creating secret... (Name: MY_SECRET_NAME, Value: REDACTED, Scopes: workers, Comment: undefined)
4848
✓ Select an account: › My account
4949
✅ Created secret! (ID: 13bc7498c6374a4e9d13be091c3c65f1)
5050
```
@@ -93,7 +93,6 @@ Refer to [manage account secrets](/secrets-store/manage-secrets/) for further op
9393

9494
To bind an account secret to your Worker, you must have one of the following [roles within your Cloudflare account](/secrets-store/access-control/):
9595
- Super Administrator
96-
- Secrets Store Admin
9796
- Secrets Store Deployer
9897

9998
### Via Wrangler
@@ -108,7 +107,7 @@ To bind an account secret to your Worker, you must have one of the following [ro
108107
```toml
109108
main = "./src/index.js"
110109
secrets_store_secrets = [
111-
{ binding = "MY_SECRETS_STORE_SECRET", store_id= '<STORE_ID>', secret_name = "<MY_SECRET_NAME>" }
110+
{ binding = "MY_SECRETS_STORE_SECRET", store_id= "<STORE_ID>", secret_name = "<MY_SECRET_NAME>" }
112111
]
113112
```
114113

@@ -131,39 +130,26 @@ secrets_store_secrets = [
131130

132131
## 3. Access the secret on the `env` object
133132

134-
[Bindings](/workers/runtime-apis/bindings/) are located on the `env` object, which can be accessed in several ways. Two examples are presented below. For further options, refer to the [Workers documentation](/workers/runtime-apis/bindings/#how-to-access-env).
133+
[Bindings](/workers/runtime-apis/bindings/) are located on the `env` object. To access the secret you first need an asynchronous call.
135134

136-
### Import `env` from `cloudflare:workers`
137135

138-
```js
139-
import { env } from "cloudflare:workers";
140-
import ApiClient from "example-api-client";
141-
142-
// MY_SECRETS_STORE_SECRET is now usable in top-level scope
143-
let apiClient = ApiClient.new({ apiKey: env.MY_SECRETS_STORE_SECRET });
144-
145-
export default {
146-
fetch(req) {
147-
// you can use apiClient configured before any request is handled
148-
},
149-
};
150-
```
151-
152-
### Pass `env` as an argument to `fetch`
136+
### Call `get()` on the binding variable
153137

154138
```js
155139
export default {
156140
async fetch(request, env) {
157141
// Example of using the secret safely in an API request
158-
let response = await fetch("https://api.example.com/data", {
159-
headers: { "Authorization": `Bearer ${env.MY_SECRETS_STORE_SECRET}` },
142+
const APIkey = await env.MY_SECRETS_STORE_SECRET.get()
143+
144+
const response = await fetch("https://api.example.com/data", {
145+
headers: { "Authorization": `Bearer ${APIKey}` },
160146
});
161147

162148
if (!response.ok) {
163149
return new Response("Failed to fetch data", { status: response.status });
164150
}
165151

166-
let data = await response.json();
152+
const data = await response.json();
167153
return new Response(JSON.stringify(data), {
168154
headers: { "Content-Type": "application/json" },
169155
});

0 commit comments

Comments
 (0)