Secrets, Environment Variables, and Scheduled Functions
Securely manage API keys in Edge Functions with secrets, read environment variables, and run functions on a schedule with cron.
Secrets, Environment Variables, and Scheduled Functions is a free Supabase Backend as a Service lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Supabase Backend as a Service learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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_123Reading 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_KEYValidating 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.
Frequently asked questions
Is the “Secrets, Environment Variables, and Scheduled Functions” lesson free?
Yes — the full text of “Secrets, Environment Variables, and Scheduled Functions” is free to read here on the web, and the Supabase Backend as a Service course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Supabase Backend as a Service course, upgrade to CoddyKit PRO.
What will I learn in “Secrets, Environment Variables, and Scheduled Functions”?
Securely manage API keys in Edge Functions with secrets, read environment variables, and run functions on a schedule with cron. You practise Supabase Backend as a Service with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Supabase Backend as a Service?
No prior experience is required. Supabase Backend as a Service on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Secrets, Environment Variables, and Scheduled Functions” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Supabase Backend as a Service lesson?
Yes. Every Supabase Backend as a Service lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to Edge Functions
- Deploying Your First Function
- Integrating Functions with Your App
- Secrets, Environment Variables, and Scheduled Functions