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:


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