Skip to content

Commit 4eadff0

Browse files
Show how to use secrets (#21299)
* Show how to use secrets * Apply suggestions from code review --------- Co-authored-by: Kody Jackson <[email protected]>
1 parent bc309cf commit 4eadff0

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,27 @@ 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+
19+
```js title="index.js"
20+
import postgres from "postgres";
21+
22+
export default {
23+
async fetch(request, env, ctx) {
24+
const sql = postgres(env.DB_CONNECTION_STRING);
25+
26+
const result = await sql`SELECT * FROM products;`;
27+
28+
return new Response(JSON.stringify(result), {
29+
headers: { "Content-Type": "application/json" },
30+
});
31+
},
32+
};
33+
```
34+
1435
:::note[Secrets Store (beta)]
1536
Secrets described on this page are defined and managed on a per-Worker level. If you want to use account-level secrets, refer to [Secrets Store](/secrets-store/). Account-level secrets are configured on your Worker as a [Secrets Store binding](/secrets-store/integrations/workers/).
1637
:::

0 commit comments

Comments
 (0)