paint-brush
Sending emails with Edge Functions on Supabaseby@deola
341 reads
341 reads

Sending emails with Edge Functions on Supabase

by Adeola AdeyemoSeptember 15th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

In this tutorial, we learn how to create an edge function to send emails. We will walk through how to trigger email notifications when the `contacts` table has a new entry in the next article. To follow along, you need basic knowledge of JavaScript. There are two ways to do this: set the variables in the `env` file in the root of your project or set the API keys in the environment variables.
featured image - Sending emails with Edge Functions on Supabase
Adeola Adeyemo HackerNoon profile picture



This is Part 2 of my Supabase tutorial series. If you haven't, please check out the previous article (How to create a contact form with Supabase & Next JS). We will make use of the same Supabase project and API keys.


In this tutorial, we learn how to create an edge function to send emails. We will walk through how to trigger email notifications when the contacts table has a new entry in the next article.


To follow along, you need basic knowledge of JavaScript.

You can also check it out on Github.

Edge functions

Edge Functions are server-side TypeScript functions, distributed globally at the edge - close to your users. They can be used for listening to webhooks or integrating your Supabase project with third-parties like Stripe."


We can deploy server-side code that works as API endpoints. If you have worked with Firebase or Netlify, this is similar to how Cloud functions and Netlify functions work.


Initialize supabase locally

To create an edge function in our project, we need the supabase CLI. If you don't have this installed, follow the instructions here.


Check that you have the latest version installed to avoid spending 2 days debugging like me.

brew install supabase/tap/supabase


Next, initialize a supabase project and follow the instructions.

supabase init


Next, link your supabase database to your local project. Replace [reference_id] with the project reference, you can check yours in the General tab of the project settings page. The database password is in the Database tab also.


supabase link --project-ref [reference_id]


Once this is done, we should have a supabase folder at the root of our local project with the config.toml file.


Create function

Next, we create an edge function named email.


supabase functions new email


This creates an email function. This function is situated in supabase/functions folder. All our functions will be added to this folder.


The default email function should look like this.

default function


Next, modify the email function to be functional. Feel free to use any email service you like. For the sake of this example, we will use resend. Thankfully, there is a guide here.


email function


A few things to note here. First, notice the email, body, and name values from our contacts table in Part 1 of this series. We also use RESEND_API_KEY from our environment variables (more on this in the next section). Feel free to use more environment variables.


Environment variables

We need to set the RESEND_API_KEY variable to use. There are two ways to do this.


The first involves creating an .env file in the root supabase folder. Remember to add .env to your .gitignore file. This will add all the variables in the .env file.


supabase secrets set --env-file ./supabase/.env


Alternatively, you can set the variables individually.

supabase secrets set RESEND_API_KEY=value


Note that some environment variables have been set by default. To view all environment variables, run:

supabase secrets list


For a more comprehensive explanation, check the functions guide or CLI documentation.


Deployment

To deploy the email function, simply run:

supabase functions deploy email


Feel free to invoke your function in your terminal to test. Replace the FUNCTION_URL and API_KEY with yours.

curl --request POST 'FUNCTION_URL' \
  --header 'Authorization: Bearer API_KEY' \
  --header 'Content-Type: application/json' \
  --data '{"email":"[email protected]", "name":"Test person", "body":"This is a test email"}'



Conclusion

I hope this tutorial helps to get started with edge functions on supabase.


Next, we learn how to trigger email notifications when our contact form (from Part 1) gets a new submission.


Till next time…


Also published here.

Lead image by Carl Nenzen Loven on Unsplash