JSTAcademy
0 XP
Dashboard
Crash Courses
Edge Functions
10 min
PhD+200 XP
Crash Courses · PhD

Edge Functions

Run server-side logic at the edge with Supabase Edge Functions (Deno).
10 min read+200 XP on completionCert: Supabase Crash Course
Tap any word in the text below to start reading from there.

Edge Functions

Supabase Edge Functions are Deno TypeScript functions deployed globally. Use for webhook handling, AI calls, email sending, or any server logic that shouldn't be in the Next.js app.

Create and Deploy

# Create
supabase functions new send-welcome-email

# Run locally
supabase functions serve

# Deploy
supabase functions deploy send-welcome-email

# Set secrets
supabase secrets set RESEND_API_KEY=re_...

Edge Function

// supabase/functions/send-welcome-email/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'

const corsHeaders = {
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
}

serve(async (req) => {
  if (req.method === 'OPTIONS') {
    return new Response(null, { headers: corsHeaders })
  }

  try {
    const { email, name } = await req.json()

    const res = await fetch('https://api.resend.com/emails', {
      method: 'POST',
      headers: {
        Authorization: `Bearer ${Deno.env.get('RESEND_API_KEY')}`,
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        from: 'academy@jsupremetech.online',
        to: email,
        subject: `Welcome to JST Academy, ${name}!`,
        html: `<h1>Welcome!</h1><p>Your crash courses are ready.</p>`,
      }),
    })

    return new Response(JSON.stringify({ success: true }), {
      headers: { ...corsHeaders, 'Content-Type': 'application/json' },
    })
  } catch (err) {
    return new Response(JSON.stringify({ error: err.message }), {
      status: 500,
      headers: { ...corsHeaders, 'Content-Type': 'application/json' },
    })
  }
})

Call from Next.js

const { data, error } = await supabase.functions.invoke('send-welcome-email', {
  body: { email: user.email, name: user.name },
})
0%