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

Async triggers

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.

1import { serve } from 'https://deno.land/std@0.131.0/http/server.ts'
2import Stripe from 'https://esm.sh/stripe?target=deno&no-check'
3import { Customer } from 'types'
4
5serve(async (req) => {
6  try {
7    // create a supabase client
8    const supabaseClient = createClient(
9      Deno.env.get('SUPABASE_URL') ?? '',
10      Deno.env.get('SUPABASE_ANON_KEY') ?? ''
11    )
12    // create a stripe client
13    const stripe = Stripe(Deno.env.get('STRIPE_SECRET_KEY'))
14
15    // Get the authorization header from the request.
16    const authHeader = req.headers.get('Authorization').replace("Bearer ","")
17    // Client now respects auth policies for this user
18    supabaseClient.auth.setAuth(authHeader)
19    // set the user profile
20    const user = supabase.auth.user()
21
22    // Retrieve user metadata that only the user is allowed to select
23    const { data, error } = await supabaseClient
24      .from<Customer>('user_profiles')
25      .select('address, tax, billing_email, phone')
26
27    if (error) throw error
28
29    const customer = await stripe.customers.create({
30      description: 'My First Stripe Customer (created by a Supabase edge function)',
31      phone: data.phone,
32      address: data.address,
33      email: user.email,
34    })
35
36    return new Response(JSON.stringify(customer), { status: 200 })
37  } catch (error) {
38    return new Response(JSON.stringify(error), { status: 400 })
39  }
40})
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140

Use functions for every server side function

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

1import { serve } from "https://deno.land/std@0.114.0/http/server.ts";
2import { SmtpClient } from "https://deno.land/x/smtp@v0.7.0/mod.ts";
3const client = new SmtpClient();
4const { hostname, port, username, password } = Deno.env.toObject();
5
6await client.connect({
7  hostname,
8  port: Number(port),
9  username,
10  password,
11});
12
13serve(async (_req) => {
14  await client.send({
15    from: "admin@acme.com",
16    to: "user@gmail.com",
17    subject: "Thank you for signing up",
18    content: "We sell the best roadrunner traps in the world!",
19  });
20
21  return new Response("Email sent", { status: 200 });
22});
1import { serve } from "https://deno.land/std@0.114.0/http/server.ts";
2import * as postgres from "https://deno.land/x/postgres@v0.14.2/mod.ts";
3
4const databaseUrl = Deno.env.get("SUPABASE_DB_URL") ?? "";
5const pool = new postgres.Pool(databaseUrl, 3, true);
6const connection = await pool.connect();
7
8serve(async (req: Request) => {
9  // sql inject your database
10  const { query } = await req.json();
11  const response = await connection.queryObject(query);
12  console.log(response);
13  return new Response("Query executed", { status: 200 });
14});
1import { serve } from "https://deno.land/std@0.114.0/http/server.ts";
2import { createClient } from "https://esm.sh/@supabase/supabase-js@1.33.1";
3
4serve(async (req) => {
5  const SUPABASE_URL = Deno.env.get("SUPABASE_URL") ?? "";
6  const SERVICE_KEY = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY") ?? "";
7  const supabase = createClient(SUPABASE_URL, SERVICE_KEY);
8  if (req.headers.get("Authorization") === "super-secret-key") {
9    const { data } = await supabase.storage
10      .from("newbucket")
11      .download("supameme.png");
12    return new Response(data, { headers: { "content-type": "image/png" } });
13  } else {
14    return new Response("Forbidden", { status: 403 });
15  }
16});
1import { serve } from "https://deno.land/std@0.114.0/http/server.ts";
2import { createClient } from "https://esm.sh/@supabase/supabase-js@1.33.1";
3
4serve(async () => {
5  const SUPABASE_URL = Deno.env.get("SUPABASE_URL") ?? "";
6  const SERVICE_KEY = Deno.env.get("SUPABASE_SERVICE_ROLE_KEY") ?? "";
7  const supabase = createClient(SUPABASE_URL, SERVICE_KEY);
8  const { data } = await supabase.from("todos").select();
9  return new Response(JSON.stringify(data), {
10    status: 200,
11    headers: {
12      "Content-Type": "application/json",
13    },
14  });
15});

Build in a weekend, scale to millions