Edge Functions

Sending Emails


Sending emails from Edge Functions using the Resend API.

Prerequisites#

To get the most out of this guide, you’ll need to:

Make sure you have the latest version of the Supabase CLI installed.

1. Create Supabase function#

Create a new function locally:

1
supabase functions new resend

Store the RESEND_API_KEY in your .env file.

2. Edit the handler function#

Paste the following code into the index.ts file:

1
import { withSupabase } from 'npm:@supabase/server@^1'
2
3
const RESEND_API_KEY = Deno.env.get('RESEND_API_KEY')
4
5
const handler = async (_request: Request): Promise<Response> => {
6
const res = await fetch('https://api.resend.com/emails', {
7
method: 'POST',
8
headers: {
9
'Content-Type': 'application/json',
10
Authorization: `Bearer ${RESEND_API_KEY}`,
11
},
12
body: JSON.stringify({
13
from: 'onboarding@resend.dev',
14
to: 'delivered@resend.dev',
15
subject: 'hello world',
16
html: '<strong>it works!</strong>',
17
}),
18
})
19
20
const data = await res.json()
21
22
return Response.json(data)
23
}
24
25
export default { fetch: withSupabase({ auth: ['user', 'secret'] }, handler) }

3. Deploy and send email#

Run function locally:

1
supabase start
2
supabase functions serve --no-verify-jwt --env-file .env

The function accepts a signed-in user's JWT or a secret key, so it can be triggered from your app (via supabase.functions.invoke) or from a database function. Test it locally with a secret key:

1
curl -i --request POST 'http://localhost:54321/functions/v1/resend' \
2
--header 'apikey: <SUPABASE_SECRET_KEY>'

Deploy function to Supabase:

1
supabase functions deploy resend --no-verify-jwt

4. Try it yourself#

Find the complete example on GitHub.