Skip to content

Commit 9d02524

Browse files
authored
Show how to use secrets
1 parent 7c8f41b commit 9d02524

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

src/content/docs/workers/configuration/secrets.mdx

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,28 @@ import { Render } from "~/components";
1111

1212
Secrets are a type of binding that allow you to attach encrypted text values to your Worker. You cannot see secrets after you set them and can only access secrets via [Wrangler](/workers/wrangler/commands/#secret) or programmatically via the [`env` parameter](/workers/runtime-apis/handlers/fetch/#parameters). Secrets are used for storing sensitive information like API keys and auth tokens. Secrets are available on the [`env` parameter](/workers/runtime-apis/handlers/fetch/#parameters) passed to your Worker's [`fetch` event handler](/workers/runtime-apis/handlers/fetch/).
1313

14+
## Access your secrets with Workers
15+
16+
Secrets can be accessed from Workers as you would any other [environment variables](/workers/configuration/environment-variables/). For instance, given a `DB_CONNECTION_STRING` secret, you can access it in your Worker code:
17+
18+
<Tabs> <TabItem label="JavaScript" icon="seti:javascript">
19+
20+
```js
21+
import postgres from "postgres";
22+
23+
export default {
24+
async fetch(request, env, ctx) {
25+
const sql = postgres(env.DB_CONNECTION_STRING);
26+
27+
const result = await sql`SELECT * FROM products;`;
28+
29+
return new Response(JSON.stringify(result), {
30+
headers: { "Content-Type": "application/json" },
31+
});
32+
},
33+
};
34+
```
35+
1436
## Local Development with Secrets
1537

1638
<Render file="secrets-in-dev" />

0 commit comments

Comments
 (0)