supabase-edge-functions by nice-wolf-studio/claude-code-supabase-skills
npx skills add https://github.com/nice-wolf-studio/claude-code-supabase-skills --skill supabase-edge-functions此技能提供用于处理 Supabase Edge Functions 的操作 - 这些是在 Deno Deploy 上运行的无服务器 TypeScript/JavaScript 函数。可用于调用函数、部署代码和管理函数生命周期。
必需的环境变量:
export SUPABASE_URL="https://your-project.supabase.co"
export SUPABASE_KEY="your-anon-or-service-role-key"
必需的工具:
supabase 命令)安装 Supabase CLI:
# macOS
brew install supabase/tap/supabase
# Linux
curl -fsSL https://github.com/supabase/cli/releases/latest/download/supabase_linux_amd64.tar.gz | tar -xz
sudo mv supabase /usr/local/bin/
# Windows (PowerShell)
scoop bucket add supabase https://github.com/supabase/scoop-bucket.git
scoop install supabase
辅助脚本: 此技能使用共享的 Supabase API 辅助脚本来调用函数:
source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
使用 POST 调用函数:
source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"
FUNCTION_NAME="hello-world"
supabase_post "/functions/v1/${FUNCTION_NAME}" '{
"name": "Alice"
}'
使用 GET 调用:
source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"
FUNCTION_NAME="get-data"
supabase_get "/functions/v1/${FUNCTION_NAME}?id=123"
传递自定义请求头:
FUNCTION_NAME="authenticated-function"
USER_TOKEN="user-access-token"
curl -s -X POST \
"${SUPABASE_URL}/functions/v1/${FUNCTION_NAME}" \
-H "apikey: ${SUPABASE_KEY}" \
-H "Authorization: Bearer ${USER_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"action": "process"}'
以认证用户身份调用函数:
source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"
FUNCTION_NAME="user-profile"
ACCESS_TOKEN="user-jwt-token"
curl -s -X POST \
"${SUPABASE_URL}/functions/v1/${FUNCTION_NAME}" \
-H "apikey: ${SUPABASE_KEY}" \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d '{}'
创建新的边缘函数:
# 导航到您的 Supabase 项目目录
cd /path/to/project
# 创建新函数
supabase functions new my-function
# 这会创建:supabase/functions/my-function/index.ts
基本函数结构:
// supabase/functions/my-function/index.ts
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
serve(async (req) => {
const { name } = await req.json()
const data = {
message: `Hello ${name}!`,
}
return new Response(
JSON.stringify(data),
{ headers: { "Content-Type": "application/json" } },
)
})
带身份验证的函数:
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
serve(async (req) => {
// 从 Authorization 请求头获取 JWT
const authHeader = req.headers.get('Authorization')!
const token = authHeader.replace('Bearer ', '')
// 使用用户的令牌创建 Supabase 客户端
const supabase = createClient(
Deno.env.get('SUPABASE_URL') ?? '',
Deno.env.get('SUPABASE_ANON_KEY') ?? '',
{ global: { headers: { Authorization: authHeader } } }
)
// 获取认证用户
const { data: { user }, error } = await supabase.auth.getUser(token)
if (error || !user) {
return new Response('Unauthorized', { status: 401 })
}
return new Response(
JSON.stringify({ message: `Hello ${user.email}!` }),
{ headers: { "Content-Type": "application/json" } },
)
})
将函数部署到 Supabase:
# 登录到 Supabase (仅首次需要)
supabase login
# 链接到您的项目 (仅首次需要)
supabase link --project-ref your-project-ref
# 部署特定函数
supabase functions deploy my-function
# 使用自定义环境变量部署
supabase functions deploy my-function \
--env-file ./supabase/.env.local
# 部署所有函数
supabase functions deploy
为边缘函数设置密钥:
# 设置单个密钥
supabase secrets set MY_SECRET_KEY=value123
# 从文件设置多个密钥
# 创建 .env 文件:
# API_KEY=abc123
# DATABASE_URL=postgres://...
supabase secrets set --env-file .env
# 列出密钥 (仅名称,不显示值)
supabase secrets list
# 取消设置密钥
supabase secrets unset MY_SECRET_KEY
在本地运行函数:
# 启动本地 Supabase (包含边缘函数)
supabase start
# 在本地提供函数服务
supabase functions serve
# 提供特定函数服务
supabase functions serve my-function --env-file ./supabase/.env.local
# 调用本地函数
curl http://localhost:54321/functions/v1/my-function \
-H "Authorization: Bearer ${SUPABASE_KEY}" \
-d '{"name": "test"}'
移除已部署的函数:
# 从 Supabase 仪表板或使用 SQL 删除函数
# 注意:没有直接的 CLI 命令可以删除,必须使用仪表板
# 移除本地函数文件
rm -rf supabase/functions/my-function
#!/bin/bash
source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"
FUNCTION_NAME="process-data"
response=$(supabase_post "/functions/v1/${FUNCTION_NAME}" '{
"action": "calculate",
"values": [1, 2, 3, 4, 5]
}')
if [[ $? -eq 0 ]]; then
result=$(echo "$response" | jq -r '.result')
echo "Function result: $result"
else
echo "Function invocation failed"
exit 1
fi
#!/bin/bash
source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"
FUNCTION_NAME="send-email"
RECIPIENTS=("alice@example.com" "bob@example.com" "charlie@example.com")
for email in "${RECIPIENTS[@]}"; do
echo "Processing $email..."
supabase_post "/functions/v1/${FUNCTION_NAME}" '{
"to": "'"$email"'",
"subject": "Hello",
"body": "Test message"
}'
echo "✓ Sent to $email"
done
#!/bin/bash
source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"
invoke_with_retry() {
local function_name="$1"
local payload="$2"
local max_retries=3
local retry_count=0
while [[ $retry_count -lt $max_retries ]]; do
if response=$(supabase_post "/functions/v1/${function_name}" "$payload" 2>&1); then
echo "$response"
return 0
else
retry_count=$((retry_count + 1))
echo "Retry $retry_count/$max_retries..." >&2
sleep 2
fi
done
echo "Function failed after $max_retries retries" >&2
return 1
}
# Use it
invoke_with_retry "my-function" '{"action": "process"}'
#!/bin/bash
# deploy-function.sh
FUNCTION_NAME="${1:-my-function}"
echo "Deploying function: $FUNCTION_NAME"
# Validate function exists
if [[ ! -d "supabase/functions/$FUNCTION_NAME" ]]; then
echo "Error: Function $FUNCTION_NAME not found"
exit 1
fi
# Deploy
if supabase functions deploy "$FUNCTION_NAME"; then
echo "✓ Deployed successfully"
# Test invocation
echo "Testing function..."
response=$(curl -s -X POST \
"${SUPABASE_URL}/functions/v1/${FUNCTION_NAME}" \
-H "apikey: ${SUPABASE_KEY}" \
-H "Content-Type: application/json" \
-d '{}')
echo "Test response: $response"
else
echo "✗ Deployment failed"
exit 1
fi
# 查看函数日志 (需要 Supabase CLI)
supabase functions logs my-function
# 实时跟踪日志
supabase functions logs my-function --follow
# 按级别过滤日志
supabase functions logs my-function --level error
# 查看特定时间的日志
supabase functions logs my-function --since 1h
// supabase/functions/get-user-data/index.ts
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
serve(async (req) => {
const supabase = createClient(
Deno.env.get('SUPABASE_URL') ?? '',
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? ''
)
const { userId } = await req.json()
const { data, error } = await supabase
.from('users')
.select('*')
.eq('id', userId)
.single()
if (error) {
return new Response(JSON.stringify({ error: error.message }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
})
}
return new Response(JSON.stringify(data), {
headers: { 'Content-Type: 'application/json' }
})
})
// supabase/functions/fetch-weather/index.ts
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
serve(async (req) => {
const { city } = await req.json()
const apiKey = Deno.env.get('WEATHER_API_KEY')
const response = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}`
)
const data = await response.json()
return new Response(JSON.stringify(data), {
headers: { 'Content-Type': 'application/json' }
})
})
// supabase/functions/daily-cleanup/index.ts
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
serve(async (req) => {
// 验证请求来自 Supabase Cron
const authHeader = req.headers.get('Authorization')
if (authHeader !== `Bearer ${Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')}`) {
return new Response('Unauthorized', { status: 401 })
}
const supabase = createClient(
Deno.env.get('SUPABASE_URL') ?? '',
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? ''
)
// 删除旧记录
const { data, error } = await supabase
.from('logs')
.delete()
.lt('created_at', new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString())
return new Response(JSON.stringify({ deleted: data?.length ?? 0 }), {
headers: { 'Content-Type': 'application/json' }
})
})
在 Supabase 仪表板中设置 cron 任务:
-- 在 SQL 编辑器中,创建 pg_cron 任务:
select cron.schedule(
'daily-cleanup',
'0 2 * * *', -- 每天凌晨 2 点运行
$$
select
net.http_post(
url := 'https://your-project.supabase.co/functions/v1/daily-cleanup',
headers := '{"Content-Type": "application/json", "Authorization": "Bearer YOUR_SERVICE_ROLE_KEY"}'::jsonb,
body := '{}'::jsonb
) as request_id;
$$
);
# 启动本地环境
supabase start
# 提供函数服务
supabase functions serve my-function
# 使用 curl 测试
curl http://localhost:54321/functions/v1/my-function \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." \
-d '{"test": "data"}'
#!/bin/bash
# test-function.sh
FUNCTION_NAME="$1"
TEST_CASES_FILE="$2"
if [[ ! -f "$TEST_CASES_FILE" ]]; then
echo "Test cases file not found"
exit 1
fi
echo "Testing function: $FUNCTION_NAME"
while IFS= read -r test_case; do
echo "Test case: $test_case"
response=$(curl -s -X POST \
"${SUPABASE_URL}/functions/v1/${FUNCTION_NAME}" \
-H "apikey: ${SUPABASE_KEY}" \
-H "Content-Type: application/json" \
-d "$test_case")
echo "Response: $response"
echo "---"
done < "$TEST_CASES_FILE"
函数错误返回 HTTP 状态码:
| 状态码 | 含义 |
|---|---|
| 200 | 成功 |
| 400 | 错误请求 (无效输入) |
| 401 | 未授权 (无效/缺少身份验证) |
| 403 | 禁止访问 (权限不足) |
| 500 | 内部服务器错误 (函数崩溃) |
| 504 | 网关超时 (函数执行时间过长) |
超时限制: 边缘函数有 2 秒的 CPU 时间限制和 150 秒的挂钟超时限制。
Promise.all() 进行并发操作每周安装数
163
代码仓库
GitHub 星标数
10
首次出现
2026年1月22日
安全审计
安装于
opencode139
gemini-cli138
codex132
cursor127
github-copilot122
claude-code120
This skill provides operations for working with Supabase Edge Functions - serverless TypeScript/JavaScript functions that run on Deno Deploy. Use for invoking functions, deploying code, and managing function lifecycles.
Required environment variables:
export SUPABASE_URL="https://your-project.supabase.co"
export SUPABASE_KEY="your-anon-or-service-role-key"
Required tools:
supabase command)Install Supabase CLI:
# macOS
brew install supabase/tap/supabase
# Linux
curl -fsSL https://github.com/supabase/cli/releases/latest/download/supabase_linux_amd64.tar.gz | tar -xz
sudo mv supabase /usr/local/bin/
# Windows (PowerShell)
scoop bucket add supabase https://github.com/supabase/scoop-bucket.git
scoop install supabase
Helper script: This skill uses the shared Supabase API helper for invoking functions:
source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"
Invoke a function with POST:
source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"
FUNCTION_NAME="hello-world"
supabase_post "/functions/v1/${FUNCTION_NAME}" '{
"name": "Alice"
}'
Invoke with GET:
source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"
FUNCTION_NAME="get-data"
supabase_get "/functions/v1/${FUNCTION_NAME}?id=123"
Pass custom headers:
FUNCTION_NAME="authenticated-function"
USER_TOKEN="user-access-token"
curl -s -X POST \
"${SUPABASE_URL}/functions/v1/${FUNCTION_NAME}" \
-H "apikey: ${SUPABASE_KEY}" \
-H "Authorization: Bearer ${USER_TOKEN}" \
-H "Content-Type: application/json" \
-d '{"action": "process"}'
Invoke function as authenticated user:
source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"
FUNCTION_NAME="user-profile"
ACCESS_TOKEN="user-jwt-token"
curl -s -X POST \
"${SUPABASE_URL}/functions/v1/${FUNCTION_NAME}" \
-H "apikey: ${SUPABASE_KEY}" \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
-d '{}'
Create a new edge function:
# Navigate to your Supabase project directory
cd /path/to/project
# Create new function
supabase functions new my-function
# This creates: supabase/functions/my-function/index.ts
Basic function structure:
// supabase/functions/my-function/index.ts
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
serve(async (req) => {
const { name } = await req.json()
const data = {
message: `Hello ${name}!`,
}
return new Response(
JSON.stringify(data),
{ headers: { "Content-Type": "application/json" } },
)
})
Function with authentication:
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
serve(async (req) => {
// Get JWT from Authorization header
const authHeader = req.headers.get('Authorization')!
const token = authHeader.replace('Bearer ', '')
// Create Supabase client with user's token
const supabase = createClient(
Deno.env.get('SUPABASE_URL') ?? '',
Deno.env.get('SUPABASE_ANON_KEY') ?? '',
{ global: { headers: { Authorization: authHeader } } }
)
// Get authenticated user
const { data: { user }, error } = await supabase.auth.getUser(token)
if (error || !user) {
return new Response('Unauthorized', { status: 401 })
}
return new Response(
JSON.stringify({ message: `Hello ${user.email}!` }),
{ headers: { "Content-Type": "application/json" } },
)
})
Deploy a function to Supabase:
# Login to Supabase (first time only)
supabase login
# Link to your project (first time only)
supabase link --project-ref your-project-ref
# Deploy specific function
supabase functions deploy my-function
# Deploy with custom environment variables
supabase functions deploy my-function \
--env-file ./supabase/.env.local
# Deploy all functions
supabase functions deploy
Set secrets for edge functions:
# Set individual secret
supabase secrets set MY_SECRET_KEY=value123
# Set multiple secrets from file
# Create .env file:
# API_KEY=abc123
# DATABASE_URL=postgres://...
supabase secrets set --env-file .env
# List secrets (names only, not values)
supabase secrets list
# Unset secret
supabase secrets unset MY_SECRET_KEY
Run functions locally:
# Start local Supabase (includes edge functions)
supabase start
# Serve functions locally
supabase functions serve
# Serve specific function
supabase functions serve my-function --env-file ./supabase/.env.local
# Invoke local function
curl http://localhost:54321/functions/v1/my-function \
-H "Authorization: Bearer ${SUPABASE_KEY}" \
-d '{"name": "test"}'
Remove a deployed function:
# Delete function from Supabase dashboard or using SQL
# Note: No direct CLI command to delete, must use dashboard
# Remove local function file
rm -rf supabase/functions/my-function
#!/bin/bash
source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"
FUNCTION_NAME="process-data"
response=$(supabase_post "/functions/v1/${FUNCTION_NAME}" '{
"action": "calculate",
"values": [1, 2, 3, 4, 5]
}')
if [[ $? -eq 0 ]]; then
result=$(echo "$response" | jq -r '.result')
echo "Function result: $result"
else
echo "Function invocation failed"
exit 1
fi
#!/bin/bash
source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"
FUNCTION_NAME="send-email"
RECIPIENTS=("alice@example.com" "bob@example.com" "charlie@example.com")
for email in "${RECIPIENTS[@]}"; do
echo "Processing $email..."
supabase_post "/functions/v1/${FUNCTION_NAME}" '{
"to": "'"$email"'",
"subject": "Hello",
"body": "Test message"
}'
echo "✓ Sent to $email"
done
#!/bin/bash
source "$(dirname "${BASH_SOURCE[0]}")/../../scripts/supabase-api.sh"
invoke_with_retry() {
local function_name="$1"
local payload="$2"
local max_retries=3
local retry_count=0
while [[ $retry_count -lt $max_retries ]]; do
if response=$(supabase_post "/functions/v1/${function_name}" "$payload" 2>&1); then
echo "$response"
return 0
else
retry_count=$((retry_count + 1))
echo "Retry $retry_count/$max_retries..." >&2
sleep 2
fi
done
echo "Function failed after $max_retries retries" >&2
return 1
}
# Use it
invoke_with_retry "my-function" '{"action": "process"}'
#!/bin/bash
# deploy-function.sh
FUNCTION_NAME="${1:-my-function}"
echo "Deploying function: $FUNCTION_NAME"
# Validate function exists
if [[ ! -d "supabase/functions/$FUNCTION_NAME" ]]; then
echo "Error: Function $FUNCTION_NAME not found"
exit 1
fi
# Deploy
if supabase functions deploy "$FUNCTION_NAME"; then
echo "✓ Deployed successfully"
# Test invocation
echo "Testing function..."
response=$(curl -s -X POST \
"${SUPABASE_URL}/functions/v1/${FUNCTION_NAME}" \
-H "apikey: ${SUPABASE_KEY}" \
-H "Content-Type: application/json" \
-d '{}')
echo "Test response: $response"
else
echo "✗ Deployment failed"
exit 1
fi
# View function logs (requires Supabase CLI)
supabase functions logs my-function
# Follow logs in real-time
supabase functions logs my-function --follow
# Filter logs by level
supabase functions logs my-function --level error
# View logs from specific time
supabase functions logs my-function --since 1h
// supabase/functions/get-user-data/index.ts
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
serve(async (req) => {
const supabase = createClient(
Deno.env.get('SUPABASE_URL') ?? '',
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? ''
)
const { userId } = await req.json()
const { data, error } = await supabase
.from('users')
.select('*')
.eq('id', userId)
.single()
if (error) {
return new Response(JSON.stringify({ error: error.message }), {
status: 400,
headers: { 'Content-Type': 'application/json' }
})
}
return new Response(JSON.stringify(data), {
headers: { 'Content-Type': 'application/json' }
})
})
// supabase/functions/fetch-weather/index.ts
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
serve(async (req) => {
const { city } = await req.json()
const apiKey = Deno.env.get('WEATHER_API_KEY')
const response = await fetch(
`https://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${apiKey}`
)
const data = await response.json()
return new Response(JSON.stringify(data), {
headers: { 'Content-Type': 'application/json' }
})
})
// supabase/functions/daily-cleanup/index.ts
import { serve } from "https://deno.land/std@0.168.0/http/server.ts"
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
serve(async (req) => {
// Verify request is from Supabase Cron
const authHeader = req.headers.get('Authorization')
if (authHeader !== `Bearer ${Deno.env.get('SUPABASE_SERVICE_ROLE_KEY')}`) {
return new Response('Unauthorized', { status: 401 })
}
const supabase = createClient(
Deno.env.get('SUPABASE_URL') ?? '',
Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? ''
)
// Delete old records
const { data, error } = await supabase
.from('logs')
.delete()
.lt('created_at', new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString())
return new Response(JSON.stringify({ deleted: data?.length ?? 0 }), {
headers: { 'Content-Type': 'application/json' }
})
})
Set up cron job in Supabase Dashboard:
-- In SQL Editor, create pg_cron job:
select cron.schedule(
'daily-cleanup',
'0 2 * * *', -- Run at 2 AM daily
$$
select
net.http_post(
url := 'https://your-project.supabase.co/functions/v1/daily-cleanup',
headers := '{"Content-Type": "application/json", "Authorization": "Bearer YOUR_SERVICE_ROLE_KEY"}'::jsonb,
body := '{}'::jsonb
) as request_id;
$$
);
# Start local environment
supabase start
# Serve function
supabase functions serve my-function
# Test with curl
curl http://localhost:54321/functions/v1/my-function \
-H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." \
-d '{"test": "data"}'
#!/bin/bash
# test-function.sh
FUNCTION_NAME="$1"
TEST_CASES_FILE="$2"
if [[ ! -f "$TEST_CASES_FILE" ]]; then
echo "Test cases file not found"
exit 1
fi
echo "Testing function: $FUNCTION_NAME"
while IFS= read -r test_case; do
echo "Test case: $test_case"
response=$(curl -s -X POST \
"${SUPABASE_URL}/functions/v1/${FUNCTION_NAME}" \
-H "apikey: ${SUPABASE_KEY}" \
-H "Content-Type: application/json" \
-d "$test_case")
echo "Response: $response"
echo "---"
done < "$TEST_CASES_FILE"
Function errors return HTTP status codes:
| Status | Meaning |
|---|---|
| 200 | Success |
| 400 | Bad request (invalid input) |
| 401 | Unauthorized (invalid/missing auth) |
| 403 | Forbidden (insufficient permissions) |
| 500 | Internal server error (function crashed) |
| 504 | Gateway timeout (function took too long) |
Timeout limit: Edge functions have a 2-second CPU time limit and 150-second wall clock timeout.
Promise.all() for concurrent operationsWeekly Installs
163
Repository
GitHub Stars
10
First Seen
Jan 22, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykWarn
Installed on
opencode139
gemini-cli138
codex132
cursor127
github-copilot122
claude-code120