diff --git a/src/content/docs/workers/configuration/secrets.mdx b/src/content/docs/workers/configuration/secrets.mdx index b217268956ad9af..d3dd12eef8b3b86 100644 --- a/src/content/docs/workers/configuration/secrets.mdx +++ b/src/content/docs/workers/configuration/secrets.mdx @@ -11,6 +11,27 @@ import { Render } from "~/components"; 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/). +## Access your secrets with Workers + +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: + + +```js title="index.js" +import postgres from "postgres"; + +export default { + async fetch(request, env, ctx) { + const sql = postgres(env.DB_CONNECTION_STRING); + + const result = await sql`SELECT * FROM products;`; + + return new Response(JSON.stringify(result), { + headers: { "Content-Type": "application/json" }, + }); + }, +}; +``` + :::note[Secrets Store (beta)] 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/). :::