Edge Functions

Serverless Edge Functions
that automatically scale

Execute your code closest to your users with fast deploy times and low latency.

database header
database header

Instant deployment

Deploy Edge Functions in seconds

Global

Deployed to 29 regions worldwide

Typescript ready

TypeScript, WASM, ES Modules

Database webhooks

Invoke Edge Functions based on any event in your database

Anatomy of an Edge Function

Asynchronous tasks within minutes using Supabase Functions with simple authenticated access to the rest of the Supabase Ecosystem.

import { serve } from 'https://deno.land/std@0.131.0/http/server.ts'
import Stripe from 'https://esm.sh/stripe?target=deno&no-check'
import { Customer } from 'types'

serve(async (req) => {
  try {
    // create a supabase client with Auth context of the user that called the function
    const supabaseClient = createClient(
      Deno.env.get('SUPABASE_URL') ?? '',
      Deno.env.get('SUPABASE_ANON_KEY') ?? '',
      { global: { headers: { Authorization: req.headers.get('Authorization')! } } }
    )
    // create a stripe client
    const stripe = Stripe(Deno.env.get('STRIPE_SECRET_KEY'))

    // get the current logged in user
    const {data: { user },} = supabase.auth.getUser()

    // Retrieve user metadata that only the user is allowed to select
    const { data, error } = await supabaseClient
      .from<Customer>('user_profiles')
      .select('address, tax, billing_email, phone')

    if (error) throw error

    const customer = await stripe.customers.create({
      description: 'My First Stripe Customer (created by a Supabase edge function)',
      phone: data.phone,
      address: data.address,
      email: user.email,
    })

    return new Response(JSON.stringify(customer), { status: 200 })
  } catch (error) {
    return new Response(JSON.stringify(error), { status: 400 })
  }
})



































































































Use functions for every server side function

Edge Functions are perfect for running code for sensitive use cases or interacting with 3rd party services.

import { serve } from "https://deno.land/std@0.114.0/http/server.ts";
import { SmtpClient } from "https://deno.land/x/smtp@v0.7.0/mod.ts";
const client = new SmtpClient();
const { hostname, port, username, password } = Deno.env.toObject();

await client.connect({
  hostname,
  port: Number(port),
  username,
  password,
});

serve(async (_req) => {
  await client.send({
    from: "admin@acme.com",
    to: "user@gmail.com",
    subject: "Thank you for signing up",
    content: "We sell the best roadrunner traps in the world!",
  });

  return new Response("Email sent", { status: 200 });
});
import { serve } from "https://deno.land/std@0.114.0/http/server.ts";
import * as postgres from "https://deno.land/x/postgres@v0.14.2/mod.ts";

const databaseUrl = Deno.env.get("SUPABASE_DB_URL") ?? "";
const pool = new postgres.Pool(databaseUrl, 3, true);
const connection = await pool.connect();

serve(async (req: Request) => {
  // sql inject your database
  const { query } = await req.json();
  const response = await connection.queryObject(query);
  console.log(response);
  return new Response("Query executed", { status: 200 });
});
import { serve } from "https://deno.land/std@0.114.0/http/server.ts";
import { createClient } from "https://esm.sh/@supabase/supabase-js@1.33.1";

serve(async (req) => {
  const SUPABASE_URL = Deno.env.get("SUPABASE_URL") ?? "";
  const SERVICE_KEY = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY") ?? "";
  const supabase = createClient(SUPABASE_URL, SERVICE_KEY);
  if (req.headers.get("Authorization") === "super-secret-key") {
    const { data } = await supabase.storage
      .from("newbucket")
      .download("supameme.png");
    return new Response(data, { headers: { "content-type": "image/png" } });
  } else {
    return new Response("Forbidden", { status: 403 });
  }
});
import { serve } from "https://deno.land/std@0.114.0/http/server.ts";
import { createClient } from "https://esm.sh/@supabase/supabase-js@1.33.1";

serve(async () => {
  const SUPABASE_URL = Deno.env.get("SUPABASE_URL") ?? "";
  const SERVICE_KEY = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY") ?? "";
  const supabase = createClient(SUPABASE_URL, SERVICE_KEY);
  const { data } = await supabase.from("todos").select();
  return new Response(JSON.stringify(data), {
    status: 200,
    headers: {
      "Content-Type": "application/json",
    },
  });
});

Build in a weekend, scale to millions