シークレット、環境変数、スケジュール実行関数
Edge Functionsでシークレットを使ってAPIキーを安全に管理し、環境変数を読み取り、cronで関数をスケジュール実行します。
「シークレット、環境変数、スケジュール実行関数」はCoddyKit上の無料Supabase Backend as a Serviceレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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_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.
よくある質問
「シークレット、環境変数、スケジュール実行関数」レッスンは無料ですか?
はい。「シークレット、環境変数、スケジュール実行関数」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Supabase Backend as a Serviceコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Supabase Backend as a Serviceコースには全4レッスンが含まれています。
「シークレット、環境変数、スケジュール実行関数」で何を学びますか?
Edge Functionsでシークレットを使ってAPIキーを安全に管理し、環境変数を読み取り、cronで関数をスケジュール実行します。 ブラウザで直接実行するハンズオンコードでSupabase Backend as a Serviceを演習し、24時間対応の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フィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Edge Functions入門
- 最初のFunctionをデプロイする
- Functionsとアプリの統合
- シークレット、環境変数、スケジュール実行関数