ข้อมูลลับ ตัวแปรสภาพแวดล้อม และฟังก์ชันตามกำหนดเวลา
จัดการคีย์ API ใน Edge Functions อย่างปลอดภัยด้วยข้อมูลลับ อ่านค่าตัวแปรสภาพแวดล้อม และเรียกใช้ฟังก์ชันตามกำหนดเวลาด้วย cron
ข้อมูลลับ ตัวแปรสภาพแวดล้อม และฟังก์ชันตามกำหนดเวลา เป็นบทเรียน Supabase Backend as a Service ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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.
คำถามที่พบบ่อย
บทเรียน “ข้อมูลลับ ตัวแปรสภาพแวดล้อม และฟังก์ชันตามกำหนดเวลา” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ข้อมูลลับ ตัวแปรสภาพแวดล้อม และฟังก์ชันตามกำหนดเวลา” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Supabase Backend as a Service ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Supabase Backend as a Service มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ข้อมูลลับ ตัวแปรสภาพแวดล้อม และฟังก์ชันตามกำหนดเวลา”
จัดการคีย์ API ใน Edge Functions อย่างปลอดภัยด้วยข้อมูลลับ อ่านค่าตัวแปรสภาพแวดล้อม และเรียกใช้ฟังก์ชันตามกำหนดเวลาด้วย cron คุณปฏิบัติ Supabase Backend as a Service ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Supabase Backend as a Service หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Supabase Backend as a Service บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ข้อมูลลับ ตัวแปรสภาพแวดล้อม และฟังก์ชันตามกำหนดเวลา” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Supabase Backend as a Service นี้ได้ไหม
ได้ บทเรียน Supabase Backend as a Service ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่ Edge Functions
- การนำฟังก์ชันแรกไปใช้งาน
- การผสานฟังก์ชันเข้ากับแอป
- ข้อมูลลับ ตัวแปรสภาพแวดล้อม และฟังก์ชันตามกำหนดเวลา