📚Academy
likeone
online

Edge Functions

Edge functions are serverless code that runs close to your users. Write once, deploy globally, pay per invocation. No servers to manage, no infrastructure to maintain. This is your backend.

What Are Edge Functions?

Think of edge functions as mini-programs that live on the internet. Instead of running on your laptop or a single server, they run on servers spread around the world, close to wherever your users are. When someone visits your app from Tokyo, the edge function runs in Tokyo. From London? It runs in London. This makes everything faster.

You do not manage servers, install software, or worry about scaling. You write a small function, deploy it with one command, and it is available to the whole world instantly. You only pay when someone actually uses it.

🌐
Global
Runs close to users — Tokyo users get Tokyo servers automatically
Deno Runtime
Modern TypeScript runtime — faster cold starts, better security than Node.js
💰
Pay Per Use
500K invocations free on Pro plan — most projects never exceed this

Deno vs. Node.js: Why Supabase Chose Deno

Deno is a modern JavaScript/TypeScript runtime — the engine that runs your code on the server. If you have heard of Node.js, Deno is its newer, more secure sibling. Supabase chose Deno for three reasons:

1
Faster Cold Starts

Edge functions spin up on demand. The first request after being idle is a "cold start" (~100ms). Deno initializes faster than Node.js, keeping that delay minimal.

2
Better Security Defaults

Deno requires explicit permission for file, network, and environment access. A malicious package cannot silently read your filesystem or make network calls without your code granting permission.

3
Native TypeScript

Deno runs TypeScript natively — no build step, no tsconfig, no compilation. Write .ts files and deploy. The same JavaScript/TypeScript you already know works in Deno.

Challenge 1: Hello World

Every API you have ever used — weather apps, payment systems, social media feeds — works by sending and receiving JSON responses. This first challenge teaches you the most fundamental skill in backend development: making a function that responds when someone calls it.

TypeScript — supabase/functions/hello/index.ts
// The simplest possible edge function
// Deno.serve is the modern way to create an HTTP server in Deno
Deno.serve(async (req) => {
  return new Response(
    JSON.stringify({ message: "Hello from the edge!" }),
    { headers: { "Content-Type": "application/json" } }
  )
})
Terminal — Deploy and test
# Deploy to Supabase
supabase functions deploy hello --project-ref <your-ref>

# Test it
curl https://<your-ref>.supabase.co/functions/v1/hello
# → {"message":"Hello from the edge!"}

Challenge 2: JSON API (Read User Input)

Challenge 1 always returned the same thing. Real APIs need to read what the user sends and respond differently. This is how login forms, search bars, and checkout buttons work — your frontend sends data, and your edge function processes it.

TypeScript — supabase/functions/greet/index.ts
Deno.serve(async (req) => {
  // Parse the JSON body from the incoming request
  const { name } = await req.json()

  // Return a personalized greeting
  return new Response(
    JSON.stringify({
      greeting: `Hello, ${name}! Welcome to the edge.`
    }),
    { headers: { "Content-Type": "application/json" } }
  )
})
Terminal — Test with curl
curl -X POST https://<ref>.supabase.co/functions/v1/greet \
  -H "Content-Type: application/json" \
  -d '{"name": "Alex"}'
# → {"greeting":"Hello, Alex! Welcome to the edge."}
🔒

This lesson is for Pro members

Unlock all 520+ lessons across 52 courses with Academy Pro.

Already a member? Sign in to access your lessons.

Academy
Built with soul — likeone.ai