0
0
Supabasecloud~5 mins

Creating Edge Functions with Deno in Supabase

Choose your learning style9 modes available
Introduction

Edge Functions let you run small programs close to users for faster responses. Deno is a simple tool to write these programs safely.

You want to run code that responds quickly to user actions on your website.
You need to process data or requests without waiting for a full server.
You want to add custom logic like authentication or data filtering near users.
You want to reduce delays by running code geographically close to users.
Syntax
Supabase
import { serve } from "https://deno.land/std@0.177.0/http/server.ts";

serve(async (req) => {
  // Your code here
  return new Response("Hello from Edge Function!", { status: 200 });
});

Use serve to start your function and handle requests.

Return a Response object to send data back to the user.

Examples
A simple function that always replies with "Hi there!".
Supabase
import { serve } from "https://deno.land/std@0.177.0/http/server.ts";

serve(() => new Response("Hi there!"));
This function reads a name from the URL and greets that name.
Supabase
import { serve } from "https://deno.land/std@0.177.0/http/server.ts";

serve(async (req) => {
  const url = new URL(req.url);
  const name = url.searchParams.get("name") || "friend";
  return new Response(`Hello, ${name}!`);
});
Sample Program

This Edge Function responds with a greeting when the path is "/hello" and returns 404 for other paths.

Supabase
import { serve } from "https://deno.land/std@0.177.0/http/server.ts";

serve(async (req) => {
  const url = new URL(req.url);
  if (url.pathname === "/hello") {
    return new Response("Hello from your Edge Function!", { status: 200 });
  }
  return new Response("Not Found", { status: 404 });
});
OutputSuccess
Important Notes

Edge Functions run in a secure, limited environment for safety.

Deno uses modern JavaScript and TypeScript, so you can use new language features.

Keep functions small and fast for best performance near users.

Summary

Edge Functions run code close to users for speed.

Deno provides a simple way to write these functions using serve.

Return Response objects to send replies to requests.