Prepare for the PgBouncer and IPv4 deprecations on 26th January 2024

Home

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:


_10
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:


_28
const RESEND_API_KEY = Deno.env.get('RESEND_API_KEY')
_28
_28
const handler = async (_request: Request): Promise<Response> => {
_28
const res = await fetch('https://api.resend.com/emails', {
_28
method: 'POST',
_28
headers: {
_28
'Content-Type': 'application/json',
_28
Authorization: `Bearer ${RESEND_API_KEY}`,
_28
},
_28
body: JSON.stringify({
_28
from: 'onboarding@resend.dev',
_28
to: 'delivered@resend.dev',
_28
subject: 'hello world',
_28
html: '<strong>it works!</strong>',
_28
}),
_28
})
_28
_28
const data = await res.json()
_28
_28
return new Response(JSON.stringify(data), {
_28
status: 200,
_28
headers: {
_28
'Content-Type': 'application/json',
_28
},
_28
})
_28
}
_28
_28
Deno.serve(handler)

3. Deploy and send email

Run function locally:


_10
supabase start
_10
supabase functions serve --no-verify-jwt --env-file .env

Test it: http://localhost:54321/functions/v1/resend

Deploy function to Supabase:


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

Open the endpoint URL to send an email:

4. Try it yourself

Find the complete example on GitHub.