Skip to content
Edge Functions

环境变量

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_KEYSpublishable 键的 JSON 字典用于你的 Supabase API。当你启用行级安全(Row Level Security)时,在浏览器中使用这是安全的
  • SUPABASE_SECRET_KEYSsecret 键是你的 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_KEYservice_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.

1
Deno.env.get('NAME_OF_SECRET')

例如,在一个函数中:

🌐 For example, in a function:

1
import { createClient } from 'npm:@supabase/supabase-js@2'
2
3
const SUPABASE_PUBLISHABLE_KEYS = JSON.parse(Deno.env.get('SUPABASE_PUBLISHABLE_KEYS')!)
4
5
// For user-facing operations (respects RLS)
6
const supabase = createClient(
7
Deno.env.get('SUPABASE_URL')!,
8
// If you want to use a different api key, change 'default' to your preferred key name
9
SUPABASE_PUBLISHABLE_KEYS['default']
10
)
11
12
const SUPABASE_SECRET_KEYS = JSON.parse(Deno.env.get('SUPABASE_SECRET_KEYS')!)
13
// For admin operations (bypasses RLS)
14
const supabaseAdmin = createClient(
15
Deno.env.get('SUPABASE_URL')!,
16
// If you want to use a different api key, change 'default' to your preferred key name
17
SUPABASE_SECRET_KEYS['default']
18
)

当地的秘密 #

🌐 Local secrets

在开发中,你可以通过两种方式加载环境变量:

🌐 In development, you can load environment variables in two ways:

  1. 通过放置在 supabase/functions/.env.env 文件,它会在 supabase start 自动加载
  2. 通过 supabase functions serve--env-file 选项。这样你就可以使用像 .env.local 这样的自定义文件名来区分不同的环境。
1
supabase functions serve --env-file .env.local

我们可以通过 Deno 的处理器自动访问我们 Edge Functions 中的秘密

🌐 We can automatically access the secrets in our Edge Functions through Deno’s handler

1
const 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:

1
supabase functions serve hello-world

或者你可以使用 --env-file 标志指定一个自定义的 .env 文件:

🌐 Or you can specify a custom .env file with the --env-file flag:

1
supabase 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.

使用仪表板

  1. 在你的仪表板中访问 Edge Function 密钥管理 页面。
  2. 添加你的秘密的键和值,然后点击保存
Edge Functions Secrets Management

注意,你可以一次粘贴多个秘密。

🌐 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
# .env
2
STRIPE_SECRET_KEY=sk_live_...

你可以使用 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.

1
supabase 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.

1
supabase 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

1
supabase secrets list