Supabase Backend as a Service · 课时

密钥、环境变量与定时函数

使用密钥安全地管理 Edge Functions 中的 API 密钥,读取环境变量,并通过 cron 按计划运行函数。

第 4 / 4 课13 个步骤

密钥、环境变量与定时函数 是 CoddyKit 上的免费 Supabase Backend as a Service 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Supabase Backend as a Service 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Supabase Backend as a Service 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Why Functions Need Secrets

Edge Functions often call third-party APIs that require keys. Hardcoding keys is unsafe, so Supabase provides a secrets manager.

Setting a Secret

Use the CLI to store a secret. It becomes an environment variable inside your functions.

supabase secrets set STRIPE_KEY=sk_live_123

Reading Environment Variables

Inside the function (Deno runtime) read it from Deno.env.

const stripeKey = Deno.env.get('STRIPE_KEY');
if (!stripeKey) throw new Error('Missing STRIPE_KEY');

Built-In Variables

Supabase injects useful defaults like SUPABASE_URL and SUPABASE_SERVICE_ROLE_KEY so functions can talk to your own project securely.

Listing and Removing Secrets

Manage secrets over time with list and unset commands.

supabase secrets list
supabase secrets unset STRIPE_KEY

Validating Config at Startup

Fail fast if a required variable is missing, so misconfiguration is caught early.

function requireEnv(names, env) {
  const missing = names.filter(n => !env[n]);
  if (missing.length) throw new Error('Missing: ' + missing.join(', '));
  return true;
}
console.log(requireEnv(['A'], { A: '1' }));

Why Scheduled Functions?

Some work is recurring: nightly cleanups, sending digests, syncing data. Supabase can invoke an Edge Function on a schedule using cron.

Understanding Cron Syntax

A cron expression has five fields: minute, hour, day-of-month, month, day-of-week.

  • 0 3 * * * = daily at 03:00
  • */15 * * * * = every 15 minutes

Scheduling With pg_cron

Enable the pg_cron extension and schedule a call to your function's HTTP endpoint.

select cron.schedule(
  'nightly-cleanup',
  '0 3 * * *',
  $$ select net.http_post('https://proj.functions.supabase.co/cleanup') $$
);

Idempotent Scheduled Work

Schedulers can fire twice or overlap. Make scheduled functions idempotent so repeated runs do not corrupt data.

Putting It Together

Store keys as secrets, read them via env vars, validate at startup, and automate recurring tasks with cron-scheduled function calls.

Quick Check

Test your understanding of secrets and scheduling.

Recap

You learned to manage secrets, read environment variables with Deno.env, validate config at startup, and run scheduled functions with cron and pg_cron, keeping scheduled work idempotent.

免费开始

用 AI 导师学习 Supabase Backend as a Service — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
11
课程
40

常见问题解答

「密钥、环境变量与定时函数」课时是免费的吗?

是的 — 「密钥、环境变量与定时函数」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Supabase Backend as a Service 课程的其余内容,请升级到 CoddyKit PRO。 Supabase Backend as a Service 课程共包含 4 节课。

「密钥、环境变量与定时函数」这节课中我会学到什么?

使用密钥安全地管理 Edge Functions 中的 API 密钥,读取环境变量,并通过 cron 按计划运行函数。 你通过在浏览器中直接运行的动手代码来练习 Supabase Backend as a Service,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Supabase Backend as a Service 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Supabase Backend as a Service 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「密钥、环境变量与定时函数」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Supabase Backend as a Service 课中编写并运行代码吗?

能。每节 Supabase Backend as a Service 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. Edge Functions 入门
  2. 部署您的第一个函数
  3. 将函数集成到应用中
  4. 密钥、环境变量与定时函数
← 返回 Supabase Backend as a Service