From 9d0252456274575f0850ec2d8267d3711c771665 Mon Sep 17 00:00:00 2001 From: Thomas Gauvin <35609369+thomasgauvin@users.noreply.github.com> Date: Tue, 1 Apr 2025 13:53:27 -0400 Subject: [PATCH 1/2] Show how to use secrets --- .../docs/workers/configuration/secrets.mdx | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/content/docs/workers/configuration/secrets.mdx b/src/content/docs/workers/configuration/secrets.mdx index e1330a62cf13377..8507ad4a480248d 100644 --- a/src/content/docs/workers/configuration/secrets.mdx +++ b/src/content/docs/workers/configuration/secrets.mdx @@ -11,6 +11,28 @@ 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 +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" }, + }); + }, +}; +``` + ## Local Development with Secrets From d12c1909e5b20258fa8b9ac4d532611b8ad98be7 Mon Sep 17 00:00:00 2001 From: Kody Jackson Date: Wed, 28 May 2025 14:13:52 -0500 Subject: [PATCH 2/2] Apply suggestions from code review --- src/content/docs/workers/configuration/secrets.mdx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/content/docs/workers/configuration/secrets.mdx b/src/content/docs/workers/configuration/secrets.mdx index 8507ad4a480248d..fea9284ebe94bb3 100644 --- a/src/content/docs/workers/configuration/secrets.mdx +++ b/src/content/docs/workers/configuration/secrets.mdx @@ -15,9 +15,8 @@ Secrets are a type of binding that allow you to attach encrypted text values to 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 +```js title="index.js" import postgres from "postgres"; export default {