Supabase Edge Functions 讓你能夠使用 TypeScript 撰寫伺服器端邏輯,並部署到全球邊緣節點 (Edge Network),擁有極低的延遲。
1 建立 Function
使用 CLI 快速建立一個新的 Function 模板:
supabase functions new hello-world 這會在 supabase/functions/hello-world/index.ts 產生範例程式碼。你可以使用 Deno runtime 的標準 Web API (如 fetch, Request, Response)。
2 部署 Function
撰寫完成後,一樣透過 CLI 進行部署:
# 部署所有 functions
supabase functions deploy# 部署特定 function
supabase functions deploy hello-world部署成功後
你會獲得一個 URL,類似:https://<project_ref>.supabase.co/functions/v1/hello-world
你可以使用 curl 或 Postman 測試這個端點。注意:預設是需要帶 Authorization Bearer token 的,如果要是公開 API,需要另外設定。
3 進階:連接外部 API
如果你需要在 Database 內部 (Postgres Trigger) 呼叫 Edge Functions 或外部 API,建議開啟 pg_net 擴充功能。
如何開啟?
前往 Supabase Dashboard > Database > Extensions,搜尋 pg_net 並啟用。
這允許你在 SQL 中使用 net.http_get 等函式,實現資料庫層級的 Webhook 功能。