环境变量
Manage sensitive data securely across environments.
默认的秘密 #
🌐 Default secrets
边缘函数默认可以访问这些密钥:
🌐 Edge Functions have access to these secrets by default:
SUPABASE_URL:你 Supabase 项目的 API 网关SUPABASE_DB_URL:你 Postgres 数据库的 URL。你可以用它直接连接到你的数据库SUPABASE_PUBLISHABLE_KEYS:publishable键的 JSON 字典用于你的 Supabase API。当你启用行级安全(Row Level Security)时,在浏览器中使用这是安全的SUPABASE_SECRET_KEYS:secret键是你的 Supabase API 的 JSON 字典。这在 Edge Functions 中使用是安全的,但绝不应该在浏览器中使用。这个键可以绕过行级安全(Row Level Security)SUPABASE_JWKS:用于验证用户 JWT 的 JSON Web 密钥集。同样的值在https://<project-ref>.supabase.co/auth/v1/.well-known/jwks.json提供
旧钥匙:
🌐 Legacy keys:
SUPABASE_ANON_KEY:你的 Supabase API 的anon密钥。当你启用了行级安全(Row Level Security)时,在浏览器中使用它是安全的。SUPABASE_SERVICE_ROLE_KEY:service_role是你 Supabase API 的密钥。它在 Edge Functions 中使用是安全的,但绝对不要在浏览器中使用。这个密钥可以绕过行级安全。
在托管环境中,函数可以访问以下环境变量:
🌐 In a hosted environment, functions have access to the following environment variables:
SB_REGION:区域函数被调用了SB_EXECUTION_ID:函数实例的 UUID(隔离)DENO_DEPLOYMENT_ID:函数代码版本({project_ref}_{function_id}_{version})
访问环境变量 #
🌐 Accessing environment variables
你可以使用 Deno 内置的处理器访问环境变量,只需传入你想要访问的环境变量的名称。
🌐 You can access environment variables using Deno's built-in handler, and passing it the name of the environment variable you’d like to access.
1Deno.env.get('NAME_OF_SECRET')例如,在一个函数中:
🌐 For example, in a function:
1import { createClient } from 'npm:@supabase/supabase-js@2'23const SUPABASE_PUBLISHABLE_KEYS = JSON.parse(Deno.env.get('SUPABASE_PUBLISHABLE_KEYS')!)45// For user-facing operations (respects RLS)6const supabase = createClient(7 Deno.env.get('SUPABASE_URL')!,8 // If you want to use a different api key, change 'default' to your preferred key name9 SUPABASE_PUBLISHABLE_KEYS['default']10)1112const SUPABASE_SECRET_KEYS = JSON.parse(Deno.env.get('SUPABASE_SECRET_KEYS')!)13// For admin operations (bypasses RLS)14const supabaseAdmin = createClient(15 Deno.env.get('SUPABASE_URL')!,16 // If you want to use a different api key, change 'default' to your preferred key name17 SUPABASE_SECRET_KEYS['default']18)当地的秘密 #
🌐 Local secrets
在开发中,你可以通过两种方式加载环境变量:
🌐 In development, you can load environment variables in two ways:
- 通过放置在
supabase/functions/.env的.env文件,它会在supabase start自动加载 - 通过
supabase functions serve的--env-file选项。这样你就可以使用像.env.local这样的自定义文件名来区分不同的环境。
1supabase functions serve --env-file .env.local绝不要把你的 .env 文件提交到 Git!相反,把这个文件的路径添加到你的 .gitignore 中。
🌐 Never check your .env files into Git! Instead, add the path to this file to your .gitignore.
我们可以通过 Deno 的处理器自动访问我们 Edge Functions 中的秘密
🌐 We can automatically access the secrets in our Edge Functions through Deno’s handler
1const secretKey = Deno.env.get('STRIPE_SECRET_KEY')现在我们可以在本地调用我们的函数。如果你使用的是默认的 .env 文件,位于 supabase/functions/.env,它会被自动加载:
🌐 Now we can invoke our function locally. If you're using the default .env file at supabase/functions/.env, it's automatically loaded:
1supabase functions serve hello-world或者你可以使用 --env-file 标志指定一个自定义的 .env 文件:
🌐 Or you can specify a custom .env file with the --env-file flag:
1supabase functions serve hello-world --env-file .env.local这对于管理不同的环境(开发、测试等)很有用。
🌐 This is useful for managing different environments (development, staging, etc.).
生产秘密 #
🌐 Production secrets
你还需要为生产环境的 Edge Functions 设置密钥。你可以通过控制面板或使用 CLI 来完成。
🌐 You will also need to set secrets for your production Edge Functions. You can do this via the Dashboard or using the CLI.
使用仪表板:
- 在你的仪表板中访问 Edge Function 密钥管理 页面。
- 添加你的秘密的键和值,然后点击保存

注意,你可以一次粘贴多个秘密。
🌐 Note that you can paste multiple secrets at a time.
使用命令行接口
你可以创建一个 .env 文件来帮助把你的秘密部署到生产环境
🌐 You can create a .env file to help deploy your secrets to production
1# .env2STRIPE_SECRET_KEY=sk_live_...绝不要把你的 .env 文件提交到 Git!相反,把这个文件的路径添加到你的 .gitignore 中。
🌐 Never check your .env files into Git! Instead, add the path to this file to your .gitignore.
你可以使用 supabase secrets set 把 .env 文件中的所有秘密推送到你的远程项目。这也会让仪表板上显示环境信息。
🌐 You can push all the secrets from the .env file to your remote project using supabase secrets set. This makes the environment visible in the dashboard as well.
1supabase secrets set --env-file .env或者,这个命令也允许你单独设置生产环境的密钥,而不是把它们存储在 .env 文件里。
🌐 Alternatively, this command also allows you to set production secrets individually rather than storing them in a .env file.
1supabase secrets set STRIPE_SECRET_KEY=sk_live_...要查看你远程设置的所有秘密,你可以使用 supabase secrets list
🌐 To see all the secrets which you have set remotely, you can use supabase secrets list
1supabase secrets list设置好你的密钥后,不需要重新部署。它们会立即在你的函数中可用。
🌐 You don't need to re-deploy after setting your secrets. They're available immediately in your functions.